消息 102,级别 15,状态 1 第 1 行 “n”附近的语法不正确

发布于 2024-11-05 12:57:36 字数 926 浏览 1 评论 0原文

我有以下查询,我正在使用批处理文件运行。在批处理文件中,我使用以下语法:

echo populating Application table with values...

SET "installFile=%sqlDir%\Install\DataFiles\Insert_ApplicationNames.sql"

OSQL /n /i "%installFile%" /d%db% /U%user% /P%pswd% /S%serv%
echo
echo populated Application table with values in Insert_ApplicationNames.sql
echo

从 SQL Management Studio 执行时,下面显示的 sql 运行时没有任何错误,但作为批处理脚本的一部分运行时,它不断出错。有人可以帮我找出我可能做错了什么吗?

此外,行确实被插入,但我们的夜间 QA 安装由于批处理脚本抛出的错误而中断。

IF NOT EXISTS(SELECT * FROM Application WHERE name = '')
BEGIN
    INSERT INTO Application
    (Name)
    VALUES
    ('')
END
GO
IF NOT EXISTS(SELECT * FROM Application WHERE name = 'App1.exe')
BEGIN
    INSERT INTO Application
    (Name)
    VALUES
    ('App1.exe')
END
GO
IF NOT EXISTS(SELECT * FROM Application WHERE name = 'App2.exe')
BEGIN
    INSERT INTO Application
    (Name)
    VALUES
    ('App2.exe')
END

I have the following query which I am running using a batch file. In the batch file I use the following syntax:

echo populating Application table with values...

SET "installFile=%sqlDir%\Install\DataFiles\Insert_ApplicationNames.sql"

OSQL /n /i "%installFile%" /d%db% /U%user% /P%pswd% /S%serv%
echo
echo populated Application table with values in Insert_ApplicationNames.sql
echo

The sql shown below runs without any errors when executed from the SQL Management Studio, but it keeps erroring out when run as a part of the batch script. Could some one help me find what I may be doing wrong here?

Also, the rows do get inserted, but our nightly QA install breaks because of the error thrown by the batch script.

IF NOT EXISTS(SELECT * FROM Application WHERE name = '')
BEGIN
    INSERT INTO Application
    (Name)
    VALUES
    ('')
END
GO
IF NOT EXISTS(SELECT * FROM Application WHERE name = 'App1.exe')
BEGIN
    INSERT INTO Application
    (Name)
    VALUES
    ('App1.exe')
END
GO
IF NOT EXISTS(SELECT * FROM Application WHERE name = 'App2.exe')
BEGIN
    INSERT INTO Application
    (Name)
    VALUES
    ('App2.exe')
END

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

简单 2024-11-12 12:57:36

GO 是 Management Studio 中的(默认)批处理分隔符关键字,但它不是真正的 SQL 关键字(即 SQL Server 无法识别它)。

从你的脚本中删除它们——在你提供的脚本中,它们无论如何都是无关紧要的——你应该很好,嗯,走。

GO is the (default) batch separator keyword in Management Studio, but it isn't a real SQL keyword (i.e., SQL Server doesn't recognize it).

Remove those from your script -- in the script you've provided, they are irrelevant anyway -- and you should be good to, um, go.

闻呓 2024-11-12 12:57:36

好奇你的变量是否应该与开关相对应。试试这个?

OSQL -n -i "%installFile%" -d %db% -U %user% -P %pswd% -S %serv%

当您在命令中使用上面的行和已知的正确值时会发生什么?

OSQL -n -i "C:\foo.sql" -d MyDB -U MyUser -P MyPwd -S MyServ

Curious whether your variables should be right up against the switches. Try this?

OSQL -n -i "%installFile%" -d %db% -U %user% -P %pswd% -S %serv%

What happens when you use the line above with your known good values right in the command?

OSQL -n -i "C:\foo.sql" -d MyDB -U MyUser -P MyPwd -S MyServ
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文