|
 |
sql_language thread: stored proc syntax
Message #1 by dont worry <aspmailbox@y...> on Tue, 12 Feb 2002 09:33:56 -0800 (PST)
|
|
This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.
------_=_NextPart_001_01C1B416.09FFBE00
Content-Type: text/plain
1. I mean it will literally print 2 ie "2"
1a. It doesn't print the first because the IF statement is not satisfied. 1
= 0 will always return false, so only the second line prints.
2. The second code example prints out nothing because the IF block is not
satisfied. As I said above 1 will never equal 0.
Have a look at this for a less confusing example:
DECLARE @Test bit
SET @Test = 1
IF (@Test = 1) -- True
PRINT 1
PRINT 2
-- Prints out "1" then "2"
IF (@Test = 0) -- False
PRINT 1
PRINT 2
-- Prints out "2"
IF (@Test = 1) -- True
BEGIN
PRINT 1
PRINT 2
END
-- Prints out "1" then "2"
IF (@Test = 0) -- False
BEGIN
PRINT 1
PRINT 2
END
-- Prints out ""
The tricky bit in this script is if you look at the 2nd block you would
expect it to print out nothing. In fact it prints out "2". In my experience
problems are most commonly caused when you have something like this is a
proc:
-- Some Code
IF (<condition>)
INSERT INTO MyTable VALUES (1)
INSERT INTO MyTable VALUES (2)
-- More code
The problem is that "INSERT INTO MyTable VALUES (2)" will *always* be
executed
3. I generally use SQL Books Online as a reference.
4. Anytime.
regards
David Cameron
nOw.b2b
dcameron@i...
|
|
 |