; 之间有什么区别?和 SQL Server 存储过程中的 GO?

发布于 2024-08-22 06:11:45 字数 214 浏览 7 评论 0原文

SQL Server 存储过程中的 ;GO 有什么区别?

实际上,如果我在 SQL Server 中有一个存储过程,并且想在其中放入 t 个单独的查询,第一个查询仅计算记录数(计数),第二个查询根据某些条件选择一些记录,那么我应该在其中使用什么那两个查询?

Go;

What is the difference between ; and GO in stored procedure in SQL Server ?

Actually, if I have a stored procedure in SQL server and wanna to put t separate queries inside it which the first one just calculates number of records (count) and the second one selects some records based on some conditions, then what should I use between that two queries?

Go or ;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情独悲 2024-08-29 06:11:45

; 只是结束语句。

GO 不是一个语句,而是一个命令,让服务器将当前批次提交到数据库。它在交易内创建止损。

http://msdn.microsoft.com/en-us/library/ms188037.aspx

(更新,感谢您的评论):
据我所知,GO 是针对管理工作室的声明,也许也适用于其他工具。

; just ends the statement.

GO is not a statement but a command to the server to commit the current batch to the Database. It creates a stop inside the transaction.

http://msdn.microsoft.com/en-us/library/ms188037.aspx

(Update, thanks for the comments):
GO is a statement intended for the Management studio as far as I know, maybe to other tools as well.

桃扇骨 2024-08-29 06:11:45

分号分隔查询,GO 命令分隔批次。 (另外,GO 不是 T-SQL 命令,它是 sqlcmd 和 osql 实用程序以及 Management Studio 识别的命令。)

您不能在存储过程中使用 GO。如果您愿意尝试,过程的定义将在此结束,其余部分将是一个单独的批次。

局部变量具有批处理的范围,因此在 GO 命令之后,您不能使用在 GO 命令之前声明的局部变量:

declare @test int

set @test = 42

GO

select @Test -- causes an error message as @Test is undefined

The semicolon separates queries, the GO command separates batches. (Also GO is not a T-SQL command, it's a command recognised by the sqlcmd and osql utilities and Management Studio.)

You can't use GO inside a stored procedure. If you would try, the definition of the procedure will end there, and the rest will be a separate batch.

A local variable has the scope of the batch, so after a GO command you can't use local variables declared before the GO command:

declare @test int

set @test = 42

GO

select @Test -- causes an error message as @Test is undefined
风筝在阴天搁浅。 2024-08-29 06:11:45

我知道这个线程很旧,但我认为这些其他用途/差异对于像我这样的关于 GO 的其他搜索可能很方便。

  1. GO 之后的任何内容都不会出现在您的存储过程中,因为 GO 将执行 CREATE/ALTER PROCEDURE 命令。例如,如果您运行此...

    创建过程 X 作为
    选择 1 作为 X

    SELECT 2 As X

然后运行它后,您返回编辑过程,您会发现那里只有 SELECT 1 As X ,因为 GO 创建了存储过程并且之后的任何事情都被假定为您正在做的下一件事,而不是存储过程的一部分。

  1. 我很惊讶我没有看到太多提及这一点,但批处理分隔符不仅特定于您正在查询的程序,而且在 SSMS 的情况下,它实际上是用户可编辑的!如果我进入设置并将批次分隔符从 GO 更改为 XX,那么在我的 SSMS 副本中,XX 执行批次而不是 GO。那么如果我尝试执行包含 GO 的存储过程会发生什么?

GO 视为告诉 SSMS 将其上方的任何内容发送到服务器执行的一种方式。服务器永远不会收到 GO,因为它只是标记您希望 SSMS 发送到服务器的一批命令的结束。
如果您需要控制存储过程中的执行流,则可以使用 BEGIN TRANSACTIONCOMMIT TRANSACTION 来实现此目的,并且这些在存储过程中是允许的。

I know this thread is old but I thought these other uses/differences might be handy for other searches like myself regarding GO.

  1. Anything after the GO will not wind up in your sproc because the GO will execute the CREATE/ALTER PROCEDURE command. For example, if you run this...

    CREATE PROCEDURE X AS
    SELECT 1 As X
    GO
    SELECT 2 As X

Then after running it you go back in to edit the procedure you will find that only the SELECT 1 As X is in there because the GO created the sproc and anything after it is assumed to be the next thing you are doing and not part of the sproc.

  1. I'm surprised I haven't seen this mentioned much out there but the batch separator is not only specific to the program you are querying with but in the case of SSMS it is actually user editable! If I went into the settings and changed the batch separator from GO to XX then in my copy of SSMS, XX executes the batch not GO. So what would happen if I tried to execute a stored procedure that contained GO?

Think of GO as a way of telling SSMS to send whatever is above it to the server for execution. The server never receives the GO as that is just there to mark the end of a batch of command you want SSMS to send to the server.
If you have a scenario where you need to control execution flow in your stored procedure then you can use BEGIN TRANSACTION and COMMIT TRANSACTION for that and those are allowed in stored procedures.

娇柔作态 2024-08-29 06:11:45

GO 不是对服务器的命令,它是 MS 提供的大多数客户端工具的默认批处理分隔符。当客户端工具自己在新行上遇到“GO”时,它会将迄今为止积累的所有命令发送到服务器,然后重新开始。

这意味着在一个批次中声明的任何变量在后续批次中都不可用。这也意味着不能在“GO”命令周围放置多行注释 - 因为服务器将看到第一批,并看到未终止的注释。

GO is not a command to the server, it's the default batch separator for most of the client tools the MS supply. When the client tool encounters "GO" on a new line by itself, it sends whatever commands it has accumulated thus far to the server, and then starts over anew.

What this means is that any variables declared in one batch are not available in subsequent batches. And it also means that multi-line comments can't be placed around a "GO" command - because the server will see the first batch, and see an unterminated comment.

2024-08-29 06:11:45

它标志着查询分析器中批次的结束
因此表示该批处理中存储过程定义的结束。
据我所知,它不属于 sp 的一部分。
GO 不是 TSQL 命令。

和 ;刚刚结束声明。

It marks the end of a batch in Query Analyzer and
therefore signals the end of a stored procedure definition in that batch.
As much as i know its not a part of sp.
GO isn't a TSQL command.

And ; just ends the statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文