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 |
bulump
Starting Member
6 Posts |
Posted - 2012-01-04 : 16:27:32
|
I'm new to writing complex sql queries with MSSQL. This sounds like an easy thing to do, but I'm finding it's more complex when putting it down on paper.Basically I need to do the following:Feed the result from one query into a variable:@var1 = 'select count(*) from table1'Then, based upon that result do the following:IF @var1 > 0BeginEXECUTE more SQLENDELSEPRINT "No data found"I'm having trouble putting the result from the parent query into a variable, vaildating that variable, then executing more sql based upon that value.TIA |
|
X002548
Not Just a Number
15586 Posts |
|
bulump
Starting Member
6 Posts |
Posted - 2012-01-04 : 17:03:57
|
quote: Originally posted by X002548 oyeIF EXISTS (SELECT * FROM table1) ...more sql Brett
DOH! Riiiiight.....oops!so if I do:IF EXISTS ( Select count(*) from table1)sql....then it works, however, what if I want to throw another IF stmt in there and goIF EXISTS ( Select count(*) from table1)IF count(*) > 1 THEN....ELSE PRINT 'verbiage'I think that's my struggle... |
 |
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2012-01-04 : 19:57:38
|
oy vayselect @var1 = count(*) from table1IF @var1 > 1BeginEXECUTE more SQLENDELSEPRINT "No data found"=================================================Men shout to avoid listening to one another. -Miguel de Unamuno |
 |
|
|
|
|