Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Using IF without a BEGIN and END

Author  Topic 

theboyholty
Posting Yak Master

226 Posts

Posted - 2011-11-11 : 11:44:47
If you use an IF statement in SQL code but don't use a BEGIN or and END, how does it know how much of the subsequent code to include?

I've come across a bit of code (below) in which there's an IF which only affects one line of code. There's no BEGIN or END and then the proc starts mucking about with cursors and all that kind of thing.
But the cursor runs, whether or not the IF condition is satisfied.
This doesn't seem logical to me, I would have expected it to only run the code AFTER the IF, on the basis that the condition was met, but it doesn't.
Call me a great big dimbo, but I just don't get it.

IF LEN(@txtmsg) < 5
SET @txtmsg = 'No Results'

DECLARE csr CURSOR FOR

SELECT PhoneNum
FROM SMSRecipients
WHERE CampaignName = 'Acquisition' and Deleted <> 1

OPEN csr
FETCH NEXT FROM csr INTO @PhoneNum

WHILE @@FETCH_STATUS = 0
BEGIN

EXEC usp_SendSms @fromtext = 'LBM', @tomobile = @PhoneNum, @msgbody =@txtmsg
FETCH NEXT FROM csr INTO @PhoneNum
END

CLOSE csr
DEALLOCATE csr

drop table #Timesheet
drop table #timesheet2
drop table #LeadCounts


---------------------------------------------------------------------------------
http://www.mannyroadend.co.uk A Bury FC supporters website and forum

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-11-11 : 11:48:32
>> If you use an IF statement in SQL code but don't use a BEGIN or and END, how does it know how much of the subsequent code to include?

the next statement only


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-11 : 11:56:16
thats the expected behaviour. it executes only following statement conditionally based on IF. all the other following statements would be executed regardless of condition check.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

theboyholty
Posting Yak Master

226 Posts

Posted - 2011-11-11 : 12:10:59
Ah right, well that makes sense. So I suppose I'd only need to use a BEGIN and END if I needed to execute a number of statements based on the IF condition.

Cheers chaps, have a good weekend.

---------------------------------------------------------------------------------
http://www.mannyroadend.co.uk A Bury FC supporters website and forum
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-11 : 12:19:16
quote:
Originally posted by theboyholty

Ah right, well that makes sense. So I suppose I'd only need to use a BEGIN and END if I needed to execute a number of statements based on the IF condition.

Cheers chaps, have a good weekend.

---------------------------------------------------------------------------------
http://www.mannyroadend.co.uk A Bury FC supporters website and forum


yep..exactly

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -