How to Use Query Batch in T-SQL

Query Batch is a group of one or more Transact-SQL statements sent at the same time from an application to SQL Server for execution
  • 3081

T-SQL (Transact SQL) is a proprietary SQL extension used by Microsoft SQL Server and Sybase SQL Server. T-SQL adds extensions for procedural programming. Transaction-SQL is an extended form of SQL that adds declared variables, transaction control, error and exception handling and row processing to SQL's existing functions. Microsoft SQL and Sybase both support T-SQL statements It is largely SQL-92 compliant, so if you're familiar with another vendor's flavor of SQL, you'll probably feel right at home with Transact-SQL.

Query Batch

A batch is a group of one or more Transact-SQL statements sent at the same time from an application to SQL Server for execution. The most important characteristic of a batch is that it is parsed and executed on the server as an undivided entity. In some cases, batches are created implicitly. SQL Server compiles the statements of a batch into a single executable unit, called an execution plan. The statements in the execution plan are then executed one at a time.

The given below example shows how to execute query batch:- 


1> create table workers(
2>     ID          int,
3>     name        nvarchar (20),
4>     salary      int,
5>     start_date  datetime,
6>     city        nvarchar (20),
7>     region      char (5))
8> GO

2> --Inserting Records in a Query Batch
5> GO
1> INSERT INTO workers (ID, Name) VALUES(01,"Ravi")
2> INSERT INTO workers (ID, Name) VALUES(02,"sapna")
3> INSERT INTO workers (ID, Name) VALUES(03,"Pankaj")
4> INSERT INTO workers (ID, Name) VALUES(04,"Udas")
5> GO

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

2> select * from workers
3> GO

   ID          name      salary      start_date       city      region
  -----     ---------- ----------- -------------  ----------  -------
   01         Ravi         NULL          NULL         NULL      NULL
   02         sapna       NULL          NULL         NULL      NULL
   03         Pankaj      NULL          NULL         NULL      NULL
   04         Udas        NULL          NULL         NULL      NULL

(3 rows affected)

Happy Learning


More Articles

© 2018 DotNetHeaven. All rights reserved.