SunQuest
 
       MS SQL Server
  Home arrow MS SQL Server arrow Page 3 - A Brief Introduction to Transact-SQL
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

A Brief Introduction to Transact-SQL
By: Barzan "Tony" Antal
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2007-12-18

    Table of Contents:
  • A Brief Introduction to Transact-SQL
  • Usage of Variables
  • Control of Flow Mechanisms
  • Looping Sequences

  • 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

    A Brief Introduction to Transact-SQL - Control of Flow Mechanisms


    (Page 3 of 4 )


    Control-of-Flow Mechanisms

    As I mentioned at the beginning of my article, one of the most outstanding enhancements that T-SQL brought to the traditional de facto standard SQL was the addition of the control-of-flow mechanism. Here we can enroll commands such as IF…END, BEGIN…END, BREAK, GOTO, CONTINUE, RETURN, WAITFOR, and WHILE.

    The way we use conditional statements hasn’t changed. However, thanks to the T-SQL enhancement we can add a block of code into IF statements. This also means that we can nest more than one IF statement. An IF conditional works based on the following methodology: it executes the followed statements if and only if the testing is true. When the condition isn’t met, the ELSE part is executed (if there is one).

    For each time you want to add a block of code, don’t forget to delimit these using the BEGIN and END commands. BEGIN…END groups the commands in between. Check out the following example.


    IF condition1

    BEGIN

    lots of commands

    END

    ELSE

    IF condition2

    BEGIN

    another pack of commands

    END

    ELSE

    BEGIN

    yet another batch of commands

    END


    I hope that the above example is intuitive. Here it is in plain English: if condition1 is met, then the lots of commands part is executed; if condition1 isn’t met, then the second condition2 is tested; if it’s validated as true, then another pack of commands is executed; however, if this also returns a false validation, then, obviously, yet another batch of commands is executed, since there aren’t any more conditions.

    Makes sense, doesn’t it? Great! Now let’s see how the CASE commands act.

    CASE is a command that handles a list of conditionals and returns the execution of one of them. There are two formats in which you can use the CASE command. The first one is the simple CASE that works on the basis of comparison. Once the condition is met, the following expressions are returned. The second type is the searched CASE that evaluates a list of Boolean expressions in order to determine the results.

    Check out the following “simple CASE” example. Can you guess what it does?


    DECLARE @stateshort nchar(2)

    DECLARE @statelong nchar(8)

    SET @stateshort = ’DS’

    SELECT @statelong =

    CASE @stateshort

    WHEN ’SC’ THEN ’SEO Chat’

    WHEN ’DS’ THEN ’Dev Shed’

    WHEN ’AF’ THEN ’SP Free’

    END


    Yes, that’s right! Indeed, the content of the stateshort variable is evaluated, which is "DS," and it is met on the second conditional line. As a result the statelong variable ends up receiving the "Dev Shed" value. Notice that we’ve set the variable statelong after SELECT, so we can “work with it” once one of the conditions are met.

    More MS SQL Server Articles
    More By Barzan "Tony" Antal


       · You've just finished reading the first part of my T-SQL series. Please don't forget...
     

    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 5 hosted by Hostway