一个关于触发器的问题

mgslovedreamsgogo 2006-02-22 04:29:15
用SQL创建一个insert触发器
功能是插入一条记录的同时把这条记录中的一些字段插入另外一张表!??
请问该怎么写??
...全文
139 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
holdy 2006-02-22
  • 打赏
  • 举报
回复
update 就是把for insert改成for update,其他不变
mgslovedreamsgogo 2006-02-22
  • 打赏
  • 举报
回复
恩,可以了,那如果是update触发器呢???
dulei115 2006-02-22
  • 打赏
  • 举报
回复
你不要管那个“(所影响的行数为 1 行)”,看数据进去没有,下面的代码测试通过,数据添加进去了
-------------------------
if object_id('table1') is not null drop table table1
go
select 1 as col1, 'a' as col2, 'a' as col3 into table1

if object_id('table2') is not null drop table table2
go
select 1 as col1, 'a' as col2 into table2
go
if object_id('test') is not null drop trigger test
go
create trigger test on table1 for insert
as
insert into table2 select col1, col2 from inserted
go
insert into table1 values(2, 'b', 'b')
go
select * from table1
/*
col1 col2 col3
2 b b
1 a a
*/
select * from table2
/*
col1 col2
2 b
1 a
*/

drop trigger test
drop table table1
drop table table2
mgslovedreamsgogo 2006-02-22
  • 打赏
  • 举报
回复
不报错,但是

(所影响的行数为 1 行)

mgslovedreamsgogo 2006-02-22
  • 打赏
  • 举报
回复
dulei115 2006-02-22
  • 打赏
  • 举报
回复
userName和uid的类型是否相同
mgslovedreamsgogo 2006-02-22
  • 打赏
  • 举报
回复
不行呀
create trigger test on user1 for insert
AS
BEGIN
insert into user2(userName) select uid from inserted
end
现在我往user1里面插入一条数据,结果还是没有被插入到uesr2表中
-狙击手- 2006-02-22
  • 打赏
  • 举报
回复
create trigger dddd
on tablename
for insert
as
begin
insert tablenameother select col,... from inserted
end
dulei115 2006-02-22
  • 打赏
  • 举报
回复
create trigger testtrig on tablename1 for insert
as
insert tablename2 select col1, col2 from inserted

34,592

社区成员

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

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