Batches

A batch is a set of SQL statements submitted together and executed as a group. A script is often a series of batches submitted one after the other. A batch is compiled only once as a whole and is terminated by an end-of-batch signal. Because of this, certain restrictions on batch size exist.

These rules apply to batches:

Examples

A.    Multiple SELECTs

This example shows multiple selects in one batch. Two results sets will be returned.

SELECT COUNT(*) FROM titles
SELECT COUNT(*) FROM authors
B.    Create and Use an Object

This example shows a table creation, an insert, and then a select.

CREATE TABLE test
(
    column1         char(10)            NOT NULL, 
    column2         int                NULL
)
INSERT test
    VALUES ('hello', 598)
SELECT * FROM test
C.    INSERT with Constraints in Separate Batches

This example shows how batches that include table definition and inserts into the table should be written. First, create the table in a separate batch from the INSERT statements(s), and then place the INSERT(s) in a separate batch.

This INSERT fails because 1234 is not a valid publisher ID.

CREATE TABLE publishers
(
pub_id        char(4)            NOT NULL 
    CONSTRAINT UPKCL_pubind PRIMARY KEY CLUSTERED
    CHECK (pub_id in ('1389', '0736', '0877', '1622', '1756')
        OR pub_id like '99[0-9][0-9]'),
pub_name        varchar(40)        NULL,
city            varchar(20)        NULL,
state        char(2)            NULL,
country        varchar(30)        NULL
    DEFAULT    ('USA')
)
go

INSERT publishers (pub_id, pub_name) VALUES('1234', 'Publishers test')

See Also

CREATE DEFAULT CREATE TABLE
CREATE INDEX CREATE TRIGGER
CREATE PROCEDURE CREATE VIEW
CREATE RULE SET