Saturday, February 25, 2012

No. of records returned by a query.

Dear all
i want to run a query on sql server based on whether the no. of records returned by another query is 0 or >0. what will be the sql code for this.Assume your SELECT statement is called Q. Than you can write

if (select count(*) from (Q) q)>0
BEGIN
SELECT 'Your TRUE block'
END
ELSE
BEGIN
SELECT 'Your block for count(*) = 0'
END|||...or, if you have already run your select statement as part of your procedure, use this so you don't have to execute it a second time:

if @.@.ROWCOUNT > 0
BEGIN
SELECT 'Your TRUE block'
END
ELSE
BEGIN
SELECT 'Your block for count(*) = 0'
END

@.@.ROWCOUNT stores the number of records returned by the last executed statement. If you don't use the value immediately, you will need to store it in a variable because it will change with your next statement.

blindman

No comments:

Post a Comment