In SQL Server, when should you use GO and when should you use semi-colon ;?

后端 未结 5 1781
南方客
南方客 2020-12-04 10:51

I’ve always been confused with when I should use the GO keyword after commands and whether a semi-colon is required at the end of commands. What is the differences and why/w

相关标签:
5条回答
  • 2020-12-04 11:03

    The reason why you see so many GO's in Generated DDL scripts is because of the following rule about batches.

    CREATE DEFAULT, CREATE FUNCTION, CREATE PROCEDURE, CREATE RULE, CREATE TRIGGER, and CREATE VIEW statements cannot be combined with other statements in a batch. The CREATE statement must begin the batch. All other statements that follow in that batch will be interpreted as part of the definition of the first CREATE statement.

    One of the use cases for Generated DDL is to generate multiple objects in a single file. Because of this a DDL generator must be able to generate batches. As others have said the GO statement ends the batch.

    0 讨论(0)
  • 2020-12-04 11:03

    You should use a semi-colon to terminate every SQL statement. This is defined in the SQL Standards,

    Sure, more often than not SQL Server allows you to omit the statement terminator but why get into bad habits?

    As others have pointed out, the statement preceding a common table expression (CTE) must be terminated with a semi-colon. As a consequence, from folk who have not fully embraced the semi-colon terminator, we see this:

    ;WITH ...
    

    which I think looks really odd. I suppose it makes sense in an online forum when you can't tell the quality of code it will be pasted into.

    Additionally, a MERGE statement must be terminated by a semi-colon. Do you see a pattern here? These are a couple of the newer additions to TSQL which closely follow SQL Standards. Looks like the SQL Server team are going down the road of mandating the use of the semi-colon terminator.

    0 讨论(0)
  • 2020-12-04 11:15

    GO is a batch terminator, a semi-colon is a statement terminator.

    you will use GO when you want to have multiple create proc statements in 1 script because create proc has to be the first statement in a batch. If you use common table expressions then the statement before it needs to be terminated with a semi-colon

    0 讨论(0)
  • 2020-12-04 11:19

    GO

    Go is a batch separator. This means that everything in that batch is local to that particular batch.

    Any declarations of Variables, Table Variables, etc do not go across GO statements.

    #Temp tables are local to a connection, so they span across GO statements.

    Semicolon

    A Semicolon is a statement terminator. This is purely used to identify that a particular statement has ended.

    In most cases, the statement syntax itself is enough to determine the end of a statement.

    CTE's however, demand that the WITH is the first statement so you need a semicolon before the WITH.

    0 讨论(0)
  • 2020-12-04 11:23

    GO only relates to SSMS - it isn't actual Transact SQL, it just tells SSMS to send the SQL statements between each GO in individual batches sequentially.

    The ; is a SQL statement delimiter, but for the most part the engine can interpret where your statements are broken up.

    The main exception, and place where the ; is used most often is before a Common Table Expression Statement.

    0 讨论(0)
提交回复
热议问题