两个表,一个A表,一个B表
A表列为BookID, Price
B表列为BookID, Number, TotalPrice
两表通过BookID连接,如何在SqlServer中设置自动使得
TotalPrice=Price*Number
------解决方案--------------------
这个应该要用的触发器吧 只要price更新或者插入 b表totalprice就要相应的随之更新和插入
------解决方案--------------------
- SQL code
create trigger tri_B_uon Bfor updateasif update(number)begin declare @number int,@id int select @id=id,@number=number from inserted--id是B表的主键 update t6 set [email protected],[email protected]*price from A where B.bookid=A.bookid and [email protected]end运行下,这个,然后你修一个number看看吧。
------解决方案--------------------
- SQL code
create trigger tri_B_ion Bfor insertasdeclare @id int @number intdeclare @number int,@id intselect @id=id,@number=number from inserted--id是B表的主键update B set [email protected]*price from A where B.bookid=A.bookid and [email protected]