U2t6 Mysql PL SQL
U2t6 Mysql PL SQL
ADVANTAGES
PL/SQL supports both static and dynamic SQL.
Static SQL supports DML operations and transaction control from PL/SQL block.
In Dynamic SQL , SQL allows embedding DDL statements in PL/SQL blocks.
PL/SQL allows sending an entire block of statements to the database at one time.
This reduces network traffic and provides high performance for the applications.
PL/SQL saves time on design and debugging by strong features ,such as
o Exception handling
o Encapsulation
o Data hiding
o OO data types
PL/SQL is portable , provides high security level, provides access to predefined SQL packages.
PL/SQL provides support for developing web applications and server pages.
General Structure of PL/SQL
If u give above query the Mysql compiler will give error because
Usually in sql all the queries end with ; ( semicolon) , so compiler will understood it
This semicolon is called ( DELIMITER)
Generally in mysql default delimiter is ; ( semicolon)
In above program leads error because we have used delimiter at the end of (select *From
inventory ; )
So it terminate there it won’t execute the (END) query
So we can change the delimiter while creating large statement as procedure
DELIMITER $$ here we have change delimiter as $$
create procedure get_all_records()
BEGIN
select *from inventory;
END$$
DELIMITER ; here we have change default delimiter
Select all the above queries and click execute icon , now we got the stored_procedure in the left screen
like as following
call find_total_product();
OUT
DELIMITER $$
create procedure filter2(IN f1 int,OUT res int)
BEGIN
select count(prono) into res from inventory where rate>f1;
END$$
DELIMITER ;
call filter2(50,@res);
select @res;
call filter2(10,@res);
select @res;
/* here @res called session variable like as global variable */
IN/OUT
DELIMITER $$
create procedure increment(IN i int,INOUT res int)
BEGIN
set res=res+i;
END$$
DELIMITER ;
set @inc=100;
call increment(150,@inc);
select @inc;
call increment(150,@inc);
select @inc;
IF_ELSE_Statement
use krish;
select *from inventory;
DELIMITER $$
create procedure get_product_name(IN pid int)
BEGIN
if pid=100 THEN
select 'PEN';
elseif pid=101 THEN
select "PENCIL" ;
elseif pid=102 then
select "SCALE" ;
else
select "product not found";
end if;
END$$
DELIMITER ;
call get_product_name(100);
SWITCH CASE
DELIMITER $$
create procedure get_product_name1(IN pid int)
BEGIN
case pid
when 100 then
select "PEN";
when 101 then
select "PENCIL";
when 102 then
select "SCALE";
end case;
END$$
DELIMITER ;
call get_product_name1(102);
LOOP
DELIMITER $$
create procedure loopdemo()
BEGIN
DECLARE i INT;
SET i=1;
krish:loop
SELECT i;
END loop;
END $$
DELIMITER ;
call loopdemo();
if run the above loop is called infinite loop ..we can solve it like as following
DELIMITER $$
create procedure loopdemo()
BEGIN
DECLARE i INT;
SET i=1;
krish:loop
if i>10 THEN
leave krish;
end if;
SELECT i;
SET i=i+1;
END loop;
END $$
DELIMITER ;
call loopdemo();
in above output we got the result in separate window if u want to complain all the output in same window
DELIMITER $$
create procedure loopdemo()
BEGIN
DECLARE i INT;
DECLARE str varchar(30);
SET i=1;
SET str='';
krish:loop
if i>10 THEN
leave krish;
end if;
SET str=CONCAT(str,i,' ');
SET i=i+1;
END loop;
select str;
END $$
DELIMITER ;
FUNTIONS
1. CREATE TABLE users(id INT primary key, sname VARCHAR(50), age INT ,gender varchar(10),city
varchar(30),contact varchar(30));
2. INSERT INTO users(id,sname,age,gender,city,contact) VALUES
(100,'Ravi',23,'Male','Namakkal','9876543210'),
(101,'Sara',23,'Female','Erode','9874521360'),
(102,'mahi',18,'female','salem','9994477191'),
(103,'jeni',22,'female','chennai','9894585056');
3. select *from users;
Here we are going to do when user will enter ID no , we want to get city name and their contact number
with help of function
Example
delimiter $$
CREATE FUNCTION add_numbers (a INT, b INT)
RETURNS INT // here we are return the final result as integer that why here we mention INT
DETERMINISTIC
BEGIN
declare c int;
set c=a+b;
RETURN c;
END$$
delimiter ;
select add_numbers(5,2);
In above example we have error in our query but it execute ( select) query
Using Exit
delimiter $$
create procedure insert_Record(IN sid int,IN name varchar(30),IN a int , IN g varchar(10),IN c
varchar(30),con varchar(10))
BEGIN
declare exit handler for 1062
BEGIN
select concat("This",sid," Alerady exists in the table try to add different ID");
END;
insert into users values(sid,name,a,g,c,con);
select *from users;
END $$
delimiter ;
call insert_Record(100,"krish",35,"Male","tirupur","9994974215");
CURSOR
If u want to do some process for each and every from a table with the help of cursor
Cursor will move each and every row in a table for doing some process
Here we are going to take backup all data(rows) from table(users) to (employees) table with the
help of cursor
CREATE TABLE employees(id INT primary key, sname VARCHAR(50), age INT ,gender varchar(10),city
varchar(30),contact varchar(30),salary numeric(10,2));
delimiter $$
create procedure backu_Employee()
begin
declare done int default 0;
declare bid,bage,bsalary int;
declare bname,bgender,bcity,bcontact varchar(30);
declare cur cursor for select *from users;
declare exit handler for not found set done=1;
delete from employees;
open cur;
krish:loop
if done=1 then
leave krish;
end if;
fetch cur into bid,bname,bage,bgender,bcity,bcontact,bsalary;
insert into employees values(bid,bname,bage,bgender,bcity,bcontact,bsalary);
end loop;
close cur;
end $$
delimiter ;
call backu_Employee();
select *from employees;