SlideShare ist ein Scribd-Unternehmen logo
1 von 33
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
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/username
in.linkedin.com/in/profilena
me
1234567890
Overview
• Stored Procedure
• Structure of stored procedure
• Cursor
• Example-cursor
• Transaction
• Example-transaction
Stored Procedure
• A stored procedure is a set of SQL statements.
• Allow for variable declarations, flow control and
other useful programming techniques.
• stored in the server.
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
AS
Begin
select course_description,department_name,SUM(fph_amount)
Amount,COUNT(distinct fk_student_id) 'no.of Student',teacher_name
from
tbl_course c join tbl_course_department cd on c.course_id=cd.fk_course_id
join tbl_department d on d.department_id=cd.fk_department_id
join tbl_student s on s.fk_dept_id=d.department_id
join tbl_fee_payment_history on s.pk_student_id=fk_student_id
left join tbl_teacher t on department_id=t.fk_dept_id
where teacher_head_id is null
group by
d.department_id,c.course_description,d.department_name,teacher_name
end
Stored Procedure contd..
• EXEC Or EXECUTE
• Example :
– EXECUTE test;
Advantage
• Improve performance
• Share logic
• Reusability
• Reduce network traffic
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 list
CLOSE cursor_name;
Cursor contd..
Cursor Example
Cursor Example
Cursor Example
• declare @id int
• declare @total int
• declare cursor_grade Cursor for select pk_int_id,Total_mark from
tbl_student
• open cursor_grade
• fetch next from cursor_grade into @id,@total
• while(@@FETCH_STATUS=0)
• begin
• if(@total>=90)
• begin
• update tbl_student set grade='A+' where pk_int_id=@id
• end
Example contd..
• else if(@total>=80)
• begin
• update tbl_student set grade='A' where pk_int_id=@id
• end
• else if(@total>=70)
• begin
• update tbl_student set grade='B+' where pk_int_id=@id
• end
• else if(@total>=60)
• begin
• update tbl_student set grade='B' where pk_int_id=@id
• end
Example contd..
• else
• begin
• update tbl_student set grade='failed' where pk_int_id=@id
• end
• fetch next from cursor_grade into @id,@total
• end
• close cursor_grade
Example contd..
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 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 ;
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
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.com | www.massbaab.com |www.baabte.com
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.
• Create a View to list out courses, department
name, Department Head ,the total fee
collected for that course and total number of
students studying for that course

Weitere ähnliche Inhalte

Ähnlich wie Stored procedure

Ruslan Platonov - Transactions
Ruslan Platonov - TransactionsRuslan Platonov - Transactions
Ruslan Platonov - Transactions
Dmitry Buzdin
 
Intro to tsql unit 14
Intro to tsql   unit 14Intro to tsql   unit 14
Intro to tsql unit 14
Syed Asrarali
 
22-4_PerformanceTuningUsingtheAdvisorFramework.pdf
22-4_PerformanceTuningUsingtheAdvisorFramework.pdf22-4_PerformanceTuningUsingtheAdvisorFramework.pdf
22-4_PerformanceTuningUsingtheAdvisorFramework.pdf
yishengxi
 

Ähnlich wie Stored procedure (20)

Module04
Module04Module04
Module04
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 3 stored procedures
 
PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts
 
Ruslan Platonov - Transactions
Ruslan Platonov - TransactionsRuslan Platonov - Transactions
Ruslan Platonov - Transactions
 
Intro to tsql
Intro to tsqlIntro to tsql
Intro to tsql
 
Intro to tsql unit 14
Intro to tsql   unit 14Intro to tsql   unit 14
Intro to tsql unit 14
 
Tech_Implementation of Complex ITIM Workflows
Tech_Implementation of Complex ITIM WorkflowsTech_Implementation of Complex ITIM Workflows
Tech_Implementation of Complex ITIM Workflows
 
Stored procedures
Stored proceduresStored procedures
Stored procedures
 
Stored procedures by thanveer danish melayi
Stored procedures by thanveer danish melayiStored procedures by thanveer danish melayi
Stored procedures by thanveer danish melayi
 
PLSQL
PLSQLPLSQL
PLSQL
 
Procedures and triggers in SQL
Procedures and triggers in SQLProcedures and triggers in SQL
Procedures and triggers in SQL
 
Stored procedures and cursors
Stored procedures and cursorsStored procedures and cursors
Stored procedures and cursors
 
Reproducible AI Using PyTorch and MLflow
Reproducible AI Using PyTorch and MLflowReproducible AI Using PyTorch and MLflow
Reproducible AI Using PyTorch and MLflow
 
Performance Instrumentation for PL/SQL: When, Why, How
Performance Instrumentation for PL/SQL: When, Why, HowPerformance Instrumentation for PL/SQL: When, Why, How
Performance Instrumentation for PL/SQL: When, Why, How
 
Sql transacation
Sql transacationSql transacation
Sql transacation
 
Stored procedures with cursor
Stored procedures with cursorStored procedures with cursor
Stored procedures with cursor
 
Stored procedure Notes By Durgesh Singh
Stored procedure Notes By Durgesh SinghStored procedure Notes By Durgesh Singh
Stored procedure Notes By Durgesh Singh
 
22-4_PerformanceTuningUsingtheAdvisorFramework.pdf
22-4_PerformanceTuningUsingtheAdvisorFramework.pdf22-4_PerformanceTuningUsingtheAdvisorFramework.pdf
22-4_PerformanceTuningUsingtheAdvisorFramework.pdf
 
Sql storeprocedure
Sql storeprocedureSql storeprocedure
Sql storeprocedure
 
Blue Prism training contents
Blue Prism training contentsBlue Prism training contents
Blue Prism training contents
 

Mehr von baabtra.com - No. 1 supplier of quality freshers

Mehr von baabtra.com - No. 1 supplier of quality freshers (20)

Agile methodology and scrum development
Agile methodology and scrum developmentAgile methodology and scrum development
Agile methodology and scrum development
 
Best coding practices
Best coding practicesBest coding practices
Best coding practices
 
Core java - baabtra
Core java - baabtraCore java - baabtra
Core java - baabtra
 
Acquiring new skills what you should know
Acquiring new skills   what you should knowAcquiring new skills   what you should know
Acquiring new skills what you should know
 
Baabtra.com programming at school
Baabtra.com programming at schoolBaabtra.com programming at school
Baabtra.com programming at school
 
99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love 99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 
Php database connectivity
Php database connectivityPhp database connectivity
Php database connectivity
 
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
 
Chapter 5 transactions and dcl statements
Chapter 5  transactions and dcl statementsChapter 5  transactions and dcl statements
Chapter 5 transactions and dcl statements
 
Chapter 4 functions, views, indexing
Chapter 4  functions, views, indexingChapter 4  functions, views, indexing
Chapter 4 functions, views, indexing
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Microsoft holo lens
Microsoft holo lensMicrosoft holo lens
Microsoft holo lens
 
Blue brain
Blue brainBlue brain
Blue brain
 
5g
5g5g
5g
 
Aptitude skills baabtra
Aptitude skills baabtraAptitude skills baabtra
Aptitude skills baabtra
 
Gd baabtra
Gd baabtraGd baabtra
Gd baabtra
 
Baabtra soft skills
Baabtra soft skillsBaabtra soft skills
Baabtra soft skills
 

Kürzlich hochgeladen

Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
FIDO Alliance
 
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
FIDO Alliance
 

Kürzlich hochgeladen (20)

The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfThe Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
 
Oauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoftOauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoft
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
 
AI mind or machine power point presentation
AI mind or machine power point presentationAI mind or machine power point presentation
AI mind or machine power point presentation
 
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
 
ERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage Intacct
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
 
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdf
 
Overview of Hyperledger Foundation
Overview of Hyperledger FoundationOverview of Hyperledger Foundation
Overview of Hyperledger Foundation
 
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
 
Your enemies use GenAI too - staying ahead of fraud with Neo4j
Your enemies use GenAI too - staying ahead of fraud with Neo4jYour enemies use GenAI too - staying ahead of fraud with Neo4j
Your enemies use GenAI too - staying ahead of fraud with Neo4j
 
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdfSimplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
 
2024 May Patch Tuesday
2024 May Patch Tuesday2024 May Patch Tuesday
2024 May Patch Tuesday
 
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The InsideCollecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
 
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
 
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
 
Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024
 
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfWhere to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
 
Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform Engineering
 

Stored procedure

  • 1.
  • 2. 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
  • 4. Jobs Applied Week Company Designation Applied Date Current Status 1 2 3
  • 5. 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
  • 6. Overview • Stored Procedure • Structure of stored procedure • Cursor • Example-cursor • Transaction • Example-transaction
  • 7. Stored Procedure • A stored procedure is a set of SQL statements. • Allow for variable declarations, flow control and other useful programming techniques. • stored in the server.
  • 9. Structure of storedprocedure CREATE PROCEDURE proc_name @parameter data type AS BEGIN //defnition of the sp END
  • 10. Example for Stored Procedure CREATE PROCEDURE test AS Begin select course_description,department_name,SUM(fph_amount) Amount,COUNT(distinct fk_student_id) 'no.of Student',teacher_name from tbl_course c join tbl_course_department cd on c.course_id=cd.fk_course_id join tbl_department d on d.department_id=cd.fk_department_id join tbl_student s on s.fk_dept_id=d.department_id join tbl_fee_payment_history on s.pk_student_id=fk_student_id left join tbl_teacher t on department_id=t.fk_dept_id where teacher_head_id is null group by d.department_id,c.course_description,d.department_name,teacher_name end
  • 11. Stored Procedure contd.. • EXEC Or EXECUTE • Example : – EXECUTE test;
  • 12. Advantage • Improve performance • Share logic • Reusability • Reduce network traffic
  • 14. Cursor • Cursor is used to iterate through a set of rows returned by a query and process each row.
  • 15. Cursor contd.. DECLARE cursor_name CURSOR FOR SELECT_statement OPEN cursor_name FETCH next from cursor_name INTO variables list CLOSE cursor_name;
  • 19. Cursor Example • declare @id int • declare @total int • declare cursor_grade Cursor for select pk_int_id,Total_mark from tbl_student • open cursor_grade • fetch next from cursor_grade into @id,@total • while(@@FETCH_STATUS=0) • begin • if(@total>=90) • begin • update tbl_student set grade='A+' where pk_int_id=@id • end
  • 20. Example contd.. • else if(@total>=80) • begin • update tbl_student set grade='A' where pk_int_id=@id • end • else if(@total>=70) • begin • update tbl_student set grade='B+' where pk_int_id=@id • end • else if(@total>=60) • begin • update tbl_student set grade='B' where pk_int_id=@id • end
  • 21. Example contd.. • else • begin • update tbl_student set grade='failed' where pk_int_id=@id • end • fetch next from cursor_grade into @id,@total • end • close cursor_grade
  • 24. Transaction • A transaction is a logical unit of work that contains one or more SQL statements • Executes as a unit.
  • 25. Transaction Control BEGIN TRANSACTION //logics… If error ROLLBACK TRANSACTION Else COMMIT TRANSACTION
  • 26. 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 ;
  • 27. 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
  • 28. ACID Property • Atomicity • Consistent • Isolation • Durability
  • 31. 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
  • 32. 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.
  • 33. • Create a View to list out courses, department name, Department Head ,the total fee collected for that course and total number of students studying for that course