0

BATCH FILE:

ECHO OFF

SET /P colu= Enter column name:
SET /P sn= Enter ID (split by commas): 

rem Grab filenames in an array
set n=0
for %%a in (%SN%) do (
   set /A n+=1
   set "id[!n!]=%%~a"
)

rem For example, to process the filenames:
for /L %%i in (1,1,%n%) do (
   echo %%i- !id[%%i]!
)

sqlcmd -U USER-P PASSW -S  SERVER -d DB -i sqlFILE.sql -o LOGS.txt -v delete=colu d_id=snode 

I want to delete several ids from the database. This batch file asks the user what they want to be deleted and it is suppose to put the entered values in an array.

These entered values are entered into this SQL script:

declare @columnName nvarchar(255)
declare @intValue int

set @columnName = '$(colu)' --COLUMN NAME WHERE VALUE IS LOCATED
set @intValue = '$(sn)'             --VALUE TO BE DELETED

declare @DeleteValue varchar(10)

set @DeleteValue = convert(varchar(10), @intValue)

declare @sql nvarchar(max) = ''

select @sql =  @sql + 'delete ' + object_name(c.object_id) + ' where ' + @columnName + ' IN ' + @DeleteValue + ';'
from sys.columns c 
where c.name = @columnName

select @sql

exec sp_executesql @sql

The problem is that the program is still looking at the values entered as one whole value and not separate values.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
royalblue
  • 439
  • 2
  • 8
  • 18
  • Similiar case http://stackoverflow.com/a/33010765/5070879 there are many ways, Dynamic-SQL, split using function, split using XML. As you use Dynamic-SQL I recommend using REPLACE – Lukasz Szozda Oct 09 '15 at 16:30
  • So you want to pass the *array* of IDs to an `sqlcmd` command rather than executing `sqlcmd` once per each individual ID like in [your other question](http://stackoverflow.com/a/33042511/5047996), right? – aschipfl Oct 11 '15 at 17:04

0 Answers0