关于SQL server中如果改了一个中字段name的某一个记录的值,怎样让另一个表中的name也自动改变??
两个表的name字段是相同的,怎样可以实现改一个另外一个也自动更改?触发器可以,应当怎样写?还有没有其它的方法??我想找个最简单实用的,谢谢!!!!触发器应该在对一个表进...
两个表的name字段是相同的,怎样可以实现改一个另外一个也自动更改?触发器可以,应当怎样写?还有没有其它的方法??我想找个最简单实用的,谢谢!!!!
触发器应该在对一个表进行insert、delete和update的时候都生效。 展开
触发器应该在对一个表进行insert、delete和update的时候都生效。 展开
2个回答
展开全部
CREATE TRIGGER TRIGGERNAME ON [dbo].[TABLENAME]
FOR UPDATE ,delete,insert
AS
declare @name_i varchar(100)
declare @name_d varchar(100)
select @name_i=name from inserted
select @name_d=name from deleted
if @name_i is null and @name_d is null
begin
return
end
if @name_i is null
begin
delete [表] where name = @name_d
end
if @name_d is null
begin
insert into [表] (name)
values(@name_i)
end
if @name_d is not null and @name_i is not null
begin
update [表] set name = @name_i where name= @name_d
end
go
FOR UPDATE ,delete,insert
AS
declare @name_i varchar(100)
declare @name_d varchar(100)
select @name_i=name from inserted
select @name_d=name from deleted
if @name_i is null and @name_d is null
begin
return
end
if @name_i is null
begin
delete [表] where name = @name_d
end
if @name_d is null
begin
insert into [表] (name)
values(@name_i)
end
if @name_d is not null and @name_i is not null
begin
update [表] set name = @name_i where name= @name_d
end
go
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询