MS SQL Server
  Home arrow MS SQL Server arrow Page 2 - 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 
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

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 / 6
    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

    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!

    Using @@ROWCOUNT and TABLE Variables for Database Interactions with Transact-SQL - CASE as part of IF condition in T-SQL


    (Page 2 of 4 )

    In this section, I would like to combine the CASE structure with IF as part of the condition. Let us go through the following script:

    use northwind
    go

    declare @EmpSales numeric(12,2)
    declare @EmpID int
    set @EmpID = 2
    set @EmpSales = (select sum(unitprice * quantity)
                     from [order details]
                     where orderid in (select orderid from orders
                                       where employeeid=@EmpID))

    If (case
          when @EmpSales < 100000 then 'Bad Sales'
          when @EmpSales between 100000 and 200000 then 'Normal Sales'
        else 'Good Sales'
       end) = 'Good Sales'

        print 'Good keep it up'
    else
        print 'You may have to improve your sales. Currently it is only: ' + convert(varchar, @EmpSales)
    go

    The CASE structure in the above example returns one of thee values (bad sales, normal sales or good sales) based on the employee sales retrieved and stored in @EmpSales. The IF structure simply tests whether or not the value returned by the CASE structure is "Good Sales." If it is, it displays a positive message; otherwise it displays a different message.

    The above script may not be practical in most scenarios. But my intention is simply to introduce you to the point that a CASE structure can also be a part of an IF condition.

    Using @@ROWCOUNT to retrieve the number of rows affected

    When a DML statement (either INSERT, UPDATE, and DELETE) is issued to a database, the operation either will or will not be successful. For example, if the UPDATE statement doesn't update any information in the database, sometimes, it may not reflect back, giving an "Unsuccessful" result.

    This scenario will mostly occur when you provide a condition which would never be satisfied. Let us consider the following example:

    use northwind
    set nocount on
    go

    declare @EmpID int
    set @EmpID = 9999

    update Employees set FirstName = 'Jag'
    where EmployeeID = @EmpID

    print 'Successfully updated'
    go

    In the above script, I am issuing an UPDATE statement with a condition which would never be satisfied (an employeeid with 9999). But when you execute the above script, you will simply get the same message: "Successfully updated."

    The problem is that we need to test whether the latest DML command has updated the database or not. If it did update anything, then we only need to display that message. This is where @@ROWCOUNT comes in. It simply returns the number of rows affected by the latest DML command. Let us modify the above script as follows:

    use northwind
    set nocount on
    go

    declare @EmpID int
    set @EmpID = 9999

    update Employees set FirstName = 'Jag'
    where EmployeeID = @EmpID

    if @@ROWCOUNT = 0
       print 'No operation took place'
    else
       print 'Successfully updated'
    go

    Now, you can observe from the above script that I am using @@ROWCOUNT to retrieve the number of rows affected by the UPDATE statement. If no rows are affected, it returns 0 and thus we can display a better message to the user.

    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