请问这样的触发器能实现吗?数据库为sql2008
8 个解决方案
#1
你的意思是只要有如何操作,都要把整个表的数据,都复制到 feedetail_old
首先创建一个表feedetail_old
然后,创建一个触发器:
create trigger dbo.feedetail_xx
on feedetail
for insert,delete,update
as
insert into feedetail_old
select *,getdate()
from feedetail f
where not exists(select 1 from deleted d where d.id = f.id) and
not exists(select 1 from inserted i where i.id = f.id)
首先创建一个表feedetail_old
然后,创建一个触发器:
create trigger dbo.feedetail_xx
on feedetail
for insert,delete,update
as
insert into feedetail_old
select *,getdate()
from feedetail f
where not exists(select 1 from deleted d where d.id = f.id) and
not exists(select 1 from inserted i where i.id = f.id)
#2
是只复制当前修改的数据行,没修改的就复制。可能某行数据会进行多次update操作,那么在feedetail表中就会有多个修改记录
#3
修改一下:
create trigger dbo.feedetail_xx
on feedetail
for insert,delete,update
as
if exists(select 1 from inserted ) and exists(select 1 from deleted)
insert into feedetail_old
select *,getdate()
from feedetail f
where not exists(select 1 from inserted d where d.id = f.id)
union all
union all
select *,GETDATE() from deleted
else if exists(select 1 from inserted)
insert into feedetail_old
select *,getdate()
from feedetail f
where not exists(select 1 from deleted d where d.id = f.id)
else
insert into feedetail_old
select *,getdate()
from feedetail
union all
select *,GETDATE() from deleted
#4
你那不是备份。准确地说,是:自动归档历史数据。
写触发器时,你要知道底层是2个表:inserted、deleted。这2个表可以任你读取任意的列(字段)。
还有一个函数:updated,判断某个列(字段)的值是否被修改。
有了上述知识,实现你的功能应当不难。
写触发器时,你要知道底层是2个表:inserted、deleted。这2个表可以任你读取任意的列(字段)。
还有一个函数:updated,判断某个列(字段)的值是否被修改。
有了上述知识,实现你的功能应当不难。
#5
create trigger dbo.feedetail_xx
on feedetail
for insert,delete,update
as
if exists(select 1 from inserted ) and exists(select 1 from deleted)
insert into feedetail_old
select *,GETDATE() from deleted
#6
sql server没有before触发器
得用instead of触发器
得用instead of触发器
#7
这个没那么复杂,在楼上的基础上修改下。我的理解是,对价格进行修改时,就把该表中的数据放到feedetail_old中,以下代码仅供参考
create trigger t_dbo.feedetail_xx
on feedetail
for insert,delete,update
as
if not exists(select 1 from deleted )
insert into feedetail_old
select *,getdate()
from inserted
else
insert into feedetail_old
select *,getdate()
from deleted
#8
上述代码中没有判断是不是对价格进行操作。如果是要判断价格进行修改时,才进行如上操作的时候,可以使用if update(价格)来判断。
create trigger t_dbo.feedetail_xx
on feedetail
for insert,delete,update
as
begin
if not exists(select 1 from deleted ) --插入时进行操作
insert into feedetail_old
select *,getdate()
from inserted
else if not exists(select 1 from inserted ) --删除时进行操作
insert into feedetail_old
select *,getdate()
from deleted
else if update(价格)
insert into feedetail_old
select *,getdate()
from deleted
#1
你的意思是只要有如何操作,都要把整个表的数据,都复制到 feedetail_old
首先创建一个表feedetail_old
然后,创建一个触发器:
create trigger dbo.feedetail_xx
on feedetail
for insert,delete,update
as
insert into feedetail_old
select *,getdate()
from feedetail f
where not exists(select 1 from deleted d where d.id = f.id) and
not exists(select 1 from inserted i where i.id = f.id)
首先创建一个表feedetail_old
然后,创建一个触发器:
create trigger dbo.feedetail_xx
on feedetail
for insert,delete,update
as
insert into feedetail_old
select *,getdate()
from feedetail f
where not exists(select 1 from deleted d where d.id = f.id) and
not exists(select 1 from inserted i where i.id = f.id)
#2
是只复制当前修改的数据行,没修改的就复制。可能某行数据会进行多次update操作,那么在feedetail表中就会有多个修改记录
#3
修改一下:
create trigger dbo.feedetail_xx
on feedetail
for insert,delete,update
as
if exists(select 1 from inserted ) and exists(select 1 from deleted)
insert into feedetail_old
select *,getdate()
from feedetail f
where not exists(select 1 from inserted d where d.id = f.id)
union all
union all
select *,GETDATE() from deleted
else if exists(select 1 from inserted)
insert into feedetail_old
select *,getdate()
from feedetail f
where not exists(select 1 from deleted d where d.id = f.id)
else
insert into feedetail_old
select *,getdate()
from feedetail
union all
select *,GETDATE() from deleted
#4
你那不是备份。准确地说,是:自动归档历史数据。
写触发器时,你要知道底层是2个表:inserted、deleted。这2个表可以任你读取任意的列(字段)。
还有一个函数:updated,判断某个列(字段)的值是否被修改。
有了上述知识,实现你的功能应当不难。
写触发器时,你要知道底层是2个表:inserted、deleted。这2个表可以任你读取任意的列(字段)。
还有一个函数:updated,判断某个列(字段)的值是否被修改。
有了上述知识,实现你的功能应当不难。
#5
create trigger dbo.feedetail_xx
on feedetail
for insert,delete,update
as
if exists(select 1 from inserted ) and exists(select 1 from deleted)
insert into feedetail_old
select *,GETDATE() from deleted
#6
sql server没有before触发器
得用instead of触发器
得用instead of触发器
#7
这个没那么复杂,在楼上的基础上修改下。我的理解是,对价格进行修改时,就把该表中的数据放到feedetail_old中,以下代码仅供参考
create trigger t_dbo.feedetail_xx
on feedetail
for insert,delete,update
as
if not exists(select 1 from deleted )
insert into feedetail_old
select *,getdate()
from inserted
else
insert into feedetail_old
select *,getdate()
from deleted
#8
上述代码中没有判断是不是对价格进行操作。如果是要判断价格进行修改时,才进行如上操作的时候,可以使用if update(价格)来判断。
create trigger t_dbo.feedetail_xx
on feedetail
for insert,delete,update
as
begin
if not exists(select 1 from deleted ) --插入时进行操作
insert into feedetail_old
select *,getdate()
from inserted
else if not exists(select 1 from inserted ) --删除时进行操作
insert into feedetail_old
select *,getdate()
from deleted
else if update(价格)
insert into feedetail_old
select *,getdate()
from deleted