[求助]触发器定位不准确

xiaobo198294 2006-03-27 10:01:33
有两个表:
test1:
create table test1(
yy varchar(50) primary key
gg
)
create table temp1(
yy varchar(50) primary key
)
我建立一触发器,功能是:更新表test1的gg字段内容,找到test1更新记录
所对应的主键yy,在表temp1中,寻找是否存在此主键,如果存在,不进行更新
如果不存在,则插入此主键值到表temp1

触发器为:
REATE TRIGGER updateto ON [dbo].[test1]
FOR instead of update
AS
declare @t_yy as varchar(50)
declare @t_gg as varchar(50)
declare @t_row as varchar(50)
begin
select @t_yy=yy,@t_gg=gg from test1
delete from temp1 where yy=@t_yy
insert into temp1 select a.yy from test1 a where a.yy=@t_yy
end

但是,运行结果:
temp1表中只插入test1表的最后一个记录的主键值
请高手指点
...全文
133 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
惯看秋月春风 2006-03-27
  • 打赏
  • 举报
回复
Alter TRIGGER updateto ON [test1]
FOR update
AS
declare @t_yy varchar(50)
begin
select @t_yy=yy from inserted
if not exists (select * from temp1 where yy=@t_yy)
begin
insert temp1 values(@t_yy)
end
end
wangdehao 2006-03-27
  • 打赏
  • 举报
回复

alter TRIGGER updateto ON [test1]
for update
AS

if update(gg)
begin
insert temp1 select a.yy from inserted a where not exists(select * from temp1 where yy=a.yy)
end
xiaobo198294 2006-03-27
  • 打赏
  • 举报
回复
如果是分不够 问题解决了我在开个帖子加分 请高手帮忙!
xiaobo198294 2006-03-27
  • 打赏
  • 举报
回复
还是不行 TEST1更新的时候 只插入test1表里的最后一条记录 而我更新的不是TEST1表里的最后一条记录
lzhs 2006-03-27
  • 打赏
  • 举报
回复
如果僅實現你所說的功能,用下面的:

Alter TRIGGER updateto ON [test1]
FOR update
AS
declare @t_yy as varchar(50)

begin
select @t_yy=yy from inserted

delete from temp1 where yy=@t_yy

if @t_yy is Not Null
Begin
insert into temp1 values(@t_yy)
End
end
wangdehao 2006-03-27
  • 打赏
  • 举报
回复
sorry,上面的举例可能不能很好的说明问题,这样改:
truncate table temp1
truncate table temp2
go

alter TRIGGER tt ON [temp1]
for insert
AS
insert temp2 select top 1 * from inserted
---依次测试 top 2 ,top3 应该可以证明了
go

insert temp1 select '1'
union select '2'
union select '3'
select * from temp2

-----结果:
/***
y
1
***/
wangdehao 2006-03-27
  • 打赏
  • 举报
回复
楼上的对inserted表的理解有问题,不妨测试一下:
create table temp1(
yy varchar(50)
)

create table temp2(
yy varchar(50)
)
go
create TRIGGER tt ON [temp1]
for insert
AS
insert temp2 select * from inserted
go



insert temp1 select '1'
union select '2'
union select '3'

select * from temp2
-----结果:
/***
yy
1
2
3
***/

---初始化:
truncate table temp1
truncate table temp2
go
----触发器改一下:
alter TRIGGER tt ON [temp1]
for insert
AS
declare @t varchar(80)
select @t= yy from inserted
insert temp2 values (@t)
go

--同样的操作:
insert temp1 select '1'
union select '2'
union select '3'
----结果
/***
yy
3
***/

---2个结果并不同......









惯看秋月春风 2006-03-27
  • 打赏
  • 举报
回复
什么叫用游标逐条取INSERTED表里的数据?
INSERTED表是个系统临时表,只有一条记录。所以不会有定位上的问题,主要是触发器的类型设置问题。
if not exists (select * from temp1 where yy=@t_yy)
begin
insert temp1 values(@t_yy)
end
这段代码是当TEMP1中不存在更新数据时,插入主键值
wangdehao 2006-03-27
  • 打赏
  • 举报
回复
也可以用游标逐条来取inserted表里的数据来解决...
zhangbo198294 2006-03-27
  • 打赏
  • 举报
回复
学习哈!
zhangbo198294 2006-03-27
  • 打赏
  • 举报
回复
我怎么还是不行啊? temp1默认是没有任何数据的 只有TEST1进行了 UPDATA操作后才把相应的数据的主键插入到TEMP1表里 但现在每次插的都是TEST1最后1条数据
SQL_study 2006-03-27
  • 打赏
  • 举报
回复
学习~

34,587

社区成员

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

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