Sunteți pe pagina 1din 3

Post |

Interview FAQs |

Exclusive Questions |

Interview Home

What is trigger?
Posted by Raja on 4/12/2008 | Category: Sql Server Interview questions | Views: 11289

Answer:
Triggers allows us to execute a batch of SQL code when either an insert, update or
against a specific table.

delete command is executed

Triggers are special types of stored procedures that are defined to execute automatically in place of or after
data modifications. They can be executed automatically on the insert, delete and update operation.
| Alert Moderator
Bookmark It
< Previous : What is agile software development?
Next > : How do you retrieve username in case of Windows Au ...

Comments or Responses
Posted by: Muhsinathk on: 9/13/2012 | Points: 10
A trigger is a pl/sql block structure which is fired when a DML statements like Insert, Delete, Update is executed
on a database table. A trigger is triggered automatically when an associated DML statement is executed.
Syntax of Triggers
The Syntax for creating a trigger is:
CREATE [OR REPLACE ] TRIGGER trigger_name
{BEFORE | AFTER | INSTEAD OF }
{INSERT [OR] | UPDATE [OR] | DELETE}
[OF col_name]
ON table_name
[REFERENCING OLD AS o NEW AS n]
[FOR EACH ROW]
WHEN (condition)
BEGIN
--- sql statements
END;
SQL Server 2000 has many types of triggers:
1. After Trigger
2. Multiple After Triggers
3.Instead Of Triggers
4. Mixing Triggers Type
Posted by: Muhsinathk on: 9/13/2012 | Points: 10
Differences between storedprocedures and triggers
1. When you create a trigger you have to identify event and action of your trigger but when you create s.p you
don't identify event and action
2.Trigger is run automatically if the event is occured but s.p don't run automatically but you have to run it
manually
3. Within a trigger you can call specific s.p but within a sp you cannot call atrigger

4.Trigger execute implicitly whereas store procedure execute via procedure call from another block.
5.We can call a stored procedure from front end (.asp files, .aspx files, .ascx files etc.) but we can't call a trigger
from these files.
6. Stored procedure can take the input parameters, but we can't pass the parameters as an input to a trigger.
We can create a trigger to update the 'product_price_history' table when the price of the product is updated in the
'product' table.
1) Create the 'product' table and 'product_price_history' table
CREATE TABLE product_price_history
(product_id number(5),
product_name varchar2(32),
supplier_name varchar2(32),
unit_price number(7,2) );
CREATE TABLE product
(product_id number(5),
product_name varchar2(32),
supplier_name varchar2(32),
unit_price number(7,2) );
2) Create the price_history_trigger and execute it.
CREATE or REPLACE TRIGGER price_history_trigger
BEFORE UPDATE OF unit_price
ON product
FOR EACH ROW
BEGIN
INSERT INTO product_price_history
VALUES
(:old.product_id,
:old.product_name,
:old.supplier_name,
:old.unit_price);
END;
/
3) Lets update the price of a product.
UPDATE PRODUCT SET unit_price = 800 WHERE product_id = 100
Once the above update query is executed, the trigger fires and updates the 'product_price_history' table.
4)If you ROLLBACK the transaction before committing to the database, the data inserted to the table is also rolled
back.
Login to post response

More Interview Questions by Raja

How do you retrieve username in case of Windows Authentication?


Types of Configuration files and their difference?
Difference between Response.Expires and Response.ExpiresAbsolute?
How can you manage server-side state?
How can you manage client-side state?
Which is stateless
What provider ADO.NET use by default?
What is boxing and unboxing?

Latest Interview Questions

How to use trace in libraray classes (like BAL, DAL)?


What are session management techniques in .NET?
Is overriding of a function possible in the same class?
What is ViewState?
What data types do the asp:RangeValidator control support?
What are Web services?

S-ar putea să vă placă și