Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Test if returns values before return

$
0
0

In a stored procedure called by a VB.NET program, I build an SQL statement then use EXEC sp_executesql @SQL to run it.  I am selecting from one column based on certain criteria, but if this would return no values, I want to build the select statement from a second column and then return those values instead.  How can I test how many records an @SQL statement will return before returning it? 

A simplified version of my code is below.  If this returns records, I want to return these.  If If this returns no records, I want to replace ' WHERE V1 IN (SELECT MAX(V1)..." with 'WHERE V2 IN (SELECT MAX(V2)..."  and then return those results.  Any suggestions on how to best accomplish this?

SET @SQL = 'SELECT * FROM ' + @MyTable + ' WHERE V1 IN (SELECT MAX(V1) FROM ' + @MyTable + ' WHERE T1 = @T1 AND C1 = @C1)' EXEC sp_execute @SQL " N'@T1 nvarchar(255), @C1 nvarchar(255)', @T1, @C1"

RETURN



Gina


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>