在触发器中加入表变量如何操作(sql server2000)

hanzhaorui 2008-04-09 10:03:09
DECLARE @TB nvarchar(50)
Declare @SQ nvarchar(50)
declare @aa as varchar(50)
SET @TB='table1'
SET @SQ='select '+@aa+'= num from '+@TB +' where id=1'
exec(@SQ)
insert into Table4(col,tt) VALUES(@aa,@SQ)
我想把表名table1放到一 变量@TB中,结果执行输入的全是空值,请问如何操作这种触发器.
...全文
140 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zl_c 2008-04-11
  • 打赏
  • 举报
回复
2楼正解
sp4 2008-04-11
  • 打赏
  • 举报
回复
换成3楼的写法

用sp_executesql
cxmcxm 2008-04-09
  • 打赏
  • 举报
回复
或者直接造出
insert into Table4
的动态语句来
cxmcxm 2008-04-09
  • 打赏
  • 举报
回复
动态语句中的值是不能赋给@aa的
可用以下方法实现
建一临时表
用insert into 临时表 exec(动态语句)
将动态语句的结果插入到临时表中
再select临时表
wzy_love_sly 2008-04-09
  • 打赏
  • 举报
回复
2
wzy_love_sly 2008-04-09
  • 打赏
  • 举报
回复
create table tb(id int,num varchar(20))
insert into tb select 1,2
insert into tb select 2,3

DECLARE @TB nvarchar(50)
Declare @SQ nvarchar(50)
declare @num varchar(50)
SET @TB='tb'
SET @SQ='select @num=num from '+@TB +' where id=1'
exec sp_executesql @SQ,N'@num varchar(50) output',@num output
select @num


变量用动态sql
fcuandy 2008-04-09
  • 打赏
  • 举报
回复
set @sq='select ' + @aa + '=num from ' + @tb + ' where id=1'

@aa未赋值,为null
因此 @sq得到null

exec(@sq) 即 exec一个null字串,未进行任何操作.
@aa,@sq均为null

insert tb4(col,tt) select @aa,@sq

所以插入的是null

不知道你想实现什么,还是说出来,想想给你怎么改.

34,871

社区成员

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

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