I want to get the @.x's value:
declare
@.sql varchar(8000),
@.p1 varchar(100),
@.p2 varchar(100),
@.x int
select @.p1 = 'lastwaittype',@.p2 = 'SLEEP_TASK'
set @.sql='
select @.x=count(*) from test1 where '+@.p1+'='+''''+@.p2+''''
EXEC (@.sql)
print(@.x)
But the error message:
Msg 137, Level 15, State 1, Line 2
Must declare the scalar variable "@.x".
There are some conditions:must use exec;do not use temp table or procodure.
Any help would be appreciated.luyan wrote:
> I want to get the @.x's value:
> declare
> @.sql varchar(8000),
> @.p1 varchar(100),
> @.p2 varchar(100),
> @.x int
> select @.p1 = 'lastwaittype',@.p2 = 'SLEEP_TASK'
> set @.sql='
> select @.x=count(*) from test1 where '+@.p1+'='+''''+@.p2+''''
> EXEC (@.sql)
> print(@.x)
> But the error message:
> Msg 137, Level 15, State 1, Line 2
> Must declare the scalar variable "@.x".
> There are some conditions:must use exec;do not use temp table or
> procodure. Any help would be appreciated.
Use sp_executesql instead. For example:
Declare @.Var1 int
Declare @.sql nvarchar(1000)
Declare @.x int
Set @.Var1 = 10
Set @.sql = N'Select @.x = count(*) From dbo.sysobjects Where id > @.Var1'
Exec sp_executesql @.sql, N'@.x int OUTPUT, @.Var1 int', @.x OUTPUT, @.Var1
Select @.x
David Gugick
Quest Software
www.quest.com
No comments:
Post a Comment