PLSQL_Lab_8
PLSQL_Lab_8
It can
be invoked by triggers, other stored procedures and applications susch as java, PHP and python
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;
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(,)
Delimiter //
create procedure getemployee()
begin
select * from employee;
end //
delimiter ;
call getemployee();
To find the record of a particular employee
delimiter //
delimeter ;
call getemployee1(10);
To add two numbers
DELIMITER //
DELIMITER ;
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