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

后端 未结 5 2219
佛祖请我去吃肉
佛祖请我去吃肉 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:37

    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:

    declare @test int
    
    set @test = 42
    
    GO
    
    select @Test -- causes an error message as @Test is undefined
    

提交回复
热议问题