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

SQL Procedure

A procedure is a collection of SQL statements stored in a database that can be executed later. Stored procedures make databases more secure by allowing access to specific procedures without granting access to underlying tables. Procedures are created using the CREATE PROCEDURE statement and can declare variables, perform operations, and return values using SELECT statements. Delimiters are used to separate blocks of code in procedures.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

SQL Procedure

A procedure is a collection of SQL statements stored in a database that can be executed later. Stored procedures make databases more secure by allowing access to specific procedures without granting access to underlying tables. Procedures are created using the CREATE PROCEDURE statement and can declare variables, perform operations, and return values using SELECT statements. Delimiters are used to separate blocks of code in procedures.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Procedure

A procedure is a collection of pre-compiled


SQL statements stored inside the database.


If you want to save the query on the database
server for execution later, one way to do it is to
use a stored procedure.

2
Make database more secure
The database administrator can grant
appropriate privileges to applications that
only access specific stored procedures
without giving any privileges on the
underlying tables.

3
Basic structure of procedure
create procedure [procedure name]

Delete procedure
drop procedure [procedure name];

4
DELIMITER //
CREATE PROCEDURE GetMovies()
BEGIN
select title,description,release_year,rating from film;
END //

DELIMITER ;

5
delimiter //
create procedure count_workers()
begin
select count(*) from worker;
end //
delimiter ;

call count_workers();

6
delimiter //
create procedure count_all()
begin
select count(distinct supervisor_id) as No_of_SV
from worker;
select count(*) as no_workers from worker;
end //
delimiter ;

7 call count_all;
Show procedure

o show procedure status;


o show procedure status where Db = 'construction';
o show create procedure count_workers;

8
Grant user to procedure
create user 'customer01'@'localhost' identified by '123';
grant execute on procedure construction.count_workers
to 'customer01'@'localhost';

9
MySQL Delimiter

10
Instructions for use
Delimiter
DEFAULT DELIMITER CHANGED TO //

SELECT * FROM products; DELIMITER //


SELECT * FROM customers //
SELECT * FROM customers;
SELECT * FROM products //

11
1 Declare variables
Declare
◇ declare i int;
◇ declare i int default 0;
◇ declare i,x,y int default 0;
◇ declare name varchar(255);
◇ declare marks double;
◇ declare DOB date;

Assign value to variable


◇ SET i=10;

Select
◇ Select i ;

13
delimiter //
create procedure print_number()
begin
declare i int default 0;
set i=45;
select i;
end //
delimiter ;

call print_number;

14
Exercise: Arithmetic operation

15
delimiter //
create procedure MathOp()
begin
declare x,y int default 0;
set x = 10;
set y = 3;
select x+y;
select x-y;
select x*y;
select x/y;
end //
delimiter ;
call MathOp;
Delimiter //
create procedure Arith_op()
begin
declare a,b,sum,sub,mul int default 0;
declare divi double default 0.0;
SET a=15;
SET b=10;
SET sum=a+b;
SET sub=a-b;
SET mul=a*b;
SET divi=a/b;
select sum,sub,mul,divi;
end //
Delimiter ;

call Arith_op();
17
Get values from query and
2 assign to variable
delimiter //
create procedure count_To_var()
begin
declare a int default 0;
select count(*) into a
from worker;
select a as A_is;
end //
delimiter ;

19 call count_To_var();
Scope of variable
3
Delimiter //
create procedure Sample_scope()
begin
begin
#Scope of x is local
Declare x int default 0;
SET x=12;
select x;
end ;
begin
Declare x int default 0;
SET x=1;
select x;
end ;
end //
Delimiter ;
Global scope

Delimiter //
create procedure Sample_scope4()
begin
declare x int default 0;
SET x=10;
begin
declare x int default 0;
SET x=5;
select x;
end ;
begin
declare y int default 0;
SET y=5;
select y;
end ;
select x;
end //
Delimiter ;
Thanks!
Any questions?

23

You might also like