How to Create Trigger in SQL

In this article I am going to explain how to create a trigger in SQL
  • 2093

Introduction

Triggers in SQL are a special type of procedure that are executed, fired or invoked automatically in response to an action query are executed on a view or table. We can set trigger on insertion, deletion and  updating or combination of these operation.

There are three type of trigger in SQL.

  • AFTER Trigger
  • INSTEAD OF Trigger
  • FOR Trigger

A Statement that create vendors table

create table vendors

 (

 vendorid int,

 vendorname varchar(15),

 vendorcity varchar(15),

 vendorstate varchar(15)

 )

Statements that insert data in vendors table

insert into vendors values (20,'vipendra','noida','up')

insert into vendors values (21,'deepak','lucknow','up')

insert into vendors values (22,'rahul','kanpur','up')

insert into vendors values (23,'malay','delhi','delhi')

insert into vendors values (24,'mayank','noida','up')

A Statement that is used to fetch data from vendors table

ven (2).jpg

A Statement that is used to create a trigger

create trigger ven_trig

on vendors

after insert,update

as

update vendor

set vendorstate = upper(vendorstate)

where vendorid IN (select vendorid from inserted)


© 2019 DotNetHeaven. All rights reserved.