Beruflich Dokumente
Kultur Dokumente
First of all, let's create a sample database with some tables and
insert some sample data in those tables using the script below:
GO
USE KDMNN
GO
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[User_Master] (
) ON [PRIMARY]
GO
[UserID]
) ON [PRIMARY]
GO
CONSTRAINT [FK_User_Details_User_Master]
FOREIGN KEY
[UserID]
) REFERENCES [dbo].[User_Master] (
[UserID]
GO
INSERT INTO USER_MASTER(USERNAME, PASSWORD)
SELECT 'Murthy','Murthy'
SELECT
1,'Navneeth','Naik','navneeth@kdmnn.com' UNION
SELECT
2,'Amol','Kulkarni','amol@kdmnn.com' UNION
SELECT 3,'Anil','Bahirat','anil@kdmnn.com'
UNION
SELECT
4,'Murthy','Belluri','murthy@kdmnn.com'
AFTER Triggers
Using the below script, first we shall create a trigger on the table
USER_MASTER for the INSERT event of the table.
USE KDMNN
Go
CREATE TRIGGER trgInsert
ON User_Master
FOR INSERT
AS
GO
BEGIN TRANSACTION
VALUES('Damerla','Damerla')
IF @ERR = 0
BEGIN
ROLLBACK TRANSACTION
END
ELSE
BEGIN
COMMIT TRANSACTION
Output
(1 row(s) affected)
ROLLBACK TRANSACTION
ON User_Master
FOR INSERT
AS
BEGIN
END
GO
ON User_Master
FOR INSERT
AS
BEGIN
END
GO
BEGIN TRANSACTION
VALUES('Damerla','Damerla')
IF @ERR = 0
BEGIN
ROLLBACK TRANSACTION
END
ELSE
BEGIN
COMMIT TRANSACTION
Output
(1 row(s) affected)
ROLLBACK TRANSACTION
From the output we can conclude that when the user tries to
insert data in the table USER_MASTER, three triggers are
executed automatically. That is, you can write several AFTER
triggers on one table for each of the three triggering actions.
Note: If a table has multiple AFTER triggers, then you can specify
which trigger should be executed first and which trigger should be
executed last using the stored procedure sp_settriggerorder. All
the other triggers are in an undefined order which you cannot
control.
as
ON vwUserMaster
FOR INSERT
AS
BEGIN
END
GO
Output
AS
BEGIN
END
GO
SELECT
SUBSTRING(syscomments.text, 0, 26) AS
[Trigger Definition],
OBJECT_NAME(sysobjects.parent_obj) AS
[Table Name],
syscomments.encrypted AS [IsEncrpted]
FROM
WHERE
(sysobjects.xtype = 'TR')
Output
ON User_Details
FOR UPDATE
AS
If UPDATE(FName)
BEGIN
ROLLBACK TRANSACTION
END
else If UPDATE(LName)
BEGIN
ROLLBACK TRANSACTION
END
else If UPDATE(MName)
BEGIN
END
else If UPDATE(Email)
BEGIN
ROLLBACK TRANSACTION
END
GO
UPDATE User_Details
WHERE UserID = 1
Output
INSTEAD OF Triggers
A trigger which gets executed automatically in place of triggering
actions i.e., INSERT, DELETE and UPDATE is called an INSTEAD OF
trigger.
On User_Details
FOR INSERT
AS
BEGIN
END
VALUES(100, 'FName','LName','MName','test@test.com')
Output
On User_Details
INSTEAD OF INSERT
AS
BEGIN
END
VALUES(100, 'FName','LName','MName','test@test.com')
Output
(1 row(s) affected)
Even if the UserID 100 does not exists in the User_Master table,
the trigger gets gets executed automatically.
on vwUserMaster
INSTEAD OF INSERT
AS
begin
End
VALUES('Damerla','Venkat')
Output
(1 row(s) affected)
AS
SELECT
[User_Master].[Username],
[User_Master].[Password],
[User_Details].[FName],
[User_Details].[MName],
[User_Details].[LName],
[User_Details].[Email]
FROM
[User_Master], [User_Details]
WHERE
[User_Master].[UserID]=[User_Details].[UserID]
ON vwUser
INSTEAD OF INSERT
AS
BEGIN
@UserName = UserName,
@Password = Password,
@FName = FName,
@MName = MName,
@LName = LName,
@Email = Email
FROM INSERTED
VALUES(@UserName, @Password)
INSERT INTO
User_Details(UserID,FName,LName,MName,Email)
VALUES(@@Identity, @FName, @LName, @MName, @Email)
END
VALUES
('Dhananjay','Dhananjay','Dhananjay','Nagesh',NULL,
'Dhananjay@kdmnn.com'
Output
AFTER Trigger [trgInsert] – Trigger executed !!
(1 row(s) affected)
(1 row(s) affected)
A view or table can have only one INSTEAD OF trigger for each
INSERT, UPDATE and DELETE events.
We have seen that you can create any number of AFTER triggers
on the table for the same event, whereas you cannot do that with
INSTEAD OF triggers.
ON vwUserMaster
INSTEAD OF UPDATE
AS
BEGIN
END
INSTEAD OF UPDATE
AS
BEGIN
END
Output
From the output, it is clear that you cannot create two INSTEAD
OF triggers on the view/ table for the same event.
At last, how would you know what are the triggers associated
with the table and what type of the trigger it is? Whether AFTER
or INSTEAD OF?
Sp_helptrigger User_Master
Output
trgInsert dbo 0
0 1 1 0
trgInsert2 dbo 0 0
1 1 0
trgInsert3 dbo 0 0
1 1 0
trgEncrypted dbo 0 0
1 1 0