0% found this document useful (0 votes)
20 views11 pages

PRACTICAL - 11 To 20

Uploaded by

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

PRACTICAL - 11 To 20

Uploaded by

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

Enrollment No.

:- IU0000000000 DBMS(CE0317)

PRACTICAL- 11
Write a PL/SQL block to insert record into emp table. Accept value at
runtime.

DECLARE
l_emp_name VARCHAR2(250);
l_emp_no NUMBER;
l_salary NUMBER;
l_manager VARCHAR2(250);
BEGIN
INSERT INTO emp(emp_name,emp_no,salary,manager)
VALUES(‘BBB’,1000,25000,’AAA’);
INSERT INTO emp(emp_name,emp_no,salary,manager)
VALUES('XXX',1001,10000,’BBB);
INSERT INTO emp(emp_name,emp_no,salary,managed
VALUES(‘YYY',1002,10000,'BBB');
INSERT INTO emp(emp_name,emp_no,salary,manager)
VALUES(‘ZZZ',1003,7500,'BBB'):
COMMIT;
Dbms_output.put_line(‘Values Inserted');
UPDATE EMP
SET salary=15000
WHERE emp_name='XXX';
COMMIT;
Dbms_output.put_line(‘Values Updated');
DELETE emp WHERE emp_name='ZZZ';
COMMIT:
Dbms_output.put_line('Values Deleted );
SELECT emp_name,emp_no,salary,manager INTO
l_emp_name,l_emp_no,l_salary,l_manager FROM emp WHERE emp_name='XXX';

Dbms output.put line(‘Employee Detail’);


Dbms_output.put_line(‘Employee Name:‘||l_emp_name);
Dbms_output.put_line(‘Employee Number:‘||l_emp_no);
Dbms_output.put_line(‘Employee Salary:‘||l_salary);
Dbms output.put line(‘Emplovee Manager Name:‘||l_manager):
END;
/

Output :

Page No. 1
Enrollment No. :- IU0000000000 DBMS(CE0317)

PRACTICAL- 12
Write a PL/SQL block to reserve a given number.
DECLARE
num NUMBER;
rev NUMBER;

BEGIN
num:=#
rev:=0;
WHILE num>0 LOOP
rev:=(rev*10) + mod(num,10);
num:=floor(num/10);
END LOOP;
DBMS_OUTPUT.PUT_LINE('Reverse of the number is: ' || rev);
END;
/

Output :

Page No. 2
Enrollment No. :- IU0000000000 DBMS(CE0317)

PRACTICAL- 13
Write a PL/SQL block to check if a given num is odd or even.

declare
n number:=&n;

begin
if mod(n,2)=0
then
dbms_output.put_line('The given number is even');
else
dbms_output.put_line('The given number is odd');
end if;
end;
/

Output :

Page No. 3
Enrollment No. :- IU0000000000 DBMS(CE0317)

PRACTICAL- 14
Write a PL/SQL block to accept id of employee (emp table) from user and
fetch a record of that employee. Check the salary and update the salary
Column as follows:
a. If salary >10000 and salary<=20000, then salary = salary+30% of salary.
b. If salary>20000 and salary<=30000, then salary =salary+ 40% of salary.

SET SERVEROUTPUT ON
DECLARE
v_employee_id emp.emp_id%TYPE;
v_employee_salary emp.salary%TYPE;
BEGIN
-- Accept employee ID from the user
v_employee_id := &emp_id; -- The '&' symbol is used to accept
user input

-- Fetch the salary of the employee


SELECT salary INTO v_employee_salary
FROM emp
WHERE emp_id = v_employee_id;

-- Check and update the salary based on conditions


IF v_employee_salary > 10000 AND v_employee_salary <= 20000 THEN
v_employee_salary := v_employee_salary + (0.3 *
v_employee_salary);
ELSIF v_employee_salary > 20000 AND v_employee_salary <= 30000
THEN
v_employee_salary := v_employee_salary + (0.4 *
v_employee_salary);
END IF;

-- Update the employee's salary


UPDATE emp
SET salary = v_employee_salary
WHERE emp_id = v_employee_id;

-- Commit the transaction


COMMIT;

DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_employee_id);


DBMS_OUTPUT.PUT_LINE('New Salary: ' || v_employee_salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);

Page No. 4
Enrollment No. :- IU0000000000 DBMS(CE0317)

END;
/

Page No. 5
Enrollment No. :- IU0000000000 DBMS(CE0317)

PRACTICAL- 15
Write a PL/SQL block that will display the information Of the first 5
employees holding the highest salary of the emp table.

SET SERVEROUTPUT ON;

DECLARE
-- Declare a cursor to select the top 5 employees with the
highest salary
CURSOR top_employees_cursor IS
SELECT empno, ename, sal
FROM emp
ORDER BY sal DESC
FETCH FIRST 5 ROWS ONLY;

-- Declare variables to store employee information


v_empno emp.empno%TYPE;
v_ename emp.ename%TYPE;
v_salary emp.sal%TYPE;

BEGIN
-- Open the cursor
OPEN top_employees_cursor;

-- Fetch and display the information of the top 5 employees with


the highest salary
DBMS_OUTPUT.PUT_LINE('Top 5 Employees with Highest Salary:');
DBMS_OUTPUT.PUT_LINE('---------------------------------');
LOOP
FETCH top_employees_cursor INTO v_empno, v_ename, v_salary;
EXIT WHEN top_employees_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Employee ' || v_empno || ': ' || v_ename
|| ', Salary: ' || v_salary);
END LOOP;

-- Close the cursor


CLOSE top_employees_cursor;
END;
/
Output :

Page No. 6
Enrollment No. :- IU0000000000 DBMS(CE0317)

PRACTICAL- 16
Write a PL/SQL block that merge ft_work and pt_work. Fetch name of emp
from pt_work, check if it is in ft_work. If it is not there then insert that record
in ft_work, otherwise display appropriate message.

DECLARE
v_emp_name pt_work.emp_name%TYPE;
BEGIN
FOR pt_rec IN (SELECT emp_name FROM pt_work) LOOP
v_emp_name := pt_rec.emp_name;

-- Check if the emp_name exists in ft_work


-- If not, insert the record, otherwise display a message
MERGE INTO ft_work ft
USING (SELECT v_emp_name AS emp_name FROM DUAL) pt
ON (ft.emp_name = pt.emp_name)
WHEN NOT MATCHED THEN
INSERT (emp_name)
VALUES (v_emp_name);

IF SQL%ROWCOUNT = 1 THEN
DBMS_OUTPUT.PUT_LINE('Record for ' || v_emp_name || '
inserted into ft_work.');
ELSE
DBMS_OUTPUT.PUT_LINE('Record for ' || v_emp_name || '
already exists in ft_work.');
END IF;
END LOOP;
END;
/

Page No. 7
Enrollment No. :- IU0000000000 DBMS(CE0317)

PRACTICAL- 17
Create trigger on supplier table which allow access between 9 AM to 5 PM
only.

CREATE PROCEDURE AccessSupplierData


AS
BEGIN
DECLARE @CurrentTime TIME = GETDATE();

IF @CurrentTime >= '09:00:00' AND @CurrentTime <= '17:00:00'


BEGIN
-- Access the supplier table
SELECT * FROM Supplier;
END
ELSE
BEGIN
-- Handle access outside of the allowed time frame
PRINT 'Access to supplier data is only allowed between 9
AM and 5 PM.';
END
END;

Page No. 8
Enrollment No. :- IU0000000000 DBMS(CE0317)

PRACTICAL- 18
Create trigger on Supplier Detail on update or insert of Sname to convert
Sname into capital letter.

CREATE TRIGGER CapitalizeSname


ON [Supplier Detail]
AFTER INSERT, UPDATE
AS
BEGIN
-- Update 'Sname' to uppercase for the newly inserted records
UPDATE [Supplier Detail]
SET [Sname] = UPPER(i.[Sname])
FROM inserted AS i
WHERE [Supplier Detail].[SupplierID] = i.[SupplierID];
END;

UPDATE [Supplier Detail]


SET [Sname] = 'newSupplier'
WHERE [SupplierID] = 1;

Output :

Page No. 9
Enrollment No. :- IU0000000000 DBMS(CE0317)

PRACTICAL- 19
Create a stored procedure which accepts CNUM from the user and print that
order is placed by that customer or not. Also write a PL/SQL block for using
this procedure.

CREATE OR REPLACE PROCEDURE CheckOrderPlacement (p_CNUM NUMBER) AS


v_OrderCount NUMBER;
BEGIN
-- Count the number of orders for the given customer number
SELECT COUNT(*) INTO v_OrderCount
FROM Orders
WHERE CustomerNumber = p_CNUM;

-- Check if any orders were placed by the customer


IF v_OrderCount > 0 THEN
DBMS_OUTPUT.PUT_LINE('Order placed by customer ' || p_CNUM);
ELSE
DBMS_OUTPUT.PUT_LINE('No order placed by customer ' ||
p_CNUM);
END IF;
END CheckOrderPlacement;
/

DECLARE
v_CustomerNumber NUMBER := 12345; -- Replace with the desired
customer number
BEGIN
CheckOrderPlacement(v_CustomerNumber);
END;
/

Page No. 10
Enrollment No. :- IU0000000000 DBMS(CE0317)

PRACTICAL- 20
Create a database

DECLARE
v_table_exists NUMBER;
BEGIN
-- Check if the table already exists
SELECT COUNT(*)
INTO v_table_exists
FROM user_tables
WHERE table_name = 'Employees';

-- Create the table if it doesn't exist


IF v_table_exists = 0 THEN
EXECUTE IMMEDIATE 'CREATE TABLE Employees (
EmployeeID NUMBER,
FirstName VARCHAR2(50),
LastName VARCHAR2(50),
Salary NUMBER
)';
DBMS_OUTPUT.PUT_LINE('Table "Employees" created
successfully.');
ELSE
DBMS_OUTPUT.PUT_LINE('Table "Employees" already exists.');
END IF;
END;
/

Page No. 11

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