--以ID字段为例 CREATE TRIGGER [A_Update] ON dbo.A FOR Update AS update B set B.ID=(select Id from inserted) update b set datetimecol=getdate() from a inner join inserted b on a.id=b.id
create trigger tri_A_update on A for update as begin insert B(col1,col2,...,coln) select col1,col2,...,getdate() from deleted end
复杂了 其实把B表对应的时间字段设置为getdate()就OK了
create trigger Test on A for update as begin insert B(B表中欲插入数据的列列表) select 移到B表的数据列列表,getdate() from deleted end
create trigger tt on dbo.a after update as if exists (select 1 from sys.columns where [object_id]= object_id(N'dbo.a')and [name]= N'gettime') begin insert into b(col1,col2,...coln) select a.col1,a.col2,...a.coln from table b as a update b set gettime = convert(varchar,getdate()) from a as a inner join insertd b on a.id = b.id end else begin alter table b add gettime varchar -- insert into b(col1,col2,...coln) select a.col1,a.col2,...a.coln from table b as a update b set gettime = convert(varchar,getdate()) from a as a inner join insertd b on a.id = b.id end
CREATE TRIGGER [A_Update] ON dbo.A
FOR Update
AS
update B set B.ID=(select Id from inserted)
update b
set datetimecol=getdate()
from a
inner join inserted b
on a.id=b.id
on A
for update
as
begin
insert B(col1,col2,...,coln) select col1,col2,...,getdate() from deleted
end
for update
as
begin
insert B(B表中欲插入数据的列列表)
select 移到B表的数据列列表,getdate()
from deleted
end
create trigger tt
on dbo.a
after update
as
if exists (select 1 from sys.columns where [object_id]= object_id(N'dbo.a')and [name]= N'gettime')
begin
insert into b(col1,col2,...coln) select a.col1,a.col2,...a.coln from table b as a
update b set gettime = convert(varchar,getdate()) from a as a inner join insertd b on a.id = b.id
end
else
begin
alter table b
add gettime varchar
--
insert into b(col1,col2,...coln) select a.col1,a.col2,...a.coln from table b as a
update b set gettime = convert(varchar,getdate()) from a as a inner join insertd b on a.id = b.id
end
执行出现如下错误,我两个表的格式是一样的,只是b表多了个时间列请问如何解决,不能在 'inserted' 表和 'deleted' 表中使用 text、ntext 或 image 列。