Search code examples
sqlbatch-filesplitscriptingsqlplus

Split string separated by comma in a batch file and call a sqlplus function in the loop


I'm trying to split a String separated by comma and then call a function of sql with each splitted strings. The length of strings to split is variable.

I have this code:

setlocal enabledelayedexpansion
@echo off
set CCVs="0008123123,000815432123"
for /F "delims=," %%a in ("%CCVs%") do (
    SQLPLUS -S -L %DBCONN% @%~dp0generate.sql %%a
)

But I see that the loop call the SQLPLUS function with the param "0008123123 000815432123" not two times one with string 0008123123 and a second time 000815432123


Solution

  • You need to use a standard FOR command. Also do not get in the habit of assigning quotes to your variables. That is bad practice in regards to coding batch files. It is a best practice though to use them to surround the assignment to protect special characters and from trailing spaces getting assigned to the value.

    @echo off
    set "CCVs=0008123123,000815432123"
    for %%a in (%CCVs%) do (
        SQLPLUS -S -L %DBCONN% @%~dp0generate.sql %%a
    )