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

xiaobo198294 xiaobo198294 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
有两个表:
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表的最后一个记录的主键值
请高手指点
...全文

相关推荐

评论 12
收藏 已收藏
分享
返回顶部
xiaobo198294
MS-SQL Server 34,876 成员 254,635 内容