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

后端 未结 5 2259
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-18 18:16

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 p

5条回答
  •  难免孤独
    2021-02-18 18:47

    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.

提交回复
热议问题