在 T-SQL 脚本中使用双引号在 SQLCMD 中会失败,但在 SSMO 中不会
通过 sqlcmd 执行以下脚本失败。但是,通过 ssmo 或 SQL Server Management Studio 执行它是可行的。
sqlcmd -S . -d test -i input.sql
input.sql:
CREATE FUNCTION test()
RETURNS @t TABLE ("ID" INT)
AS
BEGIN
RETURN
END
即使我将 SQL Server Management Studio 置于 sqlcmd 模式,它仍然失败。当我们使用 SSMS 测试脚本但使用 SQLCMD 部署时,这是一个问题。因此,当我们尝试部署时,我们只会发现我们的代码不起作用。
为什么 sqlcmd 会有这样的行为?有什么办法可以关掉它吗?
Executing the following script via sqlcmd fails. However, executing it via ssmo or SQL Server Management Studio works.
sqlcmd -S . -d test -i input.sql
input.sql:
CREATE FUNCTION test()
RETURNS @t TABLE ("ID" INT)
AS
BEGIN
RETURN
END
Even when I put SQL Server Management Studio into sqlcmd mode, it still fails. This is a problem as we test our scripts with SSMS, but deploy with SQLCMD. Thus we only find out our code doesn't work when we attempt to deploy.
Why does sqlcmd behave like this? Is there any way to turn it off?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您确实需要在脚本中使用双引号来分隔标识符,则需要在 SQLCMD 通过使用
-Ienable
开关。它在 SQLCMD 中默认为 OFF,但在 SSMS 中默认为 ON,这就是您的测试在 SSMS 中工作的原因。请此处了解有关 QUOTED_IDENTIFIER 的更多信息。
If you really need to use double quotes to delimit identifiers in your scripts, you'd need to set the quoted_identifier option on SQLCMD by using the
-Ienable
switch. It is OFF by default in SQLCMD but ON by default in SSMS, which is why your tests work in SSMS.Read more about QUOTED_IDENTIFIER here.
使用方括号:
[ID]
Use square brackets for this:
[ID]