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

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


    (Page 1 of 5 )

    This is the second article in a series focused on programming with Transact-SQL. In this article, I shall cover a few of the tricks for using variables and finally touch on IF and CASE structures as part of 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 (as I am concentrating on Microsoft SQL Server 2005). If you have any problems with executing the code, please post in the discussion area.

    Using variables with the UPDATE command

    In my previous article, I introduced variables and their usage in T-SQL scripts. Now, I shall extend the concept of variables to the UPDATE statement. Let us consider the following script:

    use northwind
    go
    set nocount on
    go
    declare @LatestValue money

    update [Order Details]
       set UnitPrice = UnitPrice + 2.5
       where OrderID = 10248
       and ProductID = 11
    set @LatestValue = (select UnitPrice from [order details]
                        where OrderID = 10248
                        and ProductID = 11)
    print 'Updated Value: ' + convert(varchar,@LatestValue)
    go

    Whenever a DML statement is executed, the output includes the number of rows affected by the DML statement. To suppress that message we can use "set nocount on." According to the above script, I declared a variable, @LatestValue, which should contain the updated unit price for the respective order and product.

    In the above script, the UPDATE statement modifies (or increments) the unit price. As the UPDATE would not return any value, I used a separate SELECT statement to retrieve the updated value and finally assigned the same to the @LatestValue variable.

    Instead of using two separate statements (UPDATE and SELECT), we can achieve the same using a single UPDATE statement as follows:

    use northwind
    go
    set nocount on
    go
    declare @LatestValue money

    update [Order Details]
       set @LatestValue = UnitPrice = UnitPrice + 2.5
       where OrderID = 10248
       and ProductID = 11

    print 'Updated Value: ' + convert(varchar,@LatestValue)
    go

    In the above UPDATE statement, the modified unit price (unit price + 2.5) gets assigned to both a row (for the column UnitPrice) and a variable (@LatestValue) at the same time!

    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

    - 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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek