
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tgr_VisitorInfo_update]') and OBJECTPROPERTY(id, N'IsTrigger') = 1)
drop trigger [dbo].[tgr_VisitorInfo_update]
GO


Create trigger [dbo].[tgr_VisitorInfo_update] on [dbo].[Tcc_VisitorInfo]    
 for update as 	
 declare @visitorId int;	
 declare @begtime datetime; 
 declare @endtime datetime; 
 declare @carnumber varchar(20); 
 declare @username varchar(50); 
 declare @tels varchar(20); 
 declare @cause varchar(50); 
 declare @examinestate int; 
 declare @ordertime datetime; 
 declare @examinetime datetime; 
 declare @examineuser varchar(50); 
 declare @freetime int; 
 declare @releasestate int; 
 declare @ordermoney decimal(18, 0); 
 declare @instate int; 

 select @visitorId = visitorId
       ,@begtime = begtime 
       ,@endtime = endtime
       ,@carnumber = carnumber
       ,@username = username
       ,@tels = tels
       ,@cause = cause
       ,@examinestate = examinestate
       ,@ordertime = ordertime
       ,@examinetime = examinetime
       ,@examineuser = examineuser
       ,@freetime = freetime
       ,@releasestate = releasestate
       ,@ordermoney = ordermoney
       ,@instate = instate
        from inserted;

 if(@visitorId > 0 and @instate = 1 and @endtime > getdate()) 
 begin 
   select @instate = 0
   insert into Tcc_VisitorInfo values(@visitorId, @begtime, @endtime, @carnumber, @username, @tels, @cause
          , @examinestate, @ordertime, @examinetime, @examineuser, @freetime, @releasestate, @ordermoney, @instate) 
 end  
GO 
