views:

17

answers:

1
DECLARE @T INT
EXEC('SELECT @T = count(user_id) FROM ( .. .. .. )')
PRINT @T

I guess the example is sufficient to explain what i am trying to do, i create dynamic query and want to access its count outside the exec statement.

I am using SQL 2000. How can this be achieved ?

+4  A: 

You'd need to use sp_executesql which allows you to pass parameters in and out. Main thing other than the solution below, is make sure you do really need dynamic SQL.

e.g.

DECLARE @SQL NVARCHAR(1000)
DECLARE @T INTEGER
SET @SQL = 'SELECT @T = COUNT(user_id) FROM ......'

EXECUTE sp_executesql @SQL, N'@T INTEGER OUT', @T OUT

PRINT @T
AdaTheDev
thanks, that really helped.
Anil Namde