RE: Dynamic Query Result

RE: Dynamic Query Result

 

  

Hi Usman,

you can do this with sp_executesql

SET @nvSqlSt = ' SELECT @Count = ISNULL(COUNT(*),0) FROM '+ @Table + ' WHERE '+ @Condition
SET @nvParamList = '@Count int output'
EXEC SP_EXECUTESQL @nvSqlSt, @nvParamList, @Count OUTPUT

Regards,
Selva Balaji. B
"You must be the change, you wish to see in the world."

-----Original Message-----
From: Usman Farhat [mailto:Usman.[Email Address Removed] Wednesday, February 19, 2003 7:22 PM
To: LazyDBA.com Discussion
Subject: Dynamic Query Result


Hi everyone,

I have a dynamic sql string in which i want to calculate rows returned by the query by assigning it to a variable
but the string followed will obviously will not work as between a dynamic sql string we cant use variable enclosed
in ' ' marks , what can be possible solution for this sort of situation ..

SET @SQL = 'SELECT @Count = Count(*) FROM ' + @TABLENAME + ' WHERE ' + @COLUMNNAME +' LIKE '+
+ ' ''' + @SEARCHSTRING + '%'' '
exec(@SQL)

Is there any way to return the querey result to variable using some thing like this ?
exec @tmp = (@SQL)

For example following query returns some rows count and i want to take that value in some variable ,for
further processing.

SET @SQL = 'SELECT Count(*) FROM ' + @TABLENAME + ' WHERE ' + @COLUMNNAME +' LIKE '+
+ ' ''' + @SEARCHSTRING + '%'' '

---------------------------------------------------------------------
TO REPLY TO EVERBODY , PLEASE CLICK REPLY-ALL, NOT JUST REPLY
To unsubscribe, e-mail: mssqldba-[Email Address Removed] additional commands, e-mail: mssqldba-[Email Address Removed]MS Sql Server LazyDBA home page