Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

PLSQL_Lab_8

The document provides an overview of stored procedures in a database, detailing their structure, syntax, and usage. It explains the different parameter modes (in, out, inout) and includes examples for creating procedures to perform various tasks such as retrieving employee records, adding numbers, calculating sums, inverting numbers, and maintaining a counter. Additionally, it discusses the use of delimiters to manage the execution of stored procedures in MySQL.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

PLSQL_Lab_8

The document provides an overview of stored procedures in a database, detailing their structure, syntax, and usage. It explains the different parameter modes (in, out, inout) and includes examples for creating procedures to perform various tasks such as retrieving employee records, adding numbers, calculating sums, inverting numbers, and maintaining a counter. Additionally, it discusses the use of delimiters to manage the execution of stored procedures in MySQL.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Stored Procedure : It is a segment or group of statements which stored inside the db catalog.

It can
be invoked by triggers, other stored procedures and applications susch as java, PHP and python

DROP PROCEDURE IF EXISTS AddTwoNumbers;


DROP PROCEDURE IF EXISTS getemployee;
DROP PROCEDURE IF EXISTS getemployee1;
delimiter // Such statement changes from standard delimiter; to //

Delimiter is used to pass the stored procedure to the server as a whole rather than mysql tool to
interpret each statement at a time

begin and end has the section called body of the stored procedure. Business logic can be embedded
within begin and end statement.

Declaration of variable
declare x,y int default 0;

To assign value to a variable


set x = 10; or through statement like
select count(*) into tot_count from employee; where tot_count is a variable.
Parameters: It has one of three modes in, out or inout
in: It is the default mode. In this, a copy of such variable is passed to the stored procedure. If there is change in the
value of such variable then there will be no change in the original variable.

out: Its value can be changed inside the stored procedure and its new value is passed back to the calling program. In
this stored procedure can not access the initial value of the out parameter when it starts.

inout: It is the combination of in and out parameters. It means that the calling program may pass the argument,
and the stored procedure can modify the inout parameter and pass the new value back to the calling program.
Syntax: mode parameter name datatype(size) and each parameter can be separated by a comma(,)

To find all the records of employees

Delimiter //
create procedure getemployee()
begin
select * from employee;
end //
delimiter ;

call getemployee();
To find the record of a particular employee

delimiter //

create procedure getemployee1(in id varchar(40))


begin
select * from employee where eid = id;
end //

delimeter ;

call getemployee1(10);
To add two numbers

DELIMITER //

CREATE PROCEDURE AddTwoNumbers(IN num1 INT, IN num2 INT)


BEGIN
DECLARE
sum_result INT; -- Calculate the sum SET sum_result = num1 + num2;
-- Display the result
SELECT CONCAT('The sum of ', num1, ' and ', num2, ' is: ', sum_result) AS Result;
END //

DELIMITER ;

CALL AddTwoNumbers(5, 7);


To calculate the sum upto 100 numbers

DELIMITER //
CREATE PROCEDURE SumNumbers()
BEGIN
DECLARE counter INT DEFAULT 1;
DECLARE total_sum INT DEFAULT 0;
-- Loop from 1 to 100
WHILE counter <= 100 DO
SET total_sum = total_sum + counter;
SET counter = counter + 1;
END WHILE;
-- Display the result
SELECT total_sum AS Result;
END //
DELIMITER ;
CALL SumNumbers();
To invert number
DELIMITER //
CREATE PROCEDURE InvertNumber(IN original_number INT)
BEGIN
DECLARE inverted_number INT DEFAULT 0;
DECLARE temp_number INT;
-- Display the original number
SELECT CONCAT('Original Number: ', original_number) AS Result;
-- Invert the number
SET temp_number := original_number;
WHILE temp_number > 0 DO
SET inverted_number = inverted_number * 10 + temp_number % 10;
SET temp_number = temp_number DIV 10;
END WHILE;
-- Display the inverted number
SELECT CONCAT('Inverted Number: ', inverted_number) AS Result;
END //
DELIMITER ;
CALL InvertNumber(5639);
To make a counter

DROP PROCEDURE IF EXISTS set_counter;


delimiter $$
create procedure set_counter(inout count int, in inc int)
Begin
set count =count + inc ;
end $$
delimiter ;
set @counter = 1;
call set_counter(@counter,1);
call set_counter(@counter,5);
select @counter;
select @counter;

You might also like