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

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 / 23
    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
     
     
    ADVERTISEMENT


    How to Use Variables, IF and CASE in Database Interactions with TransactSQL - Replacing IF with CASE


    (Page 5 of 5 )

    CASE structure is very similar to IF structure (but may not replace it sometimes). The CASE structure can include any number of conditions using WHEN..THEN. The following is a sample 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 < 100000 then 'Bad Sales'
                 when @EmpSales between 100000 and 200000 then 'Normal Sales'
                 else 'Good Sales'
               end

    print @Status + ': ' + convert(varchar, @EmpSales)
    go

    In the above script, I used case as part of an expression. This facility is not available with the IF structure. The IF structure must be completely independent. According to the above script, if the WHEN condition is met the value ("Bad Sales" etc.) gets directly assigned to the variable @Status and finally gets displayed using the PRINT statement.

    In the next article, I shall concentrate a bit more on conditions, loops, etc. So, please check back frequently or sign up for a newsletter! Any bugs, errors, improvements etc., are highly appreciated at http://jagchat.spaces.live.com


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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





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