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 2005 Forums
 Transact-SQL (2005)
 validating row count then executing more sql

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 > 0
Begin
EXECUTE more SQL
END
ELSE
PRINT "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

Posted - 2012-01-04 : 16:44:51
oye


IF EXISTS (SELECT * FROM table1)
...more sql





Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

bulump
Starting Member

6 Posts

Posted - 2012-01-04 : 17:03:57
quote:
Originally posted by X002548

oye


IF 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 go
IF EXISTS ( Select count(*) from table1)
IF count(*) > 1 THEN....

ELSE PRINT 'verbiage'

I think that's my struggle...
Go to Top of Page

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2012-01-04 : 19:57:38
oy vay

select @var1 = count(*) from table1

IF @var1 > 1
Begin
EXECUTE more SQL
END
ELSE
PRINT "No data found"



=================================================
Men shout to avoid listening to one another. -Miguel de Unamuno
Go to Top of Page
   

- Advertisement -