I am building a query in which number of columns selected is not fixed so i am generating my query runtime and executing it in a stored procedure. But i am stuck now because i want to perform some operation from my query according to the number of records but i don't know how to get count of numbers of record from a Dynamic query where my query is like
DECLARE @RowCount AS numeric(18,0)
DECLARE @SQL AS VARCHAR(MAX)
Set @SQL ='Select Count(*) From ((Select Col1,Col2,Col3,Col4 From Table1)) as rowcount'
Exec(@SQL)
Set @RowCount = ????..
Can you please tell me how i got count of records from a dynaimc query
Count of number of records is independent of columns in SELECT list unless some condition is provided in WHERE clause.
Just do this
So if you want to store rowcount value returned use a table variable and insert into it
You can also retrieve value using OUTPUT variable