Dynamic Query Result

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 + '%'' '
MS Sql Server LazyDBA home page