Wednesday 25 January 2012

TRIGGER | SQL Server Interview Questions

1.What is Trigger? What is its use? 
Triggers are a special class of stored procedure defined to execute automatically when an UPDATE, INSERT, or DELETE statement is issued against a table or view. Triggers are powerful tools that sites can use to enforce their business rules automatically when data is modified.

2. What are the types of Triggers?
The CREATE TRIGGER statement can be defined with the FOR UPDATE, FOR INSERT, or FOR DELETE clauses to target a trigger to a specific class of data modification actions. When FOR UPDATE is specified, the IF UPDATE (column_name) clause can be used to target a trigger to updates affecting a particular column.
You can use the FOR clause to specify when a trigger is executed:
  •  AFTER (default) - The trigger executes after the statement that triggered it completes. If the statement fails with an error, such as a constraint violation or syntax error, the trigger is not executed. AFTER triggers cannot be specified for views.
  •  INSTEAD OF -The trigger executes in place of the triggering action. INSTEAD OF triggers can be specified on both tables and views. You can define only one INSTEAD OF trigger for each triggering action (INSERT, UPDATE, and DELETE). INSTEAD OF triggers can be used to perform enhance integrity checks on the data values supplied in INSERT and UPDATE statements. INSTEAD OF triggers also let you specify actions that allow views, which would normally not support updates, to be updatable.
An INSTEAD OF trigger can take actions such as:
  • Ignoring parts of a batch.
  • Not processing a part of a batch and logging the problem rows.
  • Taking an alternative action if an error condition is encountered.

3. What are the new kinds of triggers in sql 2000?
In SQL Server 6.5 you could define only 3 triggers per table, one for INSERT, one for UPDATE and one for DELETE. From SQL Server 7.0 onwards, this restriction is gone, and you could create multiple triggers per each action. But in 7.0 there's no way to control the order in which the triggers fire. In SQL Server 2000 you could specify which trigger fires first or fires last using sp_settriggerorder.
Till SQL Server 7.0, triggers fire only after the data modification operation happens. So in a way, they are called post triggers. But in SQL Server 2000 you could create pre triggers also.

4. When should one use "instead of Trigger"? Example?
CREATE TABLE BaseTable
(
PrimaryKey int IDENTITY(1,1),
Color nvarchar(10) NOT NULL,
Material nvarchar(10) NOT NULL,
ComputedCol AS (Color + Material)
)
GO

--Create a view that contains all columns from the base table.
CREATE VIEW InsteadView
AS SELECT PrimaryKey, Color, Material, ComputedCol
FROM BaseTable
GO

--Create an INSTEAD OF INSERT trigger on tthe view.
CREATE TRIGGER InsteadTrigger on InsteadView
INSTEAD OF INSERT
AS
BEGIN
--Build an INSERT statement ignoring inserrted.PrimaryKey and
--inserted.ComputedCol.
INSERT INTO BaseTable
SELECT Color, Material
FROM inserted
END
GO

-- can insert value to basetable by this insert into basetable(color,material) values ('red','abc')

-- insert into InsteadView(color,material)) values ('red','abc') can't do this.
-- It will give error "'PrimaryKey' iin table 'InsteadView' cannot be null."

-- can insert value through table by this<
insert into InsteadView values (1,'red','abc',1) --PrimaryKey, ComputedCol wont take values from here

5. Difference between trigger and stored procedure?
Trigger will get execute automatically when an UPDATE, INSERT, or DELETE statement is issued against a table or view.
We have to call stored procedure manually, or it can execute automatic when the SQL Server starts (You can use the sp_procoption system stored procedure to mark the stored procedure to automatic execution when the SQL Server will start.

6. The following trigger generates an e-mail whenever a new title is added.
CREATE TRIGGER reminder
ON titles
FOR INSERT
AS
EXEC master..xp_sendmail 'MaryM', 'New title, mention in the next report to distributors.'

7. Drawback of trigger? Its alternative solution?
Triggers are generally used to implement business rules, auditing. Triggers can also be used to extend the referential integrity checks, but wherever possible, use constraints for this purpose, instead of triggers, as constraints are much faster.