从批处理文件中运行SQL命令并获得错误

时间:2022-10-04 02:04:43

Good morning everyone. Wondering if anyone can help me out. I am trying to write a batch file to make my life and others lives easier in my job but wanted to seek out some help. I typically run a bunch of different SQL scripts that I wrote to install a particular installation and decided life would be easier if i moved these all to batch files. I am trying to run the following command from a batch file:

大家早上好。不知道是否有人能帮我。我试着写一个批次文件,让我的生活和其他人在我的工作中更轻松,但我想寻求一些帮助。我通常会运行一堆不同的SQL脚本,这些脚本是我为安装某个特定的安装而编写的,我认为如果我将这些脚本全部转移到批处理文件中,生活会更轻松。我正在尝试从一个批处理文件中运行以下命令:

:T02
cls
echo Qualifiers will be installed next with names of
echo Q_AutoFax_CC_1
echo Q_AutoFax_CC_2
echo Q_AutoFax_CC_3
echo Q_AutoFax_CC_4
echo Q_AutoFax_CC_5
pause
cls
If Exist "C:\Program Files\Microsoft SQL Server\100\Tools\Binn\bcp.exe" cd "C:\Program Files\Microsoft SQL Server\100\Tools\Binn"
If Exist "C:\Program Files\Microsoft SQL Server\90\Tools\Binn\bcp.exe" cd "C:\Program Files\Microsoft SQL Server\90\Tools\Binn"
If Exist "C:\Program Files\Microsoft SQL Server\80\Tools\Binn\bcp.exe" cd "C:\Program Files\Microsoft SQL Server\80\Tools\Binn"


sqlcmd /U eiw_admin /P eiw_admin /d cabinet /S . /Q 
"declare @q int
select @q = 1
While @q < 6
    Begin
        Insert into cabinet..qualifiers 
        select 'HL7', 'Q_AutoFax_CC_'+ convert(varchar(2),@q), 
        '%Physician_Code'+ convert(varchar(2),@q)+'% == (NULL) || %Physician_Code'+ convert(varchar(2),@q)+'% == (BLANK)', 'R'    
    select @q = @q + 1
    End"

pause
cls

I typically run just:

我通常只运行:

declare @q int
select @q = 1
While @q < 6
    Begin
        Insert into cabinet..qualifiers 
        select 'HL7', 'Q_AutoFax_CC_'+ convert(varchar(2),@q), 
        '%Physician_Code'+ convert(varchar(2),@q)+'% == (NULL) || %Physician_Code'+ convert(varchar(2),@q)+'% == (BLANK)', 'R'    
    select @q = @q + 1
    End

from SQL and it runs just fine...i have used SQL commands before in a batch file...is there something different that needs to be done for this one since it is a loop and not a generic command? The file is being ran from the database server and I am getting an error:

从SQL开始,它运行得很好……我以前在批处理文件中使用过SQL命令…因为这是一个循环,而不是一个通用命令,所以需要对它做一些不同的事情吗?文件正在从数据库服务器上运行,我得到了一个错误:

Sqlcmd: '-Q': Missing argument. Enter '-?' for help. ' "declare @q int' is not recognized as an internal or external command, operable program or batch file

.....same thing with select, begin, insert and select....etc

.....使用选择一样,首先,插入并选择....等等

I thought if you ever wanted to run SQLCMD, you just had to put your command in single quotes, is this not correct?

我想如果你想要运行SQLCMD,你只需要把命令放在单引号中,这不对吗?

1 个解决方案

#1


1  

Your SQL statement in the batch file should be on the same line as the sqlcmd statement. It might be easiest to put the SQL statement in a seperate file, and use the -i switch to read the query from your file.

批处理文件中的SQL语句应该与sqlcmd语句位于同一行。将SQL语句放在一个独立的文件中,并使用-i开关从文件中读取查询,这可能是最简单的方法。

#1


1  

Your SQL statement in the batch file should be on the same line as the sqlcmd statement. It might be easiest to put the SQL statement in a seperate file, and use the -i switch to read the query from your file.

批处理文件中的SQL语句应该与sqlcmd语句位于同一行。将SQL语句放在一个独立的文件中,并使用-i开关从文件中读取查询,这可能是最简单的方法。