You are on page 1of 4

link

**************************************
http://www.mysqltutorial.org/mysql-stored-procedure-tutorial.aspx
**************************************
Stored procedure:
**************************************
DELIMITER //
create procedure test1()
begin
select * from code;
end
Call Stored procedure:
Call test1();
**************************************
Stored procedure with where condition:
DELIMITER //
create procedure test1()
begin
select * from code where `S.No.`=1;
end
Call Stored procedure:
Call test2();
***************************************
Stored procedure with local parameter:
DELIMITER //
create procedure test2()
begin
Declare par1 int default 0;
select Count(*) into par1 from code;
end
***************************************
Stored procedure with in parameter:
DELIMITER //
create procedure test3(in SNO int(11))
begin
select * from code where `S.No.`= SNO;
end
Call Stored procedure:
Call test3(1);
***************************************
Stored procedure with out parameter:
DELIMITER //
create procedure test4(in SNO int(11),out oaim varchar(255))
begin
select aim into oaim from code where `S.No.`= SNO;
end
Call Stored procedure:
Call test3(1,@oaim);
***************************************
Stored procedure with inout parameter:
DELIMITER //

create procedure test5(inout SNO int(11))


begin
select count(*) into SNO from code where `S.No.`= SNO;
end
Call Stored procedure:
SET @SNO = 1;
CALL test5(@SNO);
SELECT @SNO;
**************************************
Stored procedure with if statement:
DELIMITER //
create procedure test6(in SNO int(11),out act int(11))
begin
declare a int;
select count(*) into a from code where `S.No.`= SNO;
if a>5 then set act=20000;
elseif (a>1 and a<=5) then set act=30000;
elseif a<=1 then set act=50000;
end if;
end
Call Stored procedure:
CALL test6(3,@act);
SELECT @act;
**************************************
Stored procedure with simple case statement:
DELIMITER //
create procedure test7(in SNO int(11),out act int(11))
begin
declare a int;
select count(*) into a from code where `S.No.`= SNO;
case a
when 3 then set act=20000;
when 2 then set act=30000;
when 1 then set act=50000;
end case;
end
Call Stored procedure:
CALL test7(3,@act);
SELECT @act;
DELIMITER //
create procedure test8(in SNO int(11),out act int(11))
begin
declare a int;
select count(*) into a from code where `S.No.`= SNO;
case
when a>5 then set act=20000;
when (a>1 and a<=5) then set act=30000;
when a<=1 then set act=50000;
end case;
end
Call Stored procedure:
CALL test8(3,@act);
SELECT @act;

**************************************************
Stored procedure with while loop statement:
delimiter //
create procedure test9(in SNO int(11),out rev int(11))
begin
declare reverse int default 0;
while SNO!=0 do
set reverse=reverse*10;
set reverse=reverse+SNO%10;
set SNO=SNO/10;
end while;
set rev=reverse;
end
Call Stored procedure:
CALL test9(321,@rev);
SELECT @rev;
delimiter //
create procedure test10(in SNO int(11),out rev int(11))
begin
declare reverse int default 0;
while SNO!=0 do
set reverse=reverse*10;
set reverse=reverse+SNO%10;
set SNO=SNO/10;
end while;
select reverse;
end
Call Stored procedure:
CALL test9(321);
****************************************************
Stored procedure with repeat loop statement:
delimiter //
create procedure test11(delimiter //;
begin
declare reverse int default 0;
repeat
set reverse=reverse*10;
set reverse=reverse+SNO%10;
set SNO=SNO/10;
until SNO=0
end repeat;
set rev=reverse;
end
Call Stored procedure:
CALL test11(321,@rev);
SELECT @rev;
************************************************************
Stored procedure with LOOP, LEAVE and ITERATE Statements like for,break,continue
in C++,JAVA,PHP
The stored procedure only constructs string with even numbers e.g., 2, 4, 6, etc
.
delimiter //;
CREATE PROCEDURE test12()
BEGIN
DECLARE x INT;
DECLARE str VARCHAR(255);

SET x = 1;
SET str = '';
loop_label: LOOP
IF x > 10 THEN
LEAVE loop_label;
END IF;
SET x = x + 1;
IF (x mod 2) THEN
ITERATE loop_label;
ELSE
SET str = CONCAT(str,x,',');
END IF;
END LOOP;
SELECT str;
END
call test12();
*************************************************************

You might also like