我什么时候需要在 SQL Server 中使用 Begin/End Blocks 和 Go 关键字? [英] When do I need to use Begin / End Blocks and the Go keyword in SQL Server?

查看:37
本文介绍了我什么时候需要在 SQL Server 中使用 Begin/End Blocks 和 Go 关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我何时何地需要在 SQL Server 中使用 beginend 块?
另外,Go 关键字究竟有什么作用?

Can someone tell me when and where I need to use begin and end blocks in SQL Server?
Also, what exactly does the Go keyword do?

推荐答案

GO 就像脚本的结尾.

GO is like the end of a script.

您可以有多个 CREATE TABLE 语句,以 GO 分隔.这是一种将脚本的一部分与另一部分隔离开来,但将其全部提交到一个块中的方法.

You could have multiple CREATE TABLE statements, separated by GO. It's a way of isolating one part of the script from another, but submitting it all in one block.


BEGIN 和 END 就像 C/++/#、Java 等中的 { 和 }

BEGIN and END are just like { and } in C/++/#, Java, etc.

他们绑定了一个逻辑代码块.我倾向于在存储过程的开始和结束时使用 BEGIN 和 END,但这并不是绝对必要的.需要的地方是 for 循环和 IF 语句等,您需要不止一步...

They bound a logical block of code. I tend to use BEGIN and END at the start and end of a stored procedure, but it's not strictly necessary there. Where it IS necessary is for loops, and IF statements, etc, where you need more then one step...

IF EXISTS (SELECT * FROM my_table WHERE id = @id)
BEGIN
   INSERT INTO Log SELECT @id, 'deleted'
   DELETE my_table WHERE id = @id
END

这篇关于我什么时候需要在 SQL Server 中使用 Begin/End Blocks 和 Go 关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆