单行 GO(Batch) 语句在 SQL Server 中出错?

2021-09-10 00:00:00 sql tsql sql-server-2008 sql-server

我有一个问题.我正在创建以下程序临时.

I have one question. I was creating below procedure temporary.

当我以下面的格式执行它时,它工作正常:

When I Execute it in below format it works fine:

CREATE PROCEDURE Get_TableList_By_Name
@Proc_Name VARCHAR(255)
AS
BEGIN
    SELECT * FROM sys.tables WHERE name LIKE '%' + @Proc_Name + '%'
END 
GO
EXEC Get_TableList_By_Name 'norway'
GO
DROP PROCEDURE Get_TableList_By_Name 
GO

但是当我以下面的格式执行相同的 SQL 时,它给出错误提示:'GO' 附近的语法不正确."

But when I execute same SQL in below format it giving error saying: "Incorrect syntax near 'GO'."

CREATE PROCEDURE Get_TableList_By_Name @Proc_Name VARCHAR(255) AS BEGIN SELECT * FROM sys.tables WHERE name LIKE '%' + @Proc_Name + '%' END GO EXEC Get_TableList_By_Name 'norway' GO DROP PROCEDURE Get_TableList_By_Name GO

CREATE PROCEDURE Get_TableList_By_Name @Proc_Name VARCHAR(255) AS BEGIN SELECT * FROM sys.tables WHERE name LIKE '%' + @Proc_Name + '%' END GO 1 EXEC Get_TableList_By_Name 'norway' GO 1 DROP PROCEDURE Get_TableList_By_Name GO 1

如何在单行中用 GO 语句编写相同的 SQL?是否可以?如果不是,那为什么?

How to write same SQL with GO statement in single line? Is it possible? If not then Why?

谢谢,维沙尔

推荐答案

来自 GO (Transact-SQL)

Transact-SQL 语句不能与 GO 命令占据同一行.但是,该行可以包含注释.

A Transact-SQL statement cannot occupy the same line as a GO command. However, the line can contain comments.

所以除了注释之外,Go 需要独占一行.

So Go needs to be on its own line, except for comments.

相关文章