When do I need to use Begin / End Blocks and the Go keyword in SQL Server?

后端 未结 6 1601
借酒劲吻你
借酒劲吻你 2020-11-29 18:08

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?

6条回答
  •  抹茶落季
    2020-11-29 18:14

    GO ends a batch, you would only very rarely need to use it in code. Be aware that if you use it in a stored proc, no code after the GO will be executed when you execute the proc.

    BEGIN and END are needed for any procedural type statements with multipe lines of code to process. You will need them for WHILE loops and cursors (which you will avoid if at all possible of course) and IF statements (well techincally you don't need them for an IF statment that only has one line of code, but it is easier to maintain the code if you always put them in after an IF). CASE statements also use an END but do not have a BEGIN.

提交回复
热议问题