Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
docu...
Week Target Achieved
1
2
3
Typing Speed
Jobs Applied
Week Company Designation Applied Date Current Status
1
2
3
Stored Procedures, Cursors and
Transactions
Muhammed Ajmal
mhdajmalik@gmail.com
www.facebook.com/userna
me
twitter.com/use...
Overview
• Stored Procedure
• Structure of stored procedure
• Cursor
• Example-cursor
• Transaction
• Example-transaction
Stored Procedure
• A stored procedure is a set of SQL statements.
• stored in the server.
• Allow for variable declaration...
Stored Procedure contd..
Structure of storedprocedure
CREATE PROCEDURE proc_name
@parameter data type
AS
BEGIN
//defnition of the sp
END
Example for Stored Procedure
CREATE PROCEDURE test
@capital int,
@rate INT,
@duration INT,
@interest int OUT
AS
Begin
SET ...
Stored Procedure contd..
• EXEC Or EXECUTE
• Example :
– EXECUTE test 100,5,2,@res;
Advantage
• Improve performance
• Share logic
• Reusability
Cursor
Cursor
• Cursor is used to iterate through a set of rows
returned by a query and process each row.
Cursor contd..
DECLARE cursor_name CURSOR FOR SELECT_statement
OPEN cursor_name
FETCH next from cursor_name INTO variables...
Cursor contd..
Cursor Example
create procedure cur_salary
@dept_name varchar(20)
As
Begin
Declare @t_id int;
Declare @d_name varchar(10);...
Example contd..
FETCH next from cur_salary INTO @t_id,@d_name;
While (@@Fetch_Status = 0)
Begin
if d_name=dept_name
begin
...
Example contd..
FETCH next from cur_salary INTO
@t_id,@d_name;
end
CLOSE cur_salary;
end
Transaction
Transaction
• A transaction is a logical unit of work that contains
one or more SQL statements
• Executes as a unit.
Transaction Control
BEGIN TRANSACTION
//logics…
If error
ROLLBACK TRANSACTION
Else
COMMIT TRANSACTION
Example Transaction
create stored procedure
@ac_no1 int,
@ac_no2 int
As
BEGIN
BEGIN try
BEGIN transaction;
select balance ...
Example Transaction contd..
update Account set balance=balance+900
where Account_Number=ac_no2;
COMMIT TRANSACTION
END try...
ACID Property
• Atomicity
• Consistent
• Isolation
• Durability
Thank You
Questions??
If this presentation helped you, please visit our
page facebook.com/baabtra and like it.
Thanks in advance.
www.baabtra.co...
Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, N...
Stored procedure,transaction
Upcoming SlideShare
Loading in …5
×

Stored procedure,transaction

359 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Stored procedure,transaction

  1. 1. Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
  2. 2. Week Target Achieved 1 2 3 Typing Speed
  3. 3. Jobs Applied Week Company Designation Applied Date Current Status 1 2 3
  4. 4. Stored Procedures, Cursors and Transactions Muhammed Ajmal mhdajmalik@gmail.com www.facebook.com/userna me twitter.com/username in.linkedin.com/in/profilena me 1234567890
  5. 5. Overview • Stored Procedure • Structure of stored procedure • Cursor • Example-cursor • Transaction • Example-transaction
  6. 6. Stored Procedure • A stored procedure is a set of SQL statements. • stored in the server. • Allow for variable declarations, flow control and other useful programming techniques.
  7. 7. Stored Procedure contd..
  8. 8. Structure of storedprocedure CREATE PROCEDURE proc_name @parameter data type AS BEGIN //defnition of the sp END
  9. 9. Example for Stored Procedure CREATE PROCEDURE test @capital int, @rate INT, @duration INT, @interest int OUT AS Begin SET @interest = (@capital * @rate * @duration)/100; end
  10. 10. Stored Procedure contd.. • EXEC Or EXECUTE • Example : – EXECUTE test 100,5,2,@res;
  11. 11. Advantage • Improve performance • Share logic • Reusability
  12. 12. Cursor
  13. 13. Cursor • Cursor is used to iterate through a set of rows returned by a query and process each row.
  14. 14. Cursor contd.. DECLARE cursor_name CURSOR FOR SELECT_statement OPEN cursor_name FETCH next from cursor_name INTO variables list CLOSE cursor_name;
  15. 15. Cursor contd..
  16. 16. Cursor Example create procedure cur_salary @dept_name varchar(20) As Begin Declare @t_id int; Declare @d_name varchar(10); Declare cur_salary CURSOR FOR select id,department from tbl; Open cur_salary;
  17. 17. Example contd.. FETCH next from cur_salary INTO @t_id,@d_name; While (@@Fetch_Status = 0) Begin if d_name=dept_name begin Update tbl_teacher set salary=salary+500 where id=t_id; end Else begin Update tbl_teacher set salary=salary+400 where id=t_id; End
  18. 18. Example contd.. FETCH next from cur_salary INTO @t_id,@d_name; end CLOSE cur_salary; end
  19. 19. Transaction
  20. 20. Transaction • A transaction is a logical unit of work that contains one or more SQL statements • Executes as a unit.
  21. 21. Transaction Control BEGIN TRANSACTION //logics… If error ROLLBACK TRANSACTION Else COMMIT TRANSACTION
  22. 22. Example Transaction create stored procedure @ac_no1 int, @ac_no2 int As BEGIN BEGIN try BEGIN transaction; select balance from Account where Account_Number=ac_no1; select balance from Account where Account_Number=ac_no2; update Account set balance=balance-900 here Account_Number=ac_no1 ;
  23. 23. Example Transaction contd.. update Account set balance=balance+900 where Account_Number=ac_no2; COMMIT TRANSACTION END try BEGIN CATCH ROLLBACK TRANSACTION END CATCH END
  24. 24. ACID Property • Atomicity • Consistent • Isolation • Durability
  25. 25. Thank You
  26. 26. Questions??
  27. 27. If this presentation helped you, please visit our page facebook.com/baabtra and like it. Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
  28. 28. Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com IKK Road, East Hill, Kozhikode Kerala, India. Ph: + 91 – 495 30 63 624 NIT-TBI, NIT Campus, Kozhikode, Kerala, India.

×