SQL IF/WHILE Blocks – SQL For Programmers
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
- MSSQL CREATE and DROP Database - SQL for Programmers - August 20th, 2008
- MSSQL CREATE TABLE - SQL For Programmers - 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
- SQL For Programmers – New Question - July 6th, 2009
- SQL SELECT CASE Instead of IIF - October 20th, 2009
Other Related Items:
Inside Microsoft SQL Server 2005 - T-SQL Querying - Reference Book (34125F)Item #: 34125F. Take a detailed look at the internal architecture of T-SQL - and unveil the power of set-based querying - with comprehensive reference... Read More >
Programming Visual Basic .NET and ADO.NET with SQLLearn how to program Visual Basic .NET and ADO.NET for Microsoft Access and SQL Server databases as you watch DVDs on your TV or laptop DVD player. Th... Read More >










[...] SQL IF/WHILE Blocks – SQL For Programmers (Dave M. Bush) [...]
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,
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;