.NET Answers

ASP.NET, HTML, CSS, Visual Studio, CSharp, VB.NET and other programming items of interest.
Subscribe
  • Home
  • About Me
  • Advertising
  • Click Here to Ask a question
    • Privacy Policy
  • Site Map

SQL IF/WHILE Blocks – SQL For Programmers

September 30, 2008 By: Dave

other-012 OK, all you programmer types.  Here’s where SQL starts getting interesting.  You have the basic concepts of declaring variables, setting up stored procedures, and doing a basic insert, update or delete statement.  And if we stopped here, you’d probably be able to do 80% of the work you need to do.

You’d also start to wonder why you should even bother.

So now we tackle the basics of conditional programming in SQL.

The good news is that the two commands you are going to use for conditional programming are pretty simple.  The bad news is, all the features and functionality you are used to having in just about any other programming language are gone.

So let’s take a look at your basic IF statement, which is simple in any language:

DECLARE @someString as VARCHAR
IF @someString='ABC'
   SELECT * FROM someTABLE

Obviously, I’m skipping some steps.  But let’s assume that @someString was actually passed in.  I just declared it so you could see that it is a VARCHAR.

This is about as simple as it gets.  If @someString equals “ABC” then go ahead and get the data.

The trick is, what if you want to do multiple lines?

In that case, you’ll need a BEGIN and END statement:

DECLARE @someString as VARCHAR
IF @someString='ABC'
BEGIN
   SELECT * FROM someTABLE
   SELECT * FROM someOtherTABLE
END

BEGIN and END are the opening curly brace and closing curly brace of the SQL programming world.

In the real world, we’d be storing data into variables.  But, I think we’re all programmers enough to be able to look at the general idea.

Where the syntax starts to start looking a bit messy is when you use ELSE:

DECLARE @someString as VARCHAR
IF @someString='ABC'
BEGIN
   SELECT * FROM someTABLE
   SELECT * FROM someOtherTABLE
END
ELSE
BEGIN
   SELECT * FROM elseTABLE
END

Even with a bit of indenting, it doesn’t clean it up much:

DECLARE @someString as VARCHAR
IF @someString='ABC'
  BEGIN
   SELECT * FROM someTABLE
   SELECT * FROM someOtherTABLE
  END
ELSE
  BEGIN
   SELECT * FROM elseTABLE
  END

But this is what we have to live with if we want to program in SQL.

Next time we look at SQL, we’ll take a look at WHILE loops.

 

Other post in SQL For Programmers
  • SQL For Developers - 9 Reasons to bother - August 13th, 2008
  • SQL for Programmers - CREATE/DROP Database - August 20th, 2008
  • SQL For Programmers - CREATE TABLE - August 22nd, 2008
  • SQL For Programmers - ALTERing the TABLE - September 1st, 2008
  • SQL For Programmers - Finding a String - September 3rd, 2008
  • SQL For Programmers - Finding IN a List - September 8th, 2008
  • SQL For Programmers - Stored Procedures (Better than LINQ) - September 10th, 2008
  • SQL For Programmers - Stored Procedure Basics - September 16th, 2008
  • Basic SQL Commands - SQL For Programmers - September 18th, 2008
  • SQL IF/WHILE Blocks - SQL For Programmers - September 30th, 2008
  • SQL WHILE - SQL For Programmers - October 2nd, 2008
  • Temporary Tables - SQL For Programmers - October 7th, 2008
  • SQL CURSOR - SQL For Programmers - October 13th, 2008
  • SQL CURSOR Performance - SQL For Programmers - October 22nd, 2008
  • Random in SQL - SQL For Programmers - November 4th, 2008
  • SQL - Filtering WHERE condition on two rows - November 26th, 2008
  • SQL - Transactions - April 15th, 2009

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Related Post

  • SQL – Filtering WHERE condition on two rows
  • Random in SQL – SQL For Programmers
  • SQL CURSOR Performance – SQL For Programmers
Bookmark to:

Add to Del.icio.us Add to digg Add to DotNetKicks Add to DZone Add to Facebook Add to Slashdot Add to Stumble Upon Add to Technorati
Hide Sites
Tags: blocks, if, mssql, sql, tsql

3 Responses to “ SQL IF/WHILE Blocks – SQL For Programmers ”

  1. # 1 Dew Drop – September 30, 2008 (Evening Edition) | Alvin Ashcraft's Morning Dew Says:
    September 30th, 2008 at 9:30 pm

    [...] SQL IF/WHILE Blocks – SQL For Programmers (Dave M. Bush) [...]

  2. # 2 sene Says:
    March 9th, 2009 at 10:46 am

    Hello,

    Could you please help me with this sql procedure? I always got an error message when executed, at the line with while. The error message is after the procedure:

    declare
    limit number;
    begin
    limit := 72011080;
    while (select SEQTITRES.nextval from dual) < limit
    loop
    select SEQTITRES.nextval from dual;
    end loop;
    end;

    ERROR MESSAGE:
    PLS-00103: Encountered the symbol “SELECT” when expecting one of the following:

    ( – + case mod new not null others
    avg
    count current exists max min prior sql stddev sum variance
    execute forall merge time timestamp interval date

    pipe
    <an alternatively-quoted string literal with character set sp

    Thanks in advance,

  3. # 3 Dave Says:
    March 9th, 2009 at 1:20 pm

    I fail to see how this wouldn’t cause an infite loop. But, your problem is that you are trying to subselect in a while, which as the error states, can’t be done.

    I have to assume you aren’t showing me the whole picture. It also isn’t t-sql syntax. So, using t-sql, you want something like:

    declare limit number;
    declare nextval number;
    begin
    SET @limit = 72011080;
    Select @nextval = SEQTITRES.nextval from dual;
    while @nextVal < @limit
    loop
    Select @nextval = SEQTITRES.nextval from dual;
    end loop;
    end;

← Two Interfaces. Same Method. Two meanings.
Automatic Implementation of Interfaces →
  • Search

  • Subscribe

    U COMMENT
    I FOLLOW

    Subscribe in a reader

    OR

    Subscribe via e-mail

    Enter your email address: 

    Delivered by FeedBurner

     

  • Follow Me

    • Twitter
    • FaceBook
    • Digg
    • StumbleUpon
    • Propeller
    • Delicious
    • Plaxo

     

  • Recent Posts

    • ASUS Eee PC 1005HA-PU1X-BK Black Netbook
    • jQuery – Date Picker
    • Using VB.NET From CSharp
    • iTextSharp – Adding Images
    • Hungarian Notation – Use What Works, Spit Out The Bones
    • Pre Order Windows 7
    • jQuery Dialog – With Validation Controls
    • iTextSharp – The easy way
    • Structure of my ASP.NET Web Applications
    • 35% Off Accronis True Image 2009 Home
    • VB.NET Hide Module Name
    • ASP.NET/VB.NET – Video Training
    • Does jQuery Make Us Lazy?
    • PDFs Using iTextSharp
    • Programming SEO – Ping



  • Advertise on this site through Lake Quincy Media
  • DotNetNuke Sponsor

     

    Most Valuable Blogger
  • Sponsor

  • Categories

    • Advanced CSharp
    • Advanced VB.NET
    • ASP.NET MVC
    • Did you know
    • DotNetNuke – Module Development
    • DotNetNuke – Skinning
    • internationalization
    • iTextSharp
    • jQuery
    • none
    • Seach Engine Optimization
    • Silverlight
    • SQL For Programmers
    • Twitter
    • winforms
  • Cloud

    .net ajax architecture asp.net book books containers csharp css dal dataset datasets dotnetnuke events gridview images internationalization internet explorer javascript jQuery json linq listview modules ms-sql MVC objectdatasource programming reflection seo Silverlight skinning sql testing tsql tutorial Twitter twitterizer vb.net video view Vista visual studio webservice WordPress
  • Archives

    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • October 2007
  • Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
    • Privacy Policy
  • Calendar

    September 2008
    S M T W T F S
    « Aug   Oct »
     123456
    78910111213
    14151617181920
    21222324252627
    282930  
  • Blogroll

    • Alvin Ashcraft’s Morning Dew
    • ASP.NET Consulting
    • Life Hacker
    • Remember Anything
    • The Price of Their Toys
    • Uncategorized Thought


.NET Answers © 2007 - 2008 All Rights Reserved.
Entries and Comments.