将.BAT文件与SQL结合使用以进行用户输入

时间:2015-10-06 20:52:06

标签: sql sql-server batch-file

我有一个如下所示的批处理文件:

ECHO OFF

 set /P sn= Enter Customer ID: 
 sqlcmd -S C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\Trial  -i 
  deletesql.sql -o delete.txt -v delete=%sn%

数据库名称为Trial

我的 deletesql.sql 文件搜索数据库并删除用户输入的特定ID的每条记录。例如,如果用户键入 1 ,代码应在数据库中搜索包含 CustomerID 作为列的每个表,并将 1 作为值

这是我的deletesql.sql代码:

declare @columnName nvarchar(255)
declare @intValue int
set @columnName = 'CustomerId' --COLUMN NAME WHERE VALUE IS LOCATED
set @intValue = 2              --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 + ' = ' + @DeleteValue + ';'
from sys.columns c 
where c.name = @columnName

select @sql

exec sp_executesql @sql

代码完全由它自己运行,但我试图让它与.bat文件中的用户输入一起工作正如你所看到的,我硬编码了列名和值我的.sql文件。

如何更改我的.sql和.bat文件,以便我的.sql文件取决于我的.bat文件输入(列名和值)

我的bat文件中还有3个错误:

Sqlcmd: Error: Microsoft SQL Server Native Client 11.0 : SQL Server Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF]. .
Sqlcmd: Error: Microsoft SQL Server Native Client 11.0 : Login timeout expired.
Sqlcmd: Error: Microsoft SQL Server Native Client 11.0 : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..

0 个答案:

没有答案