使用批处理文件和sql scipt从数据库中删除

时间:2015-10-09 16:28:53

标签: sql-server batch-file sql-server-2012

批量文件:

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 

我想从数据库中删除几个ID。该批处理文件询问用户他们想要删除的内容,并且假设将输入的值放入数组中。

这些输入的值将输入到此SQL脚本中:

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

问题是程序仍在查看作为整个值输入的值而不是单独的值。

0 个答案:

没有答案