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.
| 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) < 5SET @txtmsg = 'No Results'DECLARE csr CURSOR FOR SELECT PhoneNum FROM SMSRecipients WHERE CampaignName = 'Acquisition' and Deleted <> 1OPEN csrFETCH NEXT FROM csr INTO @PhoneNumWHILE @@FETCH_STATUS = 0BEGINEXEC usp_SendSms @fromtext = 'LBM', @tomobile = @PhoneNum, @msgbody =@txtmsgFETCH NEXT FROM csr INTO @PhoneNumENDCLOSE csrDEALLOCATE csrdrop table #Timesheetdrop table #timesheet2drop 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. |
 |
|
|
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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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 |
 |
|
|
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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|