How to set/get/return parameters from dynamic executed query
DECLARE @开发者_如何学PythonT 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 ?
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
精彩评论