ISAS On SQL Features like Trigger, Transaction,Batches, Stored Procedure
Stored procedure,transaction
-
Upload
baabtracom-first-coding-school-in-india -
Category
Technology
-
view
187 -
download
0
Transcript of Stored procedure,transaction
![Page 1: Stored procedure,transaction](https://reader034.fdocuments.us/reader034/viewer/2022052619/5564a7f4d8b42a366c8b4ede/html5/thumbnails/1.jpg)
![Page 2: Stored procedure,transaction](https://reader034.fdocuments.us/reader034/viewer/2022052619/5564a7f4d8b42a366c8b4ede/html5/thumbnails/2.jpg)
Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
![Page 3: Stored procedure,transaction](https://reader034.fdocuments.us/reader034/viewer/2022052619/5564a7f4d8b42a366c8b4ede/html5/thumbnails/3.jpg)
Week Target Achieved
1
2
3
Typing Speed
![Page 4: Stored procedure,transaction](https://reader034.fdocuments.us/reader034/viewer/2022052619/5564a7f4d8b42a366c8b4ede/html5/thumbnails/4.jpg)
Jobs AppliedWeek Company Designation Applied Date Current Status
1
2
3
![Page 5: Stored procedure,transaction](https://reader034.fdocuments.us/reader034/viewer/2022052619/5564a7f4d8b42a366c8b4ede/html5/thumbnails/5.jpg)
Stored Procedures, Cursors and Transactions
Muhammed [email protected]/usernametwitter.com/usernamein.linkedin.com/in/profilename1234567890
![Page 6: Stored procedure,transaction](https://reader034.fdocuments.us/reader034/viewer/2022052619/5564a7f4d8b42a366c8b4ede/html5/thumbnails/6.jpg)
Overview
• Stored Procedure• Structure of stored procedure• Cursor• Example-cursor• Transaction• Example-transaction
![Page 7: Stored procedure,transaction](https://reader034.fdocuments.us/reader034/viewer/2022052619/5564a7f4d8b42a366c8b4ede/html5/thumbnails/7.jpg)
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.
![Page 8: Stored procedure,transaction](https://reader034.fdocuments.us/reader034/viewer/2022052619/5564a7f4d8b42a366c8b4ede/html5/thumbnails/8.jpg)
Stored Procedure contd..
![Page 9: Stored procedure,transaction](https://reader034.fdocuments.us/reader034/viewer/2022052619/5564a7f4d8b42a366c8b4ede/html5/thumbnails/9.jpg)
Structure of storedprocedure
CREATE PROCEDURE proc_name@parameter data typeAS BEGIN //defnition of the spEND
![Page 10: Stored procedure,transaction](https://reader034.fdocuments.us/reader034/viewer/2022052619/5564a7f4d8b42a366c8b4ede/html5/thumbnails/10.jpg)
Example for Stored Procedure
CREATE PROCEDURE test@capital int,@rate INT,@duration INT, @interest int OUTASBegin SET @interest = (@capital * @rate * @duration)/100;end
![Page 11: Stored procedure,transaction](https://reader034.fdocuments.us/reader034/viewer/2022052619/5564a7f4d8b42a366c8b4ede/html5/thumbnails/11.jpg)
Stored Procedure contd..
• EXEC Or EXECUTE• Example :
– EXECUTE test 100,5,2,@res;
![Page 12: Stored procedure,transaction](https://reader034.fdocuments.us/reader034/viewer/2022052619/5564a7f4d8b42a366c8b4ede/html5/thumbnails/12.jpg)
Advantage
• Improve performance • Share logic• Reusability
![Page 13: Stored procedure,transaction](https://reader034.fdocuments.us/reader034/viewer/2022052619/5564a7f4d8b42a366c8b4ede/html5/thumbnails/13.jpg)
Cursor
![Page 14: Stored procedure,transaction](https://reader034.fdocuments.us/reader034/viewer/2022052619/5564a7f4d8b42a366c8b4ede/html5/thumbnails/14.jpg)
Cursor
• Cursor is used to iterate through a set of rows returned by a query and process each row.
![Page 15: Stored procedure,transaction](https://reader034.fdocuments.us/reader034/viewer/2022052619/5564a7f4d8b42a366c8b4ede/html5/thumbnails/15.jpg)
Cursor contd..
DECLARE cursor_name CURSOR FOR SELECT_statement
OPEN cursor_name
FETCH next from cursor_name INTO variables list
CLOSE cursor_name;
![Page 16: Stored procedure,transaction](https://reader034.fdocuments.us/reader034/viewer/2022052619/5564a7f4d8b42a366c8b4ede/html5/thumbnails/16.jpg)
Cursor contd..
![Page 17: Stored procedure,transaction](https://reader034.fdocuments.us/reader034/viewer/2022052619/5564a7f4d8b42a366c8b4ede/html5/thumbnails/17.jpg)
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;
![Page 18: Stored procedure,transaction](https://reader034.fdocuments.us/reader034/viewer/2022052619/5564a7f4d8b42a366c8b4ede/html5/thumbnails/18.jpg)
Example contd..
FETCH next from cur_salary INTO @t_id,@d_name;
While (@@Fetch_Status = 0) Beginif 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
![Page 19: Stored procedure,transaction](https://reader034.fdocuments.us/reader034/viewer/2022052619/5564a7f4d8b42a366c8b4ede/html5/thumbnails/19.jpg)
Example contd..
FETCH next from cur_salary INTO @t_id,@d_name;
end
CLOSE cur_salary;
end
![Page 20: Stored procedure,transaction](https://reader034.fdocuments.us/reader034/viewer/2022052619/5564a7f4d8b42a366c8b4ede/html5/thumbnails/20.jpg)
Transaction
![Page 21: Stored procedure,transaction](https://reader034.fdocuments.us/reader034/viewer/2022052619/5564a7f4d8b42a366c8b4ede/html5/thumbnails/21.jpg)
Transaction
• A transaction is a logical unit of work that contains one or more SQL statements
• Executes as a unit.
![Page 22: Stored procedure,transaction](https://reader034.fdocuments.us/reader034/viewer/2022052619/5564a7f4d8b42a366c8b4ede/html5/thumbnails/22.jpg)
Transaction Control
BEGIN TRANSACTION
//logics…
If error
ROLLBACK TRANSACTION
Else
COMMIT TRANSACTION
![Page 23: Stored procedure,transaction](https://reader034.fdocuments.us/reader034/viewer/2022052619/5564a7f4d8b42a366c8b4ede/html5/thumbnails/23.jpg)
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 ;
![Page 24: Stored procedure,transaction](https://reader034.fdocuments.us/reader034/viewer/2022052619/5564a7f4d8b42a366c8b4ede/html5/thumbnails/24.jpg)
Example Transaction contd..
update Account set balance=balance+900 where Account_Number=ac_no2;COMMIT TRANSACTIONEND tryBEGIN CATCH ROLLBACK TRANSACTIONEND CATCHEND
![Page 25: Stored procedure,transaction](https://reader034.fdocuments.us/reader034/viewer/2022052619/5564a7f4d8b42a366c8b4ede/html5/thumbnails/25.jpg)
ACID Property
• Atomicity• Consistent• Isolation• Durability
![Page 26: Stored procedure,transaction](https://reader034.fdocuments.us/reader034/viewer/2022052619/5564a7f4d8b42a366c8b4ede/html5/thumbnails/26.jpg)
Thank You
![Page 27: Stored procedure,transaction](https://reader034.fdocuments.us/reader034/viewer/2022052619/5564a7f4d8b42a366c8b4ede/html5/thumbnails/27.jpg)
Questions??
![Page 28: Stored procedure,transaction](https://reader034.fdocuments.us/reader034/viewer/2022052619/5564a7f4d8b42a366c8b4ede/html5/thumbnails/28.jpg)
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
![Page 29: Stored procedure,transaction](https://reader034.fdocuments.us/reader034/viewer/2022052619/5564a7f4d8b42a366c8b4ede/html5/thumbnails/29.jpg)
Contact Us
Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550
NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550
Start up VillageEranakulam,Kerala, India.
Email: [email protected]
IKK Road,East Hill, KozhikodeKerala, India.Ph: + 91 – 495 30 63 624
NIT-TBI,NIT Campus, Kozhikode,Kerala, India.