Practical_Set2
Practical_Set2
a)
DECLARE
--Declare variables num1, num2, and temp of datatype number
num1 NUMBER;
num2 NUMBER;
temp NUMBER;
BEGIN
-- Assign values to num1 and num2
num1 := 1000;
num2 := 2000;
-- Print result before swapping
DBMS_OUTPUT.PUT_LINE('Before');
DBMS_OUTPUT.PUT_LINE('num1 = ' || num1 || ' num2 = ' || num2);
-- Swapping of numbers num1 and num2
temp := num1;
num1 := num2;
num2 := temp;
-- Print result after swapping
DBMS_OUTPUT.PUT_LINE('After');
DBMS_OUTPUT.PUT_LINE('num1 = ' || num1 || ' num2 = ' || num2);
END;
/
Output
b)
-- First block
DECLARE
a NUMBER;
b NUMBER;
BEGIN
a := &a;
b := &b;
IF a > b THEN
DBMS_OUTPUT.PUT_LINE('Max number is ' || a);
ELSE
DBMS_OUTPUT.PUT_LINE('Max number is ' || b);
END IF;
END;
/
-- Second block
DECLARE
a NUMBER;
b NUMBER;
BEGIN
a := &a;
b := &b;
IF a > b THEN
DBMS_OUTPUT.PUT_LINE('Max number is ' || a);
ELSE
DBMS_OUTPUT.PUT_LINE('Max number is ' || b);
END IF;
END;
/
Output
Practical No 11: Procedure and Functions
Procedures:
A Procedure is a named PL/SQL block which performs one or more specific task. This
is similar to a procedure in other programming languages. A procedure has a header
and a body. The header consists of the name of the procedure and the parameters or
variables passed to the procedure. The body consists or declaration section, execution
section and exception section similar to a general PL/SQL Block. A procedure is similar
to an anonymous PL/SQL Block but it is named for repeated usage.
Procedure Syntax:
CREATE PROCEDURE procedure_name (parameter1 datatype, parameter2 datatype, ...)
AS
-- Declarations (optional)
BEGIN
-- SQL statements and PL/SQL code
-- You can use parameters and declare local variables
-- Example:
-- DBMS_OUTPUT.PUT_LINE('Hello from the procedure!');
END;
Functions:
A function is a named PL/SQL Block which is similar to a procedure. The major
difference between a procedure and a function is, a function must always return a value,
but a procedure may or may not return a value.
Function Syntax:
Output
Practical No 12: Practice Question 1
Write a pl/sql program to find the total and average of 6 subjects and the grade
DECLARE
-- Assigning 12 into a
a NUMBER := 12;
-- Assigning 14 into b
b NUMBER := 14;
-- Assigning 20 into c
c NUMBER := 20;
-- Assigning 12 into d
d NUMBER := 12;
-- Assigning 14 into e
e NUMBER := 14;
-- Assigning 20 into f
f NUMBER := 20;
sumOf6 NUMBER;
avgOf6 NUMBER;
--Start Block
BEGIN
--print Average
dbms_output.Put_line('Average = '
||avgOf6);
END;
Practical No 13: Practice Question 2
Write a procedure to calculate the total for all students and pass regno, mark1 and mark2 as
arguments
Write a procedure raise_sal which increases the salary of an employee. It accepts an employee
number and salary increase amount. It uses the employee number to find the current salary from the
employee table and update the salary
DELIMITER //
BEGIN
ELSE
END //
DELIMITER ;