SSMS和sqlcmd的TSQL语法不相同

问题描述 投票:2回答:1

这是一个简单的tsql文件:

# file foo.sql :
DROP TABLE IF EXISTS "Testing";

CREATE TABLE "Testing"
(
    "Id" BIGINT NOT NULL,
    "FilePath" NVARCHAR (442) NULL,
    CONSTRAINT "PK_Testing" PRIMARY KEY CLUSTERED ("Id" ASC)
);

GO

当我通过SQL Server Management Studio加载它时,一切都很好:

  • 连接到服务器,选择一个数据库
  • 打开文件'foo.sql'
  • 运行“执行”
  • 输出:

    Commands completed successfully.
    
    Completion time: 2019-11-07T10:48:37.3336032+01:00
    

当通过sqlcmd将其用作输入时,出现错误:

> sqlcmd -S .\SQLEXPRESS -d DemoDb -i foo.sql
Msg 102, Level 15, State 1, Server MY-LAPTOP\SQLEXPRESS, Line 1
Incorrect syntax near 'Testing'.

问题

在这种情况下,我如何告诉sqlcmd表现得像SSMS?

我应该使用其他行为类似于SSMS的命令行工具吗?


版本信息:

> sqlcmd -?
Microsoft (R) SQL Server Command Line Tool
Version 15.0.1300.359 NT
Copyright (C) 2019 Microsoft Corporation. All rights reserved.

usage: ...

SSMS:v18.3.1

Component name                              Versions
SQL Server Management Studio                15.0.18183.0
Microsoft Analysis Services Client Tools    15.0.1487.0
Microsoft Data Access Components (MDAC)     10.0.18362.1
Microsoft MSXML                             3.0 6.0 
Microsoft Internet Explorer                 9.11.18362.0
Microsoft .NET Framework                    4.0.30319.42000
Operating System                            10.0.18362
sql-server ssms sqlcmd
1个回答
0
投票

SSMS通常会在连接到服务器时设置一堆会话配置。您可以在Options窗口的SQL Server下找到它们(这是旧SSMS 2008的示例):

enter image description here

enter image description here

您应检查已启用的功能和未启用的功能,然后可以将其添加到.sql文件的开头。对于此特定示例,我相信是SET QUOTED_IDENTIFIER导致无法按sqlcmd执行正确识别使用双引号的对象名称,因为它已设置为OFF

© www.soinside.com 2019 - 2024. All rights reserved.