Queries, GO statements and transactions in T-SQL
How does SQL interpret a query structured like this -
[create some table and insert some data]
GO
[create another table and insert some data]
GO
My understanding is that it executes the statements sequentially, but does it also commit th开发者_StackOverflow社区e changes one by one (i.e., once it reaches the end of the first batch, it inserts into the table it created at the start of the batch), or does it wait until the entire query successfully executed?
The GO statements are used to separate batches of commands. If you do not specifically initiate a transaction, SQL will do it for you.
In the case you have described, a autocommit transaction will be wrapped around each of your "create some table" commands, and your "insert some data" commands.
Each statement is executed sequentially. You will end up with two separate batches of two commands each, wrapped with their own auto-committed transaction. 4 transactions total.
GO
is a statement used by several MS tools, it is NOT SQL.
It is used by these tools to signify the end of a batch of statements.
From MSDN: (thanks @datagod)
GO is not a Transact-SQL statement; it is a command recognized by the sqlcmd and osql utilities and SQL Server Management Studio Code editor.
精彩评论