SQL Tutorials - 22.DML Triggers in SQL

Опубликовано: 29 Сентябрь 2024
на канале: Melvin King
119
5

In this tutorial, we will learn how to create a DML trigger and how many types of this type of trigger in SQL.

SCRIPT:

Create Trigger [dbo].[Insert_Flights] On [dbo].[Flights]
For Insert
As

Declare @FlightDateTime Date,
@FlightDepartureCity Varchar(50),
@FlightDestinationCity Varchar(50),
@OnTime Int,
@Audit_Note Varchar(50)

Select @FlightDateTime = i.[FlightDateTime] From inserted i;
Select @FlightDepartureCity = i.[FlightDepartureCity] From inserted i;
Select @FlightDestinationCity = i.[FlightDestinationCity] From inserted i;
Select @OnTime = i.[OnTime] From inserted i;
Select @Audit_Note = 'This records has been inserted to Flights table'



Insert Into [dbo].[Flights_Activities] (FlightDateTime, FlightDepartureCity,
FlightDestinationCity, OnTime, Audit_Time, [Audit Note] )
Values ( @FlightDateTime, @FlightDepartureCity, @FlightDestinationCity, @OnTime,
GETDATE(), @Audit_Note)


Print 'Insert_Flight has been execute automatically to table Flights_Activities'


Last but not the least, SUBSCRIBE to my YouTube channel to stay updated about the regularly uploaded new videos.

Linkedin :   / mohammed-rasool