; 之间有什么区别?和 SQL Server 存储过程中的 GO?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
;
只是结束语句。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.
分号分隔查询,GO 命令分隔批次。 (另外,GO 不是 T-SQL 命令,它是 sqlcmd 和 osql 实用程序以及 Management Studio 识别的命令。)
您不能在存储过程中使用 GO。如果您愿意尝试,过程的定义将在此结束,其余部分将是一个单独的批次。
局部变量具有批处理的范围,因此在 GO 命令之后,您不能使用在 GO 命令之前声明的局部变量:
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:
我知道这个线程很旧,但我认为这些其他用途/差异对于像我这样的关于 GO 的其他搜索可能很方便。
GO
之后的任何内容都不会出现在您的存储过程中,因为GO
将执行CREATE/ALTER PROCEDURE
命令。例如,如果您运行此...创建过程 X 作为
选择 1 作为 X
去
SELECT 2 As X
然后运行它后,您返回编辑过程,您会发现那里只有
SELECT 1 As X
,因为GO
创建了存储过程并且之后的任何事情都被假定为您正在做的下一件事,而不是存储过程的一部分。将
GO
视为告诉 SSMS 将其上方的任何内容发送到服务器执行的一种方式。服务器永远不会收到GO
,因为它只是标记您希望 SSMS 发送到服务器的一批命令的结束。如果您需要控制存储过程中的执行流,则可以使用
BEGIN TRANSACTION
和COMMIT TRANSACTION
来实现此目的,并且这些在存储过程中是允许的。I know this thread is old but I thought these other uses/differences might be handy for other searches like myself regarding
GO
.Anything after the
GO
will not wind up in your sproc because theGO
will execute theCREATE/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 theGO
created the sproc and anything after it is assumed to be the next thing you are doing and not part of the sproc.Think of
GO
as a way of telling SSMS to send whatever is above it to the server for execution. The server never receives theGO
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
andCOMMIT TRANSACTION
for that and those are allowed in stored procedures.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.
它标志着查询分析器中批次的结束
因此表示该批处理中存储过程定义的结束。
据我所知,它不属于 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.