当前位置: 代码迷 >> Sql Server >> 当一表中A列插入时更新2表信息 当1表中B列插入时更新2表信息
  详细解决方案

当一表中A列插入时更新2表信息 当1表中B列插入时更新2表信息

热度:123   发布时间:2016-04-27 11:39:42.0
当1表中A列插入时更新2表信息 当1表中B列插入时更新2表信息
表1
 create table 临时卡
  (
卡号 int not null ,
进库时间 datetime not null ,
车库名称 varchar(30),
出库时间 datetime not null ,
消费金额 money not null
  )
表2
 create table 停车场信息
  (
名称 varchar(20) primary key,
车位总数 int,
已停车位 int,
预约车位 int,
剩余车位 int
  )
进库,出库分别更新停车场信息的已停和剩余


------解决方案--------------------
SQL code
CREATE trigger [trigger_1] on [dbo].[table1]  instead   of  update  as begin    if update(A) begin      更新 table2  end     if update(B) begin      更新 table2  end   end
------解决方案--------------------
探讨
老大,您回复真及时,解决了定给高分。
俺的CREATE trigger [trigger_1] on [dbo].[临时卡]
instead of update
as begin
if update(进库时间) begin
update 停车场信息 set 剩余车位=剩余车位-1
where 名称=(select 车库名称 from inserted)
……

------解决方案--------------------
SQL code
CREATE trigger [trigger_1] on [dbo].[临时卡]   instead of update   as begin     if update(进库时间) begin     update 停车场信息 set 剩余车位=剩余车位-1  where 名称= inserted.车库名称  update 停车场信息 set 已停车位=已停车位+1  where 名称=inserted.车库名称  end     if update(出库时间) begin         update 停车场信息 set 剩余车位=剩余车位+1  where 名称=inserted.车库名称  update 停车场信息 set 已停车位=已停车位-1  where 名称=inserted.车库名称  end   end
------解决方案--------------------
SQL code
CREATE trigger [trigger_1] on [dbo].[临时卡]   instead of update   as begin     if update(进库时间)  begin     update 停车场信息 set 剩余车位=剩余车位-1 , 已停车位=已停车位+1         where 名称= inserted.车库名称 end     if update(出库时间)  begin       update 停车场信息 set 剩余车位=剩余车位+1,已停车位=已停车位-1           where 名称=inserted.车库名称 end   end
  相关解决方案