SlideShare a Scribd company logo
1 of 18
© 2012 2S Corporation. All rights reserved.
 Some Sql Features 
© 2012 2S Corporation. All rights reserved. 
Trigger 
Transaction 
Batches 
Stored Procedure
DML DDL 
© 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: 
INSERT 
UPDATE 
DELETE 
CREATE TABLE 
ALTER TABLE
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.] 
© 2012 2S Corporation. All rights reserved. 
Note: 
The general syntax of DROP TRIGGER is : 
DROP TRIGGER trigger_name
What is DML Trigger? 
A DML Trigger is fired when data in the underlying table is 
affected by DML statements, such as 
INSERT,UPDATE,DELETE. 
© 2012 2S Corporation. All rights reserved. 
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
ALTER TRIGGGER trigger_name 
{FOR|AFTER}{event_type[,...n] | 
DDL_DATABASE_LEVEL_EVENTS} 
{AS 
{sql_statement[...n]} 
} 
© 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
© 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
The SQL Server allows implementing transactions in the following 
two ways: 
© 2012 2S Corporation. All rights reserved. 
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
Reverting Transaction 
At a time there is a possibility that due to a problem all the 
statements of a transaction will not be successfully executed. 
Client gets an 
error with “Try 
Again” msg 
Client still 
waiting 
© 2012 2S Corporation. All rights reserved. 
Transaction 
timed out 
For example:- 
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 
Then we use for resolving this problem:- 
 ROLLBACK TRANSACTION: 
ROLLBACK TRAN[SACTION] [transaction_name | 
@tran_name_variable | savepoint_name | 
@savepoint_variable]]
Transactional Integrity 
To maintain the Security & Concurrency issues in giving Multi-user 
Environment we use 'LOCKS'. 
© 2012 2S Corporation. All rights reserved. 
 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
© 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 
Types of isolation levels are: 
1. READ UNCOMMITED 
2. READ COMMITED 
3. REPEATABLE READ 
4. SNAPSHOT 
5. SERIALIZABLE 
 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.
© 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
© 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.
 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. 
© 2012 2S Corporation. All rights reserved. 
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.
© 2012 2S Corporation. All rights reserved. 
 Stored Procedures 
A stored Procedure is a Pre-Compiled object stored in the 
database. 
The syntax of the create Procedure Statement is:- 
CREATE PROCEDURE proc_name 
AS 
BEGIN 
sql_statment1, sql_statement2 
END 
 Executing a Stored Procedure 
A procedure can be executed by using the EXEC 
PROCEDURE statement. 
EXEC | EXECUTE PROCEDURE proc_name AS [{login|user} ='name']
© 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
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. 
© 2012 2S Corporation. All rights reserved. 
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’
© 2012 2S Corporation. All rights reserved.

More Related Content

Viewers also liked (11)

User defined functions
User defined functionsUser defined functions
User defined functions
 
My sql presentation
My sql presentationMy sql presentation
My sql presentation
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
 
Sql presentation 1 by chandan
Sql presentation 1 by chandanSql presentation 1 by chandan
Sql presentation 1 by chandan
 
PL/SQL User-Defined Functions in the Read World
PL/SQL User-Defined Functions in the Read WorldPL/SQL User-Defined Functions in the Read World
PL/SQL User-Defined Functions in the Read World
 
user defined function
user defined functionuser defined function
user defined function
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
Trigger
TriggerTrigger
Trigger
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
SQL Tutorial - Basic Commands
SQL Tutorial - Basic CommandsSQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands
 
Sql ppt
Sql pptSql ppt
Sql ppt
 

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

Intro to tsql unit 12
Intro to tsql   unit 12Intro to tsql   unit 12
Intro to tsql unit 12Syed Asrarali
 
Locking unit 1 topic 3
Locking unit 1 topic 3Locking unit 1 topic 3
Locking unit 1 topic 3avniS
 
OTech magazine article - Principle of Least Privilege
OTech magazine article - Principle of Least PrivilegeOTech magazine article - Principle of Least Privilege
OTech magazine article - Principle of Least PrivilegeBiju Thomas
 
MySQL GTID Concepts, Implementation and troubleshooting
MySQL GTID Concepts, Implementation and troubleshooting MySQL GTID Concepts, Implementation and troubleshooting
MySQL GTID Concepts, Implementation and troubleshooting Mydbops
 
Exchange manage with scom
Exchange   manage with scomExchange   manage with scom
Exchange manage with scomGary Jackson
 
04 transaction models
04 transaction models04 transaction models
04 transaction modelsashish61_scs
 
New PLSQL in Oracle Database 12c
New PLSQL in Oracle Database 12cNew PLSQL in Oracle Database 12c
New PLSQL in Oracle Database 12cConnor McDonald
 
Less09 managing undo data
Less09 managing undo dataLess09 managing undo data
Less09 managing undo dataImran Ali
 
GLOC 2014 NEOOUG - Oracle Database 12c New Features
GLOC 2014 NEOOUG - Oracle Database 12c New FeaturesGLOC 2014 NEOOUG - Oracle Database 12c New Features
GLOC 2014 NEOOUG - Oracle Database 12c New FeaturesBiju Thomas
 
Database security
Database securityDatabase security
Database securityJaved Khan
 
Lecture 4. MS SQL. DML Triggers
Lecture 4. MS SQL. DML TriggersLecture 4. MS SQL. DML Triggers
Lecture 4. MS SQL. DML TriggersAlexey Furmanov
 
MySQL Timeout Variables Explained
MySQL Timeout Variables Explained MySQL Timeout Variables Explained
MySQL Timeout Variables Explained Mydbops
 
Data power Performance Tuning
Data power Performance TuningData power Performance Tuning
Data power Performance TuningKINGSHUK MAJUMDER
 
SQL Server Transaction Management
SQL Server Transaction ManagementSQL Server Transaction Management
SQL Server Transaction ManagementDenise McInerney
 
Error management
Error managementError management
Error managementdaniil3
 
MySQL Performance Schema : fossasia
MySQL Performance Schema : fossasiaMySQL Performance Schema : fossasia
MySQL Performance Schema : fossasiaMayank Prasad
 

Similar to ISAS On SQL Features like Trigger, Transaction,Batches, Stored Procedure (20)

T-SQL & Triggers
T-SQL & TriggersT-SQL & Triggers
T-SQL & Triggers
 
Intro to tsql unit 12
Intro to tsql   unit 12Intro to tsql   unit 12
Intro to tsql unit 12
 
Locking unit 1 topic 3
Locking unit 1 topic 3Locking unit 1 topic 3
Locking unit 1 topic 3
 
OTech magazine article - Principle of Least Privilege
OTech magazine article - Principle of Least PrivilegeOTech magazine article - Principle of Least Privilege
OTech magazine article - Principle of Least Privilege
 
MySQL GTID Concepts, Implementation and troubleshooting
MySQL GTID Concepts, Implementation and troubleshooting MySQL GTID Concepts, Implementation and troubleshooting
MySQL GTID Concepts, Implementation and troubleshooting
 
Exchange manage with scom
Exchange   manage with scomExchange   manage with scom
Exchange manage with scom
 
04 transaction models
04 transaction models04 transaction models
04 transaction models
 
New PLSQL in Oracle Database 12c
New PLSQL in Oracle Database 12cNew PLSQL in Oracle Database 12c
New PLSQL in Oracle Database 12c
 
Less09 managing undo data
Less09 managing undo dataLess09 managing undo data
Less09 managing undo data
 
Introduction to mysql part 3
Introduction to mysql part 3Introduction to mysql part 3
Introduction to mysql part 3
 
GLOC 2014 NEOOUG - Oracle Database 12c New Features
GLOC 2014 NEOOUG - Oracle Database 12c New FeaturesGLOC 2014 NEOOUG - Oracle Database 12c New Features
GLOC 2014 NEOOUG - Oracle Database 12c New Features
 
Database security
Database securityDatabase security
Database security
 
Lecture 4. MS SQL. DML Triggers
Lecture 4. MS SQL. DML TriggersLecture 4. MS SQL. DML Triggers
Lecture 4. MS SQL. DML Triggers
 
MySQL Timeout Variables Explained
MySQL Timeout Variables Explained MySQL Timeout Variables Explained
MySQL Timeout Variables Explained
 
Data power Performance Tuning
Data power Performance TuningData power Performance Tuning
Data power Performance Tuning
 
SQL Server Transaction Management
SQL Server Transaction ManagementSQL Server Transaction Management
SQL Server Transaction Management
 
Error management
Error managementError management
Error management
 
MySQL Performance Schema : fossasia
MySQL Performance Schema : fossasiaMySQL Performance Schema : fossasia
MySQL Performance Schema : fossasia
 
Ibm db2 case study
Ibm db2 case studyIbm db2 case study
Ibm db2 case study
 
Module04
Module04Module04
Module04
 

Recently uploaded

9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 

Recently uploaded (20)

9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 

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

  • 1. © 2012 2S Corporation. All rights reserved.
  • 2.  Some Sql Features © 2012 2S Corporation. All rights reserved. Trigger Transaction Batches Stored Procedure
  • 3. DML DDL © 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: INSERT UPDATE DELETE CREATE TABLE ALTER TABLE
  • 4. 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.] © 2012 2S Corporation. All rights reserved. Note: The general syntax of DROP TRIGGER is : DROP TRIGGER trigger_name
  • 5. What is DML Trigger? A DML Trigger is fired when data in the underlying table is affected by DML statements, such as INSERT,UPDATE,DELETE. © 2012 2S Corporation. All rights reserved. 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
  • 6. ALTER TRIGGGER trigger_name {FOR|AFTER}{event_type[,...n] | DDL_DATABASE_LEVEL_EVENTS} {AS {sql_statement[...n]} } © 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
  • 7. © 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
  • 8. The SQL Server allows implementing transactions in the following two ways: © 2012 2S Corporation. All rights reserved. 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
  • 9. Reverting Transaction At a time there is a possibility that due to a problem all the statements of a transaction will not be successfully executed. Client gets an error with “Try Again” msg Client still waiting © 2012 2S Corporation. All rights reserved. Transaction timed out For example:- 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 Then we use for resolving this problem:-  ROLLBACK TRANSACTION: ROLLBACK TRAN[SACTION] [transaction_name | @tran_name_variable | savepoint_name | @savepoint_variable]]
  • 10. Transactional Integrity To maintain the Security & Concurrency issues in giving Multi-user Environment we use 'LOCKS'. © 2012 2S Corporation. All rights reserved.  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
  • 11. © 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 Types of isolation levels are: 1. READ UNCOMMITED 2. READ COMMITED 3. REPEATABLE READ 4. SNAPSHOT 5. SERIALIZABLE  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.
  • 12. © 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
  • 13. © 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.
  • 14.  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. © 2012 2S Corporation. All rights reserved. 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.
  • 15. © 2012 2S Corporation. All rights reserved.  Stored Procedures A stored Procedure is a Pre-Compiled object stored in the database. The syntax of the create Procedure Statement is:- CREATE PROCEDURE proc_name AS BEGIN sql_statment1, sql_statement2 END  Executing a Stored Procedure A procedure can be executed by using the EXEC PROCEDURE statement. EXEC | EXECUTE PROCEDURE proc_name AS [{login|user} ='name']
  • 16. © 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
  • 17. 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. © 2012 2S Corporation. All rights reserved. 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’
  • 18. © 2012 2S Corporation. All rights reserved.