由于 T-SQL 查询的注释部分,SQL 查询失败

SQL*_*Boy 6 t-sql sql-server-2008-r2 sql-server-2014

我正在执行下面的查询,但我遇到了问题。

注意:SQL 2008 R2 和 SQL 2014 在 Win 2008 R2 和 Win 2012 R2 上使用两个差异服务帐户运行。TCP 端口、防火墙端口都与远程连接一起启用,我能够在本地机器上执行代码(例如:如果我连接到 SQL 2008 R2 机器并在 SQL 2008 R2 上执行成功,如果我连接到 SQL 2014 机器成功在 SQL 2014 上的 SSMS 上。问题仅在我连接到 SQL 2008 R2 并在 SQL 2014 上执行时出现。当我删除注释部分时,我能够使用 SQLCMD 成功执行代码。)我不知道这有任何兼容性问题案件。

Msg 121, Level 20, State 0, Line 0
从服务器接收结果时出现传输级错误。(提供者:TCP 提供者,错误:0 - 信号量超时期限已过期。)

但是当我删除注释部分并执行查询时,它在几秒钟内成功运行。请让我知道评论部分有什么问题。我在下面粘贴了 T-SQL 查询。

/********************************************************************************
* Summary: Creates source database schema for BCP Import. Only created PK on the tables
* No other constraints are replicated
* Parameters: 
* tempdbname
* Called by: PROCS.uspEngagement_Import
* Remarks: 
* Copyright: Copyright 2012 by XXXXX XXXXX. All rights reserved.
* Author: XXXXX
* Creation Date: July 07 2012
* Revision History:
* <Modified By> <Date Modified> <Changes made>
* XXXXX     July 31 2012    Removed IF EXISTS as this is required for non sysadmin user
* XXXXX     July 31 2012    Removed reference to sys.database_files as it returns NULL for non sysadmin users
* XXXXX     Aug 26 2012     Added Logic for FILESTREAM, ROWGUIDCOL and UNIQUE Columns. Note FILESTREAM
                                Can be created only if there is a UNIQUE ROWGUIDCOL
* XXXXX     Sep 08 2012     Removed logic to create default constraints. The defaults are not required besides 
                                are hinderance for schema upgrades
* XXXXX     Feb 02 2013     Added logic to avoid further generation of script if DB already exists
* XXXXX     Oct 24 2013     Added logic to create Database mdf and ldf in the path specified by @pstrFilePath
                                Required for Bug# 29221
                                Further removed comments and modified indentation
* XXXXX     Jan 06 2014     Refer Bug 30633. Conversion fails if schemas with '\' are present
                                in the source XXXXX Database. Added square braces '[' to take care of this issue
                                when generating schemas
                                Further removed the redundant code to PRINT 'Database Exists' if the Temp DB 
                                already exists
* XXXXX/XXXXX Dec 09 2014    PaperSizeinBytes column having issues during BCP IN, added logic to manage computed columns and its definition.
                                Computed column definition being hard coded now as xp_cmdshell cannot read from sys.computed_columns
* XXXXX  Dec 18 2015        Bug fix#73849: Conversion creates database in paths not excluded by antivirus
                                (mdf, ldf and filestrTemp DB to be created where current XXXXX DB SQL Server is configured)
*********************************************************************************/
Run Code Online (Sandbox Code Playgroud)

Sol*_*zky 15

鉴于脚本在没有标头注释块的情况下工作但失败(但仅适用于远程/TCP 连接),该问题很可能是由于解析错误造成的。例如,SQLCMD 和 SSMS 对提交的脚本进行自己的预解析(将其分解为批处理),有时预解析无法正常工作(例如,嵌套块注释的第二部分中的“GO”会中断批处理解析在 SSMS 和 SQLCMD 中)。要缩小范围,您可以尝试以下测试:

  1. 突出显示/*...*/顶部的整个注释块,然后点击Control-KControl-C通过内嵌注释(即--)注释每一行:

    --/********************************************************************************
    --* Summary: Creates source database schema for BCP Import. Only created PK on ...
    --...
    --*********************************************************************************/
    
    Run Code Online (Sandbox Code Playgroud)
  2. 仅删除注释块的一部分。所以也许可以从删除大部分开始(保留前 3 行和底线***/)。然后,如果它有效,继续添加行,从第 4 行开始,直到它停止工作。意思是,注释块将开始如下:

    /********************************************************************************
    * Summary: Creates source database schema for BCP Import. Only created PK on ...
    * No other constraints are replicated
    *********************************************************************************/
    
    Run Code Online (Sandbox Code Playgroud)

    如果可行,请添加第 4 行,以便注释块现在是:

    /********************************************************************************
    * Summary: Creates source database schema for BCP Import. Only created PK on ...
    * No other constraints are replicated
    * Parameters: 
    *********************************************************************************/
    
    Run Code Online (Sandbox Code Playgroud)

OP 尝试了上面的方法 #2,并且仅删除了以下行(从底部算起第 4 行)就能够使脚本远程工作:

/*******...
  Computed column definition being hard coded now as xp_cmdshell cannot read from sys.computed_columns
...*******/
Run Code Online (Sandbox Code Playgroud)

OP 然后使用内联注释(上面的方法#1)缩小违规行的子集,并能够使以下内容起作用:

/*******...
  Computed column definition being hard coded now as --xp_cmdshell cannot read from sys.computed_columns
...*******/
Run Code Online (Sandbox Code Playgroud)

目前,尚不清楚问题的确切根本原因。我无法使用完全相同的注释块重现此错误,在 SSMS 2008 R2 和 SSMS 2012 中进行测试,甚至启用 SQLCMD 模式强制建立 TCP/IP 连接(通过 验证SELECT net_transport FROM sys.dm_exec_connections WHERE session_id = @@SPID;)。@MartinSmith 在讨论中询问是否“客户端和服务器之间存在任何可能对 xp_cmdshell 产生异常的网络安全性”。这仍然是一种可能性,因此需要更多的测试来确定哪个层不满意xp_cmdshell(或新添加的右侧的该行的其他部分--)。



更新

虽然尚未确定根本原因,但以下是更多信息,希望有助于缩小范围:

  1. OP 通过 SSMS、SQLCMD 和 .NET SqlClient 运行以下两个语句:

    1. 仅运行xp_cmdshell(单独运行)会出现“缺少参数”错误,而不是超时

    2. 运行xp_cmdshell 'cls'得到超时错误。错误包括“HRESULT:-2146232060”。

      SELECT CONVERT(VARBINARY(30), -2146232060 );
      -- 0x80131904
      
      Run Code Online (Sandbox Code Playgroud)

      搜索“HRESULT: 0x80131904”发现这是一个不太具体的 SQL Server 错误(但由于某种原因主要与 SharePoint 相关)。以下是它的一些变化:

  2. OP 在 SSMS 中运行以下三个查询并收到“语法不正确”错误:

    select 1/0; 
    go 
    xp_cmdshell --comment 
    'cls';
    
    Run Code Online (Sandbox Code Playgroud)

    和:

    select 1/0; 
    go 
    axp_cmdshell --comment 
    'cls';
    
    Run Code Online (Sandbox Code Playgroud)

    和:

    select 1/0; 
    go 
    p_cmdshell 'cls';
    
    Run Code Online (Sandbox Code Playgroud)

    所有这三个查询都收到以下错误:

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

  3. OP 在 SSMS 中运行以下查询并收到不同的“语法不正确”错误,但没有超时错误:

    select 1/0; 
    xp_cmdshell 'cls';
    
    Run Code Online (Sandbox Code Playgroud)

    该查询收到以下错误:

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

  4. @Tom V 提到此问题已在 Microsoft Connect 上提交,但也已关闭为“不可重现”:

    文本“xp_cmdshell”导致传输级错误:来自远程系统的信号量超时期限已过期。

  5. 这是两个非常相似的报告(都在 SO 上,但都没有任何解决方案):