Sql-Server触发器,根据条件匹配另一个表中的字段

Posted 沫丶灬沫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Sql-Server触发器,根据条件匹配另一个表中的字段相关的知识,希望对你有一定的参考价值。

USE [CDM]
GO
/****** Object:  Trigger [dbo].[UpdateAkisFlight]    Script Date: 2018/6/14 16:43:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [dbo].[UpdateAkisFlight]
   ON  [dbo].[FlightWarnTime]
   FOR INSERT,DELETE
AS 
BEGIN
if(exists(select 1 from inserted) and not exists(select 1 from deleted))
    begin
      declare @TwoCode nvarchar(50),@FlightNum  nvarchar(50),@ThreeCode  nvarchar(50),@Etd datetime,@FlightId  int,@Calsign  nvarchar(50),@ID int
       select @TwoCode=TwoCode,@FlightNum=FlightNumber,@Etd = Etd,@ID = Id from inserted 
       select @ThreeCode=Code3  from Company  with(nolock)  where Code2=@TwoCode
       if @ThreeCode is not null
           begin
             set @[email protected]+@FlightNum
             if @FlightNum is not null
                begin
                   update [dbo].[FlightWarnTime] set [email protected] where Id=@ID
                end
             if @Etd is not null
               begin 
                 select  @FlightId=FlightId from [AKIS].[dbo].[ValidFlight]  with(nolock)  where [email protected]  and  ABS(DATEDIFF(MINUTE,Etd,@Etd)) <=60  and DepAddrName=ZHCC
               end
             if @FlightId is not null
               begin      
                  update [dbo].[FlightWarnTime] set [email protected] where Id=@ID
               end
           end
    end
END

 

以上是关于Sql-Server触发器,根据条件匹配另一个表中的字段的主要内容,如果未能解决你的问题,请参考以下文章