MS SQL Server
  Home arrow MS SQL Server arrow Composite Transact-SQL Constructs: Batches...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MS SQL SERVER

Composite Transact-SQL Constructs: Batches, Scripts, and Transactions
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 8
    2006-08-10

    Table of Contents:
  • Composite Transact-SQL Constructs: Batches, Scripts, and Transactions
  • Using Batches
  • Batches and Errors
  • DDL Batches

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Composite Transact-SQL Constructs: Batches, Scripts, and Transactions


    (Page 1 of 4 )

    If you want to learn more about Transact-SQL statements, this article gets you off to a good start. The first of a multi-part series, it covers batches. It is excerpted from chapter five of the book Microsoft SQL Server 2005 Stored Procedure Programming in T-SQL and .NET, written by Dejan Sunderic (McGraw-Hill/Osborne, 2006; ISBN: 0072262281).

    Transact-SQL (T-SQL) statements can be grouped and executed together in a variety of ways. They can be

    • Compiled as a part of a stored procedure, user-defined function, or trigger
    • Written and executed individually or in groups from client utilities in the form of batches
    • Grouped and stored in external script files that can be opened and executed from various client utilities
    • Grouped in transactions that succeed completely or fail completely

    This chapter discusses batches, scripts, and transactions.

    It is not necessary to run examples from the text against the Asset5 database, but if you do, you must first make sure that the database contains the following table by executing the following script against the Asset5 database:

    Create Table Part(PartId int identity,
                      Make varchar(50),
                      Model varchar(50),
                      Type varchar(50))

    This table is used to illustrate the concepts discussed in this chapter. Some of the changes are destructive, so existing tables such as Equipment will not be used, which may be needed for other purposes later.

    Batches

    A batch is a set of Transact-SQL statements that are sent to and executed by SQL Server as a single unit. 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. For example, if you execute a set of Transact-SQL statements from Query Analyzer, the program will treat that set as one batch and do so invisibly:

    Insert Into Part (Make, Model, Type)
    Values ('Toshiba', 'Portege 7010CT', 'notebook')
    Insert Into Part (Make, Model, Type)
    Values ('Toshiba', 'Portege 7020CT', 'notebook')

    Insert Into Part (Make, Model, Type)
    Values ('Toshiba', 'Portege 7030CT', 'notebook')

    Some tools, such as the Query window in Management Studio and SQLCMD in SQL Server 2005, and Query Analyzer, osql, and isql in earlier versions of SQL Server, use the Go command to divide Transact-SQL code into explicitly set batches. In the following example, the code for dropping a stored procedure is in one batch and the code for creating a new stored procedure is in another. The batch is explicitly created using the Go command.

    If Exists (Select * From sysobjects
               Where id = object_id(N'[dbo].[prPartList]')
               And OBJECTPROPERTY(id, N'IsProcedure') = 1)
         Drop Procedure [dbo].[prPartList]
    Go

    Create Procedure prPartList
    As
         Select * from Part
    Return 0
    Go

    In a Query window of Management Studio, you can highlight part of the code and execute it. Management Studio treats the selected piece of code as a batch and sends it to the server and ignores the rest of the code (see Figure 5-1).

    In other utilities and development environments, batches may be divided in some other manner. In ADO, OLE DB, ODBC, and DB-Library, each command string prepared for execution (in the respective object or function) is treated as one batch.

    NOTE


    In SQLCMD (and command line tools in earlier version of SQL Server), the Go command has a different function. It is also a signal to the tool to start executing the code entered so far.

    More MS SQL Server Articles
    More By McGraw-Hill/Osborne


       · This article is an excerpt from the book "Microsoft SQL Server 2005 Stored Procedure...
     

    Buy this book now. This article is excerpted from chapter five of the book Microsoft SQL Server 2005 Stored Procedure Programming in T-SQL and .NET, written by Dejan Sunderic (McGraw-Hill/Osborne, 2006; ISBN: 0072262281). Check it out today at your favorite bookstore. Buy this book now.

    MS SQL SERVER ARTICLES

    - Completing the Introduction to Transact-SQL
    - A Brief Introduction to Transact-SQL
    - Lookups and Blocking Bad Data
    - Field Validation Rules for Blocking Bad Data
    - Using Masks to Block Bad Data
    - Blocking Bad Data
    - Using @@ROWCOUNT and TABLE Variables for Dat...
    - How to Use Variables, IF and CASE in Databas...
    - Creating Important Aspects of Notification S...
    - Working wth Variables in Database Interactio...
    - Delving Deeper into Notification Services
    - Notification Services
    - Building a Multi-table Report with SQL 2005 ...
    - A Secure Way of Building Connection Strings
    - Transferring a Database Using the SSIS Desig...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway