动态查询SQL中变量的返回值

Posted on 2007-07-13 07:41  秋雨飘飘  阅读(409)  评论(0编辑  收藏  举报
1DECLARE @tbname sysname,@sql varchar(100)
SET @sql='SELECT '+@tbname+'=name FROM sysobjects where id=1'
EXEC(@sql)
SELECT @tbname


2declare @num int@sqls nvarchar(4000
set @sqls='select @a=count(*) from tableName ' 
exec sp_executesql @sqls,N'@a int output',@num output 

3)

   
Set @StrSQL = ''
   
Set @StrSQL = '
   set @a=
''''

   Select @a=@a + Cast(
' + @IDField + ' As varchar(20)) +'','''+ '
   From 
    
'+ @TableName +'
   Where
    ParentID = 
' + Cast(@NodeID As varchar(20))
   
--print @StrSQL
   Exec sp_executeSQL @StrSQL, N'@a varchar(4000) output',@cNodeIDStr out  
   
--print @cNodeIDStr

Copyright © 2024 秋雨飘飘
Powered by .NET 8.0 on Kubernetes