ISAS On SQL Features like Trigger, Transaction,Batches, Stored Procedure

18
© 2012 2S Corporation. All rights reserved.

Transcript of ISAS On SQL Features like Trigger, Transaction,Batches, Stored Procedure

Page 1: ISAS On SQL Features like Trigger, Transaction,Batches, Stored Procedure

© 2012 2S Corporation. All rights reserved.

Page 2: ISAS On SQL Features like Trigger, Transaction,Batches, Stored Procedure

© 2012 2S Corporation. All rights reserved.

Trigger

Transaction

Batches

Stored Procedure

Some Sql Features

Page 3: ISAS On SQL Features like Trigger, Transaction,Batches, Stored Procedure

© 2012 2S Corporation. All rights reserved.

Trigger

What is Trigger?

A Trigger is a named database object which defines

some action that the database should take when

some databases related event occurs.

The trigger event can be any of the DML or DDL Statement:

INSERTUPDATEDELETE

CREATE TABLEALTER TABLE

DML DDL

Page 4: ISAS On SQL Features like Trigger, Transaction,Batches, Stored Procedure

© 2012 2S Corporation. All rights reserved.

The general syntax of CREATE TRIGGER is :

CREATE TRIGGER trigger_name trigger_time trigger_event

ON tbl_name FOR EACH ROW trigger_statement

[Trigger_statement have the statement that executes when the

trigger fires but if you want to execute multiple statement the you

have to use the BEGIN or END compound statement.]

Note:

The general syntax of DROP TRIGGER is :

DROP TRIGGER trigger_name

Page 5: ISAS On SQL Features like Trigger, Transaction,Batches, Stored Procedure

What is DML Trigger?

© 2012 2S Corporation. All rights reserved.

A DML Trigger is fired when data in the underlying table is

affected by DML statements, such as

INSERT,UPDATE,DELETE.

What is DDL Trigger?

A DDL trigger is fired to response to DDL statement,

such as CREATE TABLE or ALTER TABLE.

Depending on the way in which trigger are fired , they are

categorized as:

1. After Trigger 2. Nested Trigger

3. Instead of Trigger 4. Recursive Trigger

Page 6: ISAS On SQL Features like Trigger, Transaction,Batches, Stored Procedure

© 2012 2S Corporation. All rights reserved.

Managing Triggers

How can you manage triggers?

While managing triggers, you can perform the following

operations on a triggers:

1. Altering a trigger

2. Deleting a trigger

DROP TRIGGER trigger_name

ALTER TRIGGGER trigger_name

{FOR|AFTER}{event_type[,...n] |

DDL_DATABASE_LEVEL_EVENTS}

{AS

{sql_statement[...n]}

}

Page 7: ISAS On SQL Features like Trigger, Transaction,Batches, Stored Procedure

© 2012 2S Corporation. All rights reserved.

Transactions

What is Transaction?

A transaction can be define as a sequence performed

together as a single logical unit of work.

A Single unit of work must process the four properties called

ACID.

Atomicity

Consistency

Isolation

Durability

A

C

I

D

Page 8: ISAS On SQL Features like Trigger, Transaction,Batches, Stored Procedure

© 2012 2S Corporation. All rights reserved.

The SQL Server allows implementing transactions in the following

two ways:

1. Autocommit Transaction

2. Explicit Transaction

Explicit transaction are specified by using two statement:

1. BEGIN TRANSACTION

2. COMMIT TRANSACTION

For example:-

BEGIN TRAN myTran

Select * From Humanresource.Department

COMMIT TRAN myTran

Page 9: ISAS On SQL Features like Trigger, Transaction,Batches, Stored Procedure

© 2012 2S Corporation. All rights reserved.

Reverting Transaction

Client gets an error with “Try

Again” msg

Client still waiting

Transaction timed out

Client hit the button to place

the orders

12:00 AM 12:02 AM 12:05 AM 12:08 AM 12:11 AM 12:14 AM

At a time there is a possibility that due to a problem all the

statements of a transaction will not be successfully executed.

For example:-

Then we use for resolving this problem:-

ROLLBACK TRANSACTION:

ROLLBACK TRAN[SACTION] [transaction_name |

@tran_name_variable | savepoint_name |

@savepoint_variable]]

Page 10: ISAS On SQL Features like Trigger, Transaction,Batches, Stored Procedure

© 2012 2S Corporation. All rights reserved.

To maintain the Security & Concurrency issues in giving Multi-

user Environment we use 'LOCKS'.

Transactional Integrity

Need For Locking:

In the absence of Locking, problems may occur if more then

one transaction use the same data from a database at the same

time.

These problem Include:-

1. Lost Updates

2. Uncommitted dependency(Dirty Read)

3. Inconsistent Analysis

4. Phantom Reads

Page 11: ISAS On SQL Features like Trigger, Transaction,Batches, Stored Procedure

© 2012 2S Corporation. All rights reserved.

Controlling Locks

If you want to change the lock mode from a Normal shared lock to

an Exclusive lock you need use Isolation levels

1. READ UNCOMMITED

2. READ COMMITED

3. REPEATABLE READ

4. SNAPSHOT

5. SERIALIZABLE

Types of isolation levels are:

Deadlocks Condition:

A Deadlock is a situation where two or more transaction have

locks on separate objects, and each transaction wait for a lock on

the other object to be released.

Page 12: ISAS On SQL Features like Trigger, Transaction,Batches, Stored Procedure

© 2012 2S Corporation. All rights reserved.

Resolving Deadlocks:

To resolved the deadlock Situation we uses :-

1. Setting Deadlock Priority

SET DEADLOCK_PRIORITY{LOW|NORMAL|@deadlock_var}

2. Customizing Lock Timeout

SET LOCK_TIMEOUT [timeout_period]

3. Using sys.dm_exec_requests

SELECT * FROM sys.dm_exec_requests

Page 13: ISAS On SQL Features like Trigger, Transaction,Batches, Stored Procedure

© 2012 2S Corporation. All rights reserved.

Batches

What is Batches ?

A Batches is group of Sql statements submitted together to

the Sql Server for execution.

To create a batch, you can write multiple SQL statements

followed by the keyword GO at the end:-

<T-SQL Statement1>

<T-SQL Statement2>

<T-SQL Statement3>

......

GO

Note:- Go is the command that specifies the end of the batch

statement.

Page 14: ISAS On SQL Features like Trigger, Transaction,Batches, Stored Procedure

© 2012 2S Corporation. All rights reserved.

Using Variables with Batches

While creating batches, you might need to save value

temporarily during the execution time then you define a variable

by DECLARE statement.

For example:-

DECLARE @variable_name data_type

Guideline to Create Batches

You cannot combine statements, such as CREATE RULE,

CREATE TRIGGER etc, with other statements while

creating a batch.

You can use the EXECUTE statement in a batch when it is not

the 1st statement of the batch.

Page 15: ISAS On SQL Features like Trigger, Transaction,Batches, Stored Procedure

© 2012 2S Corporation. All rights reserved.

Stored ProceduresA stored Procedure is a Pre-Compiled object stored in the

database.

The syntax of the create Procedure Statement is:-

CREATE PROCEDURE proc_nameAS BEGINsql_statment1, sql_statement2END

Executing a Stored Procedure

A procedure can be executed by using the EXEC

PROCEDURE statement.

EXEC | EXECUTE PROCEDURE proc_name AS [{login|user} ='name']

Page 16: ISAS On SQL Features like Trigger, Transaction,Batches, Stored Procedure

© 2012 2S Corporation. All rights reserved.

Altering a Stored Procedure

A Stored Procedure can be modified by using the ALTER

PROCEDURE statement.

Syntax:-ALTER PROCEDURE proc_name

Dropping a Stored Procedure

You can Drop the Stored Procedure from the Database by

using DROP PROCEDURE statement.

Syntax:-

DROP PROCEDURE proc_name

Page 17: ISAS On SQL Features like Trigger, Transaction,Batches, Stored Procedure

© 2012 2S Corporation. All rights reserved.

Creating Parameterized Stored procedures

When you need to Execute Procedure for different values of a

variable that provided at runtime. for this, you can create

parameterized store procedure.

Syntax:-

CREATE PROC prcListEmployee @title char(50)

AS

BEGIN

PRINT 'List of Employees'

SELECT EmployeeID, Login, Title from

HumanResource.Employee WHERE Title=@title

END

EXECUTE prcListEmployee @title=‘tool designer’

Page 18: ISAS On SQL Features like Trigger, Transaction,Batches, Stored Procedure

© 2012 2S Corporation. All rights reserved.