有人在吗?请教一个小问题,如何把一个变量动态生成的语句结果再斌给另一个变量。

hbjmdx008 2006-10-18 09:46:12
如何把一个变量动态生成的语句结果再斌给另一个变量。

如果
set @a="select sum(a) from t1 where id=1"

exec(@a)
的结果
100

哪如何让这个结果斌给@b ?
我用
set @b=exec(@a)

set @b=(exec(@a))
都不行。
...全文
267 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
hbjmdx008 2006-10-19
  • 打赏
  • 举报
回复
是这样呀。看来只能改语句了。

并且,如果 传进去的语句不能太复杂是吧。
hellowork 2006-10-19
  • 打赏
  • 举报
回复
to :Hopewell_Go(好的在后頭﹗希望更好﹗﹗)
多谢提醒,这个办法的确不行.
楼主有必要改写保存在表中的SQL字符串的内容,将
select sum(columnID) from outtableCol where columntype >129
改成
select @colValue = sum(columnID) from outtableCol where columntype >129
然后象楼上fcuandy或二楼写的那样,使用sp_executesql来获得返回值.

如果不改SQL字符串的内容,可以参考Hopewell_Go的办法,将返回值插入到临时表中再从临时表中提取.
fcuandy 2006-10-19
  • 打赏
  • 举报
回复
create table tbTest(columnID int,columntype int)
insert tbTest
select 1 ,130 union all
select 2 ,130 union all
select 3 ,130

declare @colSql nvarchar(4000)
declare @colValue int

exec sp_executesql N'select @v=sum(columnID) from tbTest where columntype > 129', N'@v VARCHAR(100) OUTPUT',@colValue OUTPUT
select @colValue

drop table tbTest
Well 2006-10-19
  • 打赏
  • 举报
回复
楼上的方法不行。。
EXEC @colValue = sp_executesql @colSql
只返回的是0或1
Return Code Values
0 (success) or 1 (failure)

---
楼主是要复制给一个变量。。
hellowork 2006-10-18
  • 打赏
  • 举报
回复
这个用法没有问题,下面的例子模仿楼主的情况,测试结果是正确的.请楼主检查一下表中保存的@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

----清除测试环境
drop table tbTest

/*结果
6
*/
marco08 2006-10-18
  • 打赏
  • 举报
回复
学习
hbjmdx008 2006-10-18
  • 打赏
  • 举报
回复
上边的是就是@colSql 中的一个。。。和这样的类似的有很多。
hbjmdx008 2006-10-18
  • 打赏
  • 举报
回复
select sum(columnID) from outtableCol where columntype >129
hellowork 2006-10-18
  • 打赏
  • 举报
回复
@ColSql的内容是什么,请贴出一个来.
hbjmdx008 2006-10-18
  • 打赏
  • 举报
回复
啊呀,还是不对呀
正确的应是30442
结果是0呀,
(所影响的行数为 1 行)

0
hellowork 2006-10-18
  • 打赏
  • 举报
回复
抱歉,更正一下.下面这行是错误的:
SET @colvalue = EXEC(@ColSql)
更正如下:
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'
EXEC @colvalue = sp_executesql @ColSql /*修改此处,这样调用*/
print @colValue
hellowork 2006-10-18
  • 打赏
  • 举报
回复
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
hbjmdx008 2006-10-18
  • 打赏
  • 举报
回复
好像不行呀。
set @sql = 'select @sum = sum(a) from t1 where id = 1'
@sql 中一定要加上@sum = 吗
‘sum(a) from t1 where id = 1' 是从数据库中动态读出来的。。

原句是这样
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'
EXEC sp_executesql @colSql,N'@colValue int output',@colValue output
print @colValue
结果为空。
cxmcxm 2006-10-18
  • 打赏
  • 举报
回复
定义一临时表
将结果插入主临时表中,再读临时表即可
Well 2006-10-18
  • 打赏
  • 举报
回复
用虚拟表进行转换

create table #test
(
value varchar(100)
)
declare @a varchar(2000)
,@b varchar(100)
set @a='select gz from testA where gz=800'

insert into #test
exec(@a)
select @b=value from #test
select @b
drop table #test
hbjmdx008 2006-10-18
  • 打赏
  • 举报
回复
多谢楼上,试试先
hellowork 2006-10-18
  • 打赏
  • 举报
回复
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变量*/
hbjmdx008 2006-10-18
  • 打赏
  • 举报
回复
自已UP 一下。

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧