◆◆◆◆抢分!如何写这个触发器?(很简单)◆◆◆◆

puffgeng 2008-05-09 11:30:57
我想在SQL SERVER 2000上写一个触发器,以前没有写过,故请教大家,需求如下:
有一个数据库DB和其中的两个表tblA、tblB,我想写一个监视tblA插入新记录的触发器,即:
USE DB
CREATE TRIGGER tblA_Insert
ON [tblA]
FOR INSERT

当tblA插入一条新记录时,如果新记录的fld1字段的前4位是“abcd”,则将此条记录insert到tblB表中去,并同时删除tblA表中的此条记录。
用伪码表述如下:
if left(fld1,4) = 'abcd' then
insert this record to tblB
delete this record from tblA
end if
这两个表的主键都是fldKey字段。

感谢大家帮助!
...全文
107 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
muzhenxing013 2008-05-09
  • 打赏
  • 举报
回复
CREATE TRIGGER tblA_Insert ON [tblA] FOR INSERT
as
begin
if exists(select 1 from inserted where len(fld1,4) = 'abcd'
begin
insert into tblb select * from inserted
delete from tbla where id = (select id from inserted)
end
skyzcl 2008-05-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 Limpire 的回复:]
SQL codeUSE DB
CREATE TRIGGER tblA_Insert
ON [tblA]
instead of insert
as
insert tblA select * from inserted where fld1 not like 'abcd%'
insert tblB select * from inserted where fld1 like 'abcd%'
go
[/Quote]
areswang 2008-05-09
  • 打赏
  • 举报
回复
create trigger test on tb1
instead of insert
as
begin
insert into tb2 select * from inserted
delete tb1 from tb1,inserted i where tb1.id = i.id
end
-- drop trigger test
Limpire 2008-05-09
  • 打赏
  • 举报
回复
USE DB
CREATE TRIGGER tblA_Insert
ON [tblA]
instead of insert
as
insert tblA select * from inserted where fld1 not like 'abcd%'
insert tblB select * from inserted where fld1 like 'abcd%'
go
Limpire 2008-05-09
  • 打赏
  • 举报
回复
USE DB
CREATE TRIGGER tblA_Insert
ON [tblA]
instead of insert
as
insert tblA select * from inserted where fld1 not like 'abcd%'
insert tblB sleect * from inserted where fld1 like 'abcd%'
go
dawugui 2008-05-09
  • 打赏
  • 举报
回复
CREATE TRIGGER tblA_Insert ON [tblA] FOR INSERT 
as
begin
if exists(select 1 from inserted where left(fld1,4) = 'abcd' )
begin
insert into tblb select * from inserted --如果字段不一样,自己对应改.
delete from tbla where id = (select id from inserted)--假设ID为关键字
end
end
dawugui 2008-05-09
  • 打赏
  • 举报
回复
CREATE TRIGGER tblA_Insert ON [tblA] FOR INSERT 
as
begin
if exists(select 1 from inserted where len(fld1,4) = 'abcd' )
begin
insert into tblb select * from inserted --如果字段不一样,自己对应改.
delete from tbla where id = (select id from inserted)--假设ID为关键字
end
end
laowan688 2008-05-09
  • 打赏
  • 举报
回复
CREATE TRIGGER tblA_Insert ON [tblA]
instead of insert
as
insert tblA select * from inserted where fld1 not like 'abcd%'
insert tblB select * from inserted where fld1 like 'abcd%'
go


bbcddc 2008-05-09
  • 打赏
  • 举报
回复
learn.
flairsky 2008-05-09
  • 打赏
  • 举报
回复
CREATE TRIGGER tblA_Insert ON [tblA] FOR INSERT 
as
begin
if exists(select 1 from inserted where len(fld1,4) = 'abcd' )
begin
insert into tblb select * from inserted --如果字段不一样,自己对应改.
delete from tbla where id = (select id from inserted)--假设ID为关键字
end
end
-狙击手- 2008-05-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 Limpire 的回复:]
SQL codeUSE DB
CREATE TRIGGER tblA_Insert
ON [tblA]
instead of insert
as
insert tblA select * from inserted where fld1 not like 'abcd%'
insert tblB select * from inserted where fld1 like 'abcd%'
go
[/Quote]
pt1314917 2008-05-09
  • 打赏
  • 举报
回复

CREATE TRIGGER tblA_Insert ON [tblA]
instead of insert
as
insert tblA select * from inserted where fld1 not like 'abcd%'
insert tblB select * from inserted where fld1 like 'abcd%'
go

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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