to :Hopewell_Go(好的在后頭﹗希望更好﹗﹗)
多谢提醒,这个办法的确不行.
楼主有必要改写保存在表中的SQL字符串的内容,将
select sum(columnID) from outtableCol where columntype >129
改成
select @colValue = sum(columnID) from outtableCol where columntype >129
然后象楼上fcuandy或二楼写的那样,使用sp_executesql来获得返回值.
这个用法没有问题,下面的例子模仿楼主的情况,测试结果是正确的.请楼主检查一下表中保存的@colSql内容.注意:表中保存SQL字符串的列的数据类型必须为nvarchar类型.
请验证一下:
if object_id('tbTest') is not null
drop table tbTest
GO
----创建测试数据
create table tbTest(columnID int,columntype int)
insert tbTest
select 1 ,130 union all
select 2 ,130 union all
select 3 ,130
GO
declare @colSql nvarchar(4000)
declare @colValue int
----SQL字符串内容
set @colSql = 'select sum(columnID) from tbTest where columntype > 129'
----获得SQL返回值
EXEC @colValue = sp_executesql @colSql
----打印结果
print @colValue
declare @Col varchar(250)
declare @ColCN nvarchar(500)
declare @colValue int
declare @colSql nvarchar(4000)
SET @TID=1
select @ColSql=ObTableColSql
from obTable
where obTableID=@TID and obtableCol='a1'
SET @colvalue = EXEC(@ColSql) /*这样调用*/
print @colValue
declare @sum int,@b int
declare @sql nvarchar(4000)
set @sql = 'select @sum = sum(a) from t1 where id = 1'
EXEC sp_executesql @sql,N'@sum int output',@sum output
set @b = @sum /*将汇总结果赋给@b变量*/