MS SQL Server
  Home arrow MS SQL Server arrow Page 3 - 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 
VeriSign Whitepapers 
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
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    How to Use Variables, IF and CASE in Database Interactions with TransactSQL - Introducing the IF statement in T-SQL


    (Page 3 of 5 )

    No programming language exists without the IF statement. It is a conditional control statement which executes a set of statements based on the condition provided by the user. Let us consider 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 @EmpSales > 200000
       print 'Good sales: ' + convert(varchar, @EmpSales)
    else
       print 'Normal sales: ' + convert(varchar, @EmpSales)
    go

    In the above script, the "IF" condition simply checks whether employee sales have crossed 200000 or not. If the sales value is more than 200K, it prints out the sales value with message "Good Sales." If the sales value is less than (or equal to) 200K, it prints out the sales value with the message "Normal Sales."

    If you would like to include more than one statement as part of the true/false parts, you need to embed them within a "begin..end" structure as follows:

    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 @EmpSales > 200000
       begin
       print 'Good sales: ' + convert(varchar, @EmpSales)
       print 'keep it up'
       end
    else
       begin
       print 'Normal sales: ' + convert(varchar, @EmpSales)
       print 'Try hard to achieve Good sales'
       end
    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...





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