0% found this document useful (0 votes)
19 views

Lec13 - Lab - CSC371 - Database Systems

This document discusses SQL stored procedures. Stored procedures allow saving prepared SQL code that can be reused and executed repeatedly by calling the stored procedure. Parameters can be passed to stored procedures to make them more flexible. The document provides examples of simple stored procedures with one or multiple parameters and statements.

Uploaded by

sp22-bse-097
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Lec13 - Lab - CSC371 - Database Systems

This document discusses SQL stored procedures. Stored procedures allow saving prepared SQL code that can be reused and executed repeatedly by calling the stored procedure. Parameters can be passed to stored procedures to make them more flexible. The document provides examples of simple stored procedures with one or multiple parameters and statements.

Uploaded by

sp22-bse-097
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

1

CSC371-DATABASE SYSTEMS I LECTURE-13


(LAB)

(FALL 2023)
2

PREVIOUS LECTURE REVIEW

SQL Sub Query


3
4

AGENDA
SQL Stored Procedure
SQL STORED PROCEDURES 5

• A stored procedure is a prepared SQL code that you can save.


• The code can be reused repeatedly.
• So if you have an SQL query that you write again and again, save it as a stored
procedure, and then just call it to execute it.
• You can also pass parameters to a stored procedure, so that the stored procedure
can act based on the parameter value(s) that is passed.
• A stored procedure can consist of more than one query.
• Syntax
• CREATE PROCEDURE procedure_name
AS
sql_statement (s)
;

• Execution
• EXEC procedure_name;
SIMPLE EXAMPLE 7

• CREATE PROCEDURE SelectAllStaff


AS
SELECT * FROM Staff;

• exec selectallStaff;
STORED PROCEDURE WITH ONE PARAMETER 8

• CREATE PROCEDURE SelectPositionWiseStaff @position varchar(30)


AS
SELECT * FROM staff WHERE position = @position;

• exec selectpositionwisestaff 'manager'


STORED PROCEDURE WITH MULTIPLE PARAMETERS 9

• CREATE PROCEDURE PropertiesBranchstaff @staffno varchar(30), @branchno varchar(10)


AS
SELECT propertyno FROM PropertyForRent WHERE staffno = @staffno AND branchno =
@branchno;

• exec PropertiesBranchstaff @staffno = ‘SG37', @branchno = ‘B003’;


STORED PROCEDURE WITH MULTIPLE 10

STATEMENTS
• CREATE PROCEDURE SelectPositionWiseStaff_branch @position varchar(30)
AS
SELECT * FROM staff WHERE position = @position;
SELECT * FROM branch where branchno in (select branchno from staff where position=
@position);
• execute SelectPositionWiseStaff_branch ‘manager’;
SALARY UPDATION OF AN ASSISTANT SL41 11

• CREATE PROCEDURE salaryUpdationStaff @position varchar(30), @staffno varchar(30),


@salary int
AS
SELECT * FROM staff WHERE position = @position AND staffno=@staffno;
update staff set salary = salary+(salary *@salary/100) where position = @position AND
staffno=@staffno;
SELECT * FROM staff WHERE position = @position AND staffno=@staffno;
• execute salaryUpdationStaff 'Assistant’, 'SL41’, 5;
12

SUMMARY
SQL Stored Procedure

You might also like