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

时间:2022-03-08 19:02:44
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 from inserted) and not exists(select from deleted))
begin
declare @TwoCode nvarchar(),@FlightNum nvarchar(),@ThreeCode nvarchar(),@Etd datetime,@FlightId int,@Calsign nvarchar(),@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 @Calsign=@ThreeCode+@FlightNum
if @FlightNum is not null
begin
update [dbo].[FlightWarnTime] set Calsign=@Calsign where Id=@ID
end
if @Etd is not null
begin
select @FlightId=FlightId from [AKIS].[dbo].[ValidFlight] with(nolock) where Calsign=@Calsign and ABS(DATEDIFF(MINUTE,Etd,@Etd)) <= and DepAddrName='ZHCC'
end
if @FlightId is not null
begin
update [dbo].[FlightWarnTime] set AkisFlightId=@FlightId where Id=@ID
end
end
end
END