日期:2014-05-16  浏览次数:20658 次

关于修改某个字段后调用触发器的语句
数据库里有2张表:

房间表:
房间代码   房间名称  状态
 101       标准间   入住
 102       双人间   入住


入住表:
房间代码   退房日期
 101       null   
 102      2014-3-8 


我现在要做个触发器,当“入住表”的退房日期被update日期后,则对应的“房间表”的状态变为“空闲”。

请帮忙,谢谢!  



------解决方案--------------------
建表语句:
create table 房间表(房间代码 int,   房间名称 varchar(20), 状态 varchar(20))

insert into 房间表
select  101       ,'标准间'   ,'入住' union all
select  102       ,'双人间'   ,'入住'


create table 入住表(房间代码 int, 退房日期 datetime)

insert into 入住表
select  101       ,null   union all
select  102      ,'2014-3-8' 
go


触发器:
create trigger dbo.trigger_update_入住表
on 入住表
after update
as

if UPDATE(退房日期) 
   update 房间表
   set 状态='空闲'
   from 房间表 t
   inner join inserted i
           on t.房间代码 = i.房间代码
   where i.退房日期 is not null

go



自动更新状态
update 入住表
set 退房日期=GETDATE()
where 房间代码=101


select *
from 房间表
where 房间代码=101
/*
房间代码 房间名称 状态
101 标准间 空闲
*/