MS SQL Server
  Home arrow MS SQL Server arrow Page 2 - How to Use Variables, IF and CASE in Datab...
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

How to Use Variables, IF and CASE in Database Interactions with TransactSQL
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 20
    2007-03-26

    Table of Contents:
  • How to Use Variables, IF and CASE in Database Interactions with TransactSQL
  • Generating serial numbers (or sequences) and cumulative sums using the UPDATE statement
  • Introducing the IF statement in T-SQL
  • Using WHERE conditions in the IF statement
  • Replacing IF with CASE

  • 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
     
    Iron Speed
     
    ADVERTISEMENT

    Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!

    How to Use Variables, IF and CASE in Database Interactions with TransactSQL - Generating serial numbers (or sequences) and cumulative sums using the UPDATE statement


    (Page 2 of 5 )

    Let us consider that I have a table with several rows. I would like to add a new column of the numeric type and fill it with serial numbers. You can achieve this by  using an UPDATE statement together with an auto-updatable variable.

    Before proceeding with the script, we need to create a table with existing rows and add a new column to fill in the serial numbers. Execute the following command, which creates a new table named "SampleCustomers" (with exactly the same structure and rows as is available in the Customers table) along with a new column SlNo (filled with default zeroes):

    select 0 SlNo, * into SampleCustomers from customers

    At this point, if you open the SampleCustomers table, it would contain a new column named SlNo (filled with zeroes) together with all the columns and rows from existing Customers table.

    Now, I would like to update all the rows in that table with serial numbers using an UPDATE statement. The following is the script that will achieve the same: 

    use northwind
    go
    set nocount on
    go
    declare @SlNo int
    set @SlNo = 0
    update SampleCustomers
       set @SlNo = SlNo = @SlNo + 1

    print 'Updated Successfully'
    go

    You can also achieve this using the IDENTITY column. But I would like to demonstrate the efficiency of variables together with the UPDATE statement.

    We can create not only sequences, but also running or cumulative sums and update them as part of the table itself. Let us now create another sample table which contains an Order wise price list and an extra column to store running sums (defaults with zeroes) as follows:

    select OrderID, Amt, 0 RunningSum into SampleOrders
    from
    (
    select orderid, sum(unitprice * quantity) amt
    from [order details]
    group by orderid
    )as a

    Now, we can update the RunningSum with values using the following script:

    use northwind
    go
    set nocount on
    go
    declare @sum int
    set @sum = 0
    update SampleOrders
       set @sum = RunningSum = @sum + Amt

    print 'Updated Successfully'
    go 

    More MS SQL Server Articles
    More By Jagadish Chaterjee


       · Hello guys,This is second in series focusing on the programming with...
       · I was able to retrieve value of a column which is not updated!Declare @Location...
       · Wow that is great. I'm going to try a couple of tricks and see what happens. ...
     

    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...

    Iron Speed




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