MS SQL Server
  Home arrow MS SQL Server arrow 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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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: 4 stars4 stars4 stars4 stars4 stars / 14
    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


    (Page 1 of 4 )

    This is the third article in a series focusing on programming with Transact-SQL. In this article, I shall go a bit in-depth into CASE structure and introduce two new topics, @@ROWCOUNT and TABLE variables in Transact-SQL.

    The examples in this series were tested on Microsoft SQL Server 2005 on Windows Server 2003 Enterprise Edition (and Windows XP Professional Edition).  I didn't really test the examples with any other/previous editions because I am concentrating on Microsoft SQL Server 2005. Please understand that all the samples in this series are meant only for learning purposes. They may not be practical in some scenarios. If you have any problems with executing the code, please post in the discussion area.

    CASE structure with SELECT statements as conditions

    In my previous articles, I covered defining, initializing and using variables in scripts along with IF and CASE structures. In this section, I would like to introduce SELECT statements as part of CASE structure. Let us go through the following script:

    use northwind
    go

    declare @EmpSales numeric(12,2)
    declare @EmpID int
    declare @Status varchar(20)
    set @EmpID = 2
    set @EmpSales = (select sum(unitprice * quantity)
                     from [order details]
                     where orderid in (select orderid from orders
                                       where employeeid=@EmpID))
    set @Status = case
                      when @EmpSales < (select avg(unitprice * quantity)
                                     from [order details]) then 'Low sales'
                      else 'Good Sales'
                  end
    print @Status + ': ' + convert(varchar, @EmpSales)
    go

    From the above script, you can observe that I am calculating the entire sales of an employee (identified by the employee id as 2) and assigning the value to a variable named @EmpSales. I am using the same value to test a part of the CASE statement. But the value is checked against the average price of all orders, which is retrieved as part of the SELECT statement.

    I used only a simple SELECT in the above CASE structure. Depending on your needs, you can even work with sub-queries, joins etc. on both sides of the condition!

    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

    - Windows Server 2008 as a Workstation OS
    - An Overview of Windows Server 2008 R2
    - LINQ to MySQL, Oracle and PostgreSQL Provide...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT