Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
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
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
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.
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
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
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
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