这个怎么写啊?

qaz1105836858 2009-07-27 01:45:25
当我修改table1的字段status=6的时候,就在另一张表table2中插入一条数据
table2包含有字段id,table2的id等于table1的id,我怎么把我修改的那一条的id找出来,插入到table2中啊?
...全文
108 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
qaz1105836858 2009-07-27
  • 打赏
  • 举报
回复
谢谢,windows和刘亦菲。- - 谢谢大家!
octwind 2009-07-27
  • 打赏
  • 举报
回复

CREATE TRIGGER reminder
ON table1
FOR UPDATE
AS
insert into table2 select id,... from deleted where status=6
  • 打赏
  • 举报
回复
刚才那个错的。这个没问题

create table a
(
id int primary key identity(1,1),
name varchar(10)
)

create table b
(
id int primary key identity(1,1),
aid int,
name varchar(10)
)
insert a
select 'a1' union all
select 'a2'

insert into b
select 1,'b01' union all
select 2,'b02'

drop trigger trig_a
create trigger trig_a
on a
for update
as
declare @id int
select @id=id from inserted
update b set name = 'ok' where aid = @id
go

update a set name = 'a3' where id = 1

select * from a
select * from b
/**
id name
----------- ----------
1 a3
2 a3

(所影响的行数为 2 行)

id aid name
----------- ----------- ----------
1 1 ok
2 2 b03

(所影响的行数为 2 行)
**/
fwacky 2009-07-27
  • 打赏
  • 举报
回复

drop table a
drop table b
create table a
(
id int primary key identity(1,1),
name varchar(50)
)

create table b
(
id int primary key identity(1,1),
aid int
)

insert a
select 'a1' union all
select 'a2'

CREATE TRIGGER reminder
ON a
FOR update
AS
insert into b
select id from a where id in(select top 1 id from deleted )
go

update a set name = 'qaz1105836858' where id = 2


octwind 2009-07-27
  • 打赏
  • 举报
回复

CREATE TRIGGER reminder
ON table1
FOR UPDATE
AS
insert into table2 select id,col,... from table1 where status=6
fwacky 2009-07-27
  • 打赏
  • 举报
回复


CREATE TRIGGER reminder
ON 表
FOR update
AS
insert into table2
select id from table1 where id in(select * from deleted)
go


  • 打赏
  • 举报
回复

create table a
(
id int primary key identity(1,1),
name varchar(10)
)

create table b
(
id int primary key identity(1,1),
aid int,
name varchar(10)
)
insert a
select 'a1' union all
select 'a2'

insert into b
select 1,'b01' union all
select 2,'b02'

create trigger trig_a
on a
for update
as
declare @id i
SELECT @id=IDENT_CURRENT('a')
update b set name = 'b03' where aid = @id
go

update a set name = 'a3' where id = 2

select * from a
select * from b

/**
id name
----------- ----------
1 a1
2 a3

(所影响的行数为 2 行)

id aid name
----------- ----------- ----------
1 1 b01
2 2 b03

(所影响的行数为 2 行)
**/



sql2000测试通过。
qaz1105836858 2009-07-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 supercrsky 的回复:]
SQL codeCREATETRIGGER reminderON 表FORupdateAS--sql语句go
[/Quote]

我只插入修改的这条数据的id,
insert into table2
select id from table1 where ??
这的条件怎么写啊??
xiaoxiangfei 2009-07-27
  • 打赏
  • 举报
回复
第一个问题是用触发器.
第二个问题是用 SELECT IDENT_CURRENT('TableName')来取得刚插入那条记录.如果是修改就得自己控制了.
qaz1105836858 2009-07-27
  • 打赏
  • 举报
回复
。。我写触发器了,但是我修改status这个字段后,插入的时候怎么只插入修改的这一条啊。

我把table2中所有的数据都插进去了。。。。
  • 打赏
  • 举报
回复

CREATE TRIGGER reminder
ON 表
FOR update
AS
--sql语句

go

--小F-- 2009-07-27
  • 打赏
  • 举报
回复
写个触发器
  • 打赏
  • 举报
回复
触发器。

34,593

社区成员

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

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