当前位置: 代码迷 >> Sql Server >> sqlserver中跨表乘法计算,该如何解决
  详细解决方案

sqlserver中跨表乘法计算,该如何解决

热度:56   发布时间:2016-04-27 12:39:08.0
sqlserver中跨表乘法计算
两个表,一个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]
  相关解决方案