SQL Server: What are batching statements (i.e. using “GO”) good for?

后端 未结 4 2029
無奈伤痛
無奈伤痛 2020-12-08 03:47

I know that in SQL Server GO is considered a batch separator.

My question is: What is the point of having a batch separator? What benef

4条回答
  •  甜味超标
    2020-12-08 04:29

    Like Martain said, statements such as CREATE PROCEDURE must be the only ones in a batch.

    For example, I use batch separators whenever I create stored procedures and add permissions to a certain user. If I left out the 'go' then I would end up with a stored procedure that grants rights every time it runs. This way I can write them at the same time and be sure that I'm not writing stored procedures that break when I call them. For example

    create procedure [procedurename]
    (parameters)
    as begin
    
    select prefname, lastname from people
    
    end
    
    go
    
    grant execute on [procedurename] to [username]
    

提交回复
热议问题