触发器问题(高手帮忙)

MaDaHai 2006-11-22 03:30:41
create trigger Table1Update
ON Table1
after update
as
begin
DELETE FROM Table1 Where exists(select * from instered where instered.Sno = Table1.Sno)

INSERT INTO Table1 (Sno, col1, col2)
SELECT Sno, col1, col2 FROM inserted

end
go

這樣觸發的時候DELETE那個語句好象有問題,注釋了就可以
不注釋會出錯
請問怎麼回事
...全文
178 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
sgucxc0 2006-11-23
  • 打赏
  • 举报
回复
呵呵...
fuxiaoyang13 2006-11-23
  • 打赏
  • 举报
回复
instered=》》应是inserted
tx1icenhe 2006-11-22
  • 打赏
  • 举报
回复
正常应该:

create trigger Table1Update
ON Table1
after update
as
begin
update Table2
set col1=i.col1,col2=i.col2
from Table2 ,inserted i
where i.Sno = Table2.Sno
and

INSERT INTO Table2 (Sno, col1, col2)
SELECT Sno, col1, col2
FROM inserted i
where not exists (
select 1 from Table2
where sNo=i.Sno
)

end
go

tx1icenhe 2006-11-22
  • 打赏
  • 举报
回复
这样改:

create trigger Table1Update
ON Table1
after update
as
begin
DELETE FROM Table2 Where exists(select * from instered i where i.Sno = Table2.Sno)

INSERT INTO Table2 (Sno, col1, col2)
SELECT Sno, col1, col2 FROM inserted

end
go

MaDaHai 2006-11-22
  • 打赏
  • 举报
回复
放在前面insert into 的時候有主健重復的時候怎麼插入啊
中国风 2006-11-22
  • 打赏
  • 举报
回复
create trigger Table1Update
ON Table1
after update
as
begin
INSERT INTO Table1 (Sno, col1, col2)
SELECT Sno, col1, col2 FROM inserted
DELETE FROM Table1 Where exists(select * from instered where instered.Sno = Table1.Sno)



end
把insert into放在delete前面
MaDaHai 2006-11-22
  • 打赏
  • 举报
回复
sorry 寫錯了更正一下,Table1 and Table2 結構一致
create trigger Table1Update
ON Table1
after update
as
begin
DELETE FROM Table2 Where exists(select * from instered where instered.Sno = Table1.Sno)

INSERT INTO Table2 (Sno, col1, col2)
SELECT Sno, col1, col2 FROM inserted

end
go
tx1icenhe 2006-11-22
  • 打赏
  • 举报
回复
try:

DELETE FROM Table1 Where exists(select * from instered where instered.Sno = Table1.Sno)
-->

DELETE FROM Table1 Where exists(select * from instered where Sno = Table1.Sno)



不过从语句看,你的触发器还有问题,上面只是排除个语法错误而已



34,590

社区成员

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

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