0% found this document useful (0 votes)
9 views

Practical_Set2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Practical_Set2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Practical No 10: PL-SQL

Aim - Advanced SQL queries using Pl-SQL

Solve the following PL-Sql programs

a) Write a PL-SQL program to swap two numbers


b) Write a Pl-SQL program to find the largest of two numbers

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

Aim - Advanced SQL queries using Procedure and Function

Write the syntax of Procedure and Function

Create a procedure to print the odd numbers from 1 to 10

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:

CREATE FUNCTION function_name (parameter1 datatype, parameter2 datatype, ...)


RETURN return_datatype
AS
-- Declarations (optional)
BEGIN
-- SQL statements and PL/SQL code
-- You can use parameters and declare local variables
-- Example: -- RETURN some_value;
END;
CREATE PROCEDURE PrintOddNumbers
AS
-- Declaration of a variable to store the current number
current_number INTEGER;
BEGIN
-- Loop through numbers from 1 to 10
FOR current_number IN 1..10 LOOP
-- Check if the number is odd
IF MOD(current_number, 2) <> 0 THEN
-- Print the odd number
DBMS_OUTPUT.PUT_LINE('Odd Number: ' || current_number);
END IF;
END LOOP;
END;

Output
Practical No 12: Practice Question 1

Write a pl/sql program to find the total and average of 6 subjects and the grade

--To find total and average of 6 subjects and 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;

-- Declare variable for total

sumOf6 NUMBER;

-- Declare variable for average

avgOf6 NUMBER;

--Start Block
BEGIN

-- Assigning sum of a, b and c into sumOf3


sumOf6 := a + b + c + d + e + f;

-- Assigning average of a, b and c into avgOf3


avgOf6 := sumOf6 / 6;

--print Result total


dbms_output.Put_line('Sum = '
||sumOf6);

--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

Practical No 14: Practice Question 3

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

Consider the EMP table created in earlier practical.

-- Display the current content of EMPLOYEE table


SELECT * FROM EMPLOYEE;
-- Create a stored procedure raise_sal

DELIMITER //

CREATE PROCEDURE raise_sal(IN emp_number INT, IN salary_increase DECIMAL(10,2))

BEGIN

DECLARE current_salary DECIMAL(10, 2);


-- Get the current salary for the given employee number

SELECT SALARY INTO current_salary FROM EMPLOYEE WHERE EMPNO = emp_number;


-- Check if the employee exists

IF current_salary IS NOT NULL THEN


-- Update the salary with the increase amount
UPDATE EMPLOYEE SET SALARY = current_salary + salary_increase
WHERE EMPNO = emp_number;
-- Display a success message
SELECT CONCAT('Salary for employee ', emp_number, ' increased by ', salary_increase, '. New
salary: ',SALARY)
FROM EMPLOYEE WHERE EMPNO = emp_number;

ELSE

-- Display an error message if the employee does not exist

SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Employee does not exist';


END IF;

END //

DELIMITER ;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy