近有个项目需要新表和老表之间关联(Table1和Table2),不是整表关联,是列关联。比如,表Table1有列Column1,Column2,表Table2有列Column3和Column4,需要将列Column2和Column4关联,要能做到我在Table2中修改列Column4的数据,Table1中Column2自动更新和Column4中数据保持一致!
请问大大们这个怎么搞阿?我在SQL数据库里建立关联没效果阿呢!请帮忙啊,急用!跪求答案!
请问大大们这个怎么搞阿?我在SQL数据库里建立关联没效果阿呢!请帮忙啊,急用!跪求答案!
这里我发个例子,仅做参考!
create trigger buy_addnew
on dbo.buy --buy为商品采购表
for insert
as
update g set amount=(g.amount+i.amount) --新插入商品,商品数量自动增加
from goods as g inner join inserted as i
on g.goodsid=i.goodid
for update
as
if update(Column4)
begin
update a set Column2=Column4
from table1 a,inserted b
where a.id=b.id
end
go
for update
as
if update(Column4)
begin
update a set a.Column2=b.Column4
from table1 a,inserted b
where a.id=b.id
end
go
不过你要能根据表1的更新确定表2的对应数据行才行.
create trigger trig_name on table2
for update
as
if update(Column4)
begin
update a set a.Column2=b.Column4
from table1 a inner join inserted b on a.id=b.id
end
go
create trigger trig_name on table2
for update
as
if update(Column4)
begin
update a set a.Column2=b.Column4
from table1 a,inserted b
where a.id=b.id
end
go
where a.id=b.id这里是两表联系的关键,把id改成能传递联系的列就是了
id怎么改呢?
a.Column2=b.Column4?
没有语法错误了
但是我改表TABLE2的Column4列,TABLE1的Column2列没有变化啊!