Ran*_*Dev 5 sql-server cte ssms sp-whoisactive
我有几个相同的(据我所知)SQL Server,我最近在其中添加了 sp_WhoIsActive(向一些人展示了我有多喜欢这个工具),但其中一个不允许我创建存储过程。我收到一个我非常熟悉的错误,该错误涉及 CTE 需要前面的语句以分号结尾。其他服务器均未收到此错误。
我将脚本归结为以下内容以重新创建问题:
SET QUOTED_IDENTIFIER ON;
SET ANSI_PADDING ON;
SET CONCAT_NULL_YIELDS_NULL ON;
SET ANSI_WARNINGS ON;
SET NUMERIC_ROUNDABORT OFF;
SET ARITHABORT ON;
GO
IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME = 'sp_WhoIsActive')
EXEC ('CREATE PROC dbo.sp_WhoIsActive AS SELECT ''stub version, to be replaced''')
GO
ALTER PROC dbo.sp_WhoIsActive
(
@filter sysname = ''
)
AS
BEGIN;
SET NOCOUNT ON;
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
SET QUOTED_IDENTIFIER ON;
SET ANSI_PADDING ON;
SET CONCAT_NULL_YIELDS_NULL ON;
SET ANSI_WARNINGS ON;
SET NUMERIC_ROUNDABORT OFF;
SET ARITHABORT ON;
IF @filter IS NULL
BEGIN;
RAISERROR('Input parameters cannot be NULL', 16, 1);
RETURN;
END;
--SELECT 'FIZZ' AS BUZZ;
WITH
a0 AS
(SELECT 'FOO' AS BAR)
SELECT * FROM a0;
END;
GO
Run Code Online (Sandbox Code Playgroud)
如果您取消注释该SELECT 'FIZZ' AS BUZZ;
行,则会创建它。
有人知道这个服务器有什么不同吗?
我检查过的事情:
实际错误全文如下:
Msg 319, Level 15, State 1, Procedure sp_WhoIsActive, Line 206 [Batch Start Line 11]
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Msg 102, Level 15, State 1, Procedure sp_WhoIsActive, Line 209 [Batch Start Line 11]
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Procedure sp_WhoIsActive, Line 212 [Batch Start Line 11]
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Procedure sp_WhoIsActive, Line 215 [Batch Start Line 11]
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Procedure sp_WhoIsActive, Line 218 [Batch Start Line 11]
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Procedure sp_WhoIsActive, Line 221 [Batch Start Line 11]
Incorrect syntax near ','.
Msg 156, Level 15, State 1, Procedure sp_WhoIsActive, Line 225 [Batch Start Line 11]
Incorrect syntax near the keyword 'ORDER'.
Msg 102, Level 15, State 1, Procedure sp_WhoIsActive, Line 230 [Batch Start Line 11]
Incorrect syntax near ','.
Msg 319, Level 15, State 1, Procedure sp_WhoIsActive, Line 443 [Batch Start Line 11]
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Msg 102, Level 15, State 1, Procedure sp_WhoIsActive, Line 446 [Batch Start Line 11]
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Procedure sp_WhoIsActive, Line 449 [Batch Start Line 11]
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Procedure sp_WhoIsActive, Line 452 [Batch Start Line 11]
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Procedure sp_WhoIsActive, Line 455 [Batch Start Line 11]
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Procedure sp_WhoIsActive, Line 458 [Batch Start Line 11]
Incorrect syntax near ','.
Msg 156, Level 15, State 1, Procedure sp_WhoIsActive, Line 462 [Batch Start Line 11]
Incorrect syntax near the keyword 'ORDER'.
Msg 102, Level 15, State 1, Procedure sp_WhoIsActive, Line 468 [Batch Start Line 11]
Incorrect syntax near ','.
Completion time: 2023-01-06T15:04:08.0850888-05:00
Run Code Online (Sandbox Code Playgroud)
我可以通过进入连接属性和设置Column Encryption Setting=Enabled
(或者只是勾选“启用始终加密”框)来重现此问题
我建议删除它以使过程创建成功。
显然,这个选项会对源文本进行一些修改,包括删除注释和一些不正确的分号应用。
注释删除将是您的行号远低于预期的原因(ORDER
代码中的第一个实例比第 225 行晚得多)。
当我执行时
CREATE OR ALTER PROC #foo
AS
BEGIN;
X: /*This is a comment*/
END;
WITH a0
AS (SELECT 'FOO' AS BAR)
SELECT *
FROM a0;
Run Code Online (Sandbox Code Playgroud)
启用该选项后,我看到实际执行的是
CREATE OR ALTER PROCEDURE #foo
AS
BEGIN
X:
END
WITH a0
AS (SELECT 'FOO' AS BAR)
SELECT *
FROM a0;
Run Code Online (Sandbox Code Playgroud)
BEGIN
和(这里有问题)之后的注释、文本缩进、额外换行符和分号END
已被删除。
看起来文本转换实际上会在大多数情况下添加分号,而分号在原始源中并不存在,因此此定义将在启用该选项的情况下成功,而不是像预期的那样因“关键字‘with’附近的语法不正确”而失败。
CREATE OR ALTER PROC #foo
AS
PRINT ''
WITH a0
AS (SELECT 'FOO' AS BAR)
SELECT *
FROM a0;
Run Code Online (Sandbox Code Playgroud)