MS SQL Server
  Home arrow MS SQL Server arrow Page 3 - Using @@ROWCOUNT and TABLE Variables for D...
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 
Moblin 
JMSL Numerical Library 
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

Using @@ROWCOUNT and TABLE Variables for Database Interactions with Transact-SQL
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 7
    2007-04-02

    Table of Contents:
  • Using @@ROWCOUNT and TABLE Variables for Database Interactions with Transact-SQL
  • CASE as part of IF condition in T-SQL
  • A brief introduction to TABLE variables in T-SQL programming
  • Filling a TABLE variable with existing rows

  • 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


    Using @@ROWCOUNT and TABLE Variables for Database Interactions with Transact-SQL - A brief introduction to TABLE variables in T-SQL programming


    (Page 3 of 4 )

    Up to now, we have seen variables of standard data types like numeric, varchar, and so on. Now we are going to concentrate on TABLE variables. A TABLE type of variable is very similar to a variable which can hold a table of data (in the form of rows and columns). These table variables would exist only during the execution of script and will not physically affect existing database structures. In other words, they are completely managed in memory (and sometimes in TempDB as well).

    A table variable must be defined with a structure (column specification) based on the rows it may contain. Let's take a look at a sample script which uses a TABLE variable. The following is the code:

    use northwind
    set nocount on
    go

    declare @t_emp TABLE
       (
         EmpID int,
         Ename varchar(30)
       )

    insert into @t_emp(EmpID, Ename)
    values (1001, 'Jagadish')
    insert into @t_emp(EmpID, Ename)
    values (1002, 'Chatarji')

    select * from @t_emp

    go

    Let us try to understand the above script line by line.

    declare @t_emp TABLE
      
    (
        
    EmpID int,
        
    Ename varchar(30)
       
    )

    The above statement simply defines a new variable named @t_emp which contains two columns (EmpID and Ename) of storage. This table variable exists only during the execution of the script and automatically vanishes (it is erased from memory) once the script execution is complete. Going further, we have the following:

    insert into @t_emp(EmpID, Ename)
    values (1001, 'Jagadish')
    insert into @t_emp(EmpID, Ename)
    values (1002, 'Chatarji')

    The above two are very similar to the traditional INSERT statements, but are focused on inserting into the @t_emp table variable. Going further, we have the following:

    select * from @t_emp

    Once all the rows are inserted into the table variable, I am simply displaying all the information back to the user in the form of a real table!

    Primary key and Identity columns in TABLE variables

    Just like database tables, we can enforce TABLE variables with Primary Key, Unique, Identity, NOT NULL etc. types of constraints. Please understand, however, that not all types of constraints will work with TABLE variables.

    Let us rewrite the previous script to enforce the "primary key" constraint as follows:

    use northwind
    set nocount on
    go

    declare @t_emp TABLE
       (
         EmpID int primary key,
         Ename varchar(30)
       )

    insert into @t_emp(EmpID, Ename)
    values (1001, 'Jagadish')
    insert into @t_emp(EmpID, Ename)
    values (1001, 'Chatarji')

    select * from @t_emp

    go

    The above script would result in an error with primary key violation, as I am trying to insert two rows with the same employee id.

    We can even include identity columns (also called auto number or surrogate key columns) as part of the TABLE variable as follows:

    use northwind
    set nocount on
    go

    declare @t_emp TABLE
       (
         ID int identity(1,1),
         EmpID int primary key,
         Ename varchar(30)
       )

    insert into @t_emp(EmpID, Ename)
    values (1001, 'Jagadish')
    insert into @t_emp(EmpID, Ename)
    values (1002, 'Chatarji')

    select * from @t_emp

    go

    The output for the above script would result in three columns, where the first column would contain sequentially generated values starting with 1 and incrementing by 1.

    More MS SQL Server Articles
    More By Jagadish Chaterjee


       · Hello guys,This is third in series focusing on programming with T-SQL. In this...
     

    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 2 hosted by Hostway