在 Azure SQL 服务器中使用分号时出错

Error using semicolon in Azure SQL Server

我正在使用 Microsoft SQL Azure (RTM) - 12.0.2000.8 Nov 29 2017 09:37:51 Copyright (C) 2017 Microsoft Corporation 和 compatibilty_level=120,当我 运行 这个查询时

ALTER PROCEDURE SPCreateSession3
@a int,
@b int
AS
select count(*) as a from events where IDevent > @a;
select count(*) as b from sessions where IDsession > @b;
GO
exec SPCreateSession3 1, 1

它给我错误 Must declare the scalar variable @b。没有分号,它工作正常。看起来 ; 是某种 GO 命令。那为什么很多人建议here,那;永远不会有什么问题。我需要分号,因为我在现实中使用 MERGE 命令,因此 ; 是必需的。此外,如果我将查询包装在 BEGIN 块中,如

ALTER PROCEDURE SPCreateSession3
@a int,
@b int
AS
BEGIN
select count(*) as a from events where IDevent > @a;
select count(*) as b from sessions where IDsession > @b;
END
GO
exec SPCreateSession3 1, 1

然后,sql 服务器说 incorrect syntax near ;。由于分号再次出现问题。我错过了什么或分号真的应该归咎于此吗?当我需要使用分号而不出现这两个错误时,有什么解决方法?

快照:

这不是 Azure 问题。 T-SQL 中的分号结束批处理(范围)。在分号之后,您将开始一个新的批处理(作用域),并且在前一个批处理中声明的任何变量都不再有效。在这方面,半色的作用与 GO 语句相同;它结束了一批语句。

作为此问题的解决方法,请使用 Microsoft SQL Operations Studio 作为 Linux 和 MAC 的替代查询编辑器。从 here 下载。使用 Operations Studio 就不会有这个问题。

用户目前要求 SQL Server Management Studio 成为跨平台工具,如您所见here。现在您可以使用 Microsoft SQL Operations Studio。