tags:

views:

135

answers:

3
+1  Q: 

What is a batch?

In Transact-SQL, a batch is a set of SQL statements submitted together and executed as a group, one after the other. Batches can be stored in command files.

Is an *.sql file containing several SQL statements considered a batch? What else do we consider a batch?

+4  A: 

Batches are not part of TSQL, they are a function of the SQL Management Studio Editor. So, for example, you cannot submit an ADO.NET command object with "GO" as part of your command text.

The batch marker - "GO" tells the query editor to submit the SQL up to that point before proceeding during execution.

It is possible to configure the query editor to use a different batch marker beside "GO", but this would just confuse the heck out of people.

Paul Keister
They are part of TSQL. You can send a batch with multiple commands in ADO.NET, too.
erikkallen
I believe you are mistaken. Here's an MSDN TSQL reference page on the GO command: http://msdn.microsoft.com/en-us/library/ms188037.aspx
Paul Keister
On second thought, I see what you mean. The batch definition in the original question is correct, and batches affect the execution of TSQL statements.
Paul Keister
+2  A: 

Is an *.sql file containing several sql statements considered a batch?

Sort of. A *.sql file can actually contain several batches. You delimit batches via the batch separator. You might need multiple batches in a file because some statements (especially certain "ALTER" commands) can only be executed once per batch. That can make thing like performing ALTERs in a loop tricky.

Another trick here is how you separate individual batches in a file/document. In 99% of Sql Server tools, the batch separator is "GO". However, this is something that is configurable by the tool, and therefore you will run into the odd person now and then who uses something else.

Joel Coehoorn
1) I’m not sure I understand why having batches is a good thing ( I realize certain statements can only be executed once per a batch, but if batches didn’t exist, then we wouldn’t have that problem ), thus why did Sql server development team thought that it would be a good idea to introduce batches?!2) Besides the fact that certain statements can only be executed once per a batch, are there any other reasons why we would put a bunch of statements into several different batches instead of putting them all into a single batch?
carewithl
Certain other statements (SET options) will apply until the end of the batch.
Joel Coehoorn
thanx for helping me
carewithl
+1  A: 

from Books online

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

A table cannot be changed and then the new columns referenced in the same batch.

From this you might infer that multiple batches are needed to ensure the database structure changes have taken effect before you try to use them.

HLGEM