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

PL SQL Answers

The document contains a series of PL/SQL code snippets demonstrating various programming concepts such as outputting messages, finding the maximum number, calculating factorials, checking for prime numbers, date formatting, and handling cursors. It also includes the creation of a table and a trigger for customer records, as well as a function to return the maximum of two numbers. Overall, it showcases basic database operations and control structures in PL/SQL.

Uploaded by

anagha16nair
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)
2 views

PL SQL Answers

The document contains a series of PL/SQL code snippets demonstrating various programming concepts such as outputting messages, finding the maximum number, calculating factorials, checking for prime numbers, date formatting, and handling cursors. It also includes the creation of a table and a trigger for customer records, as well as a function to return the maximum of two numbers. Overall, it showcases basic database operations and control structures in PL/SQL.

Uploaded by

anagha16nair
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/ 4

Q1

BEGIN
DBMS_OUTPUT.PUT_LINE('Welcome Subodh');
END;
/

Q2

DECLARE
a NUMBER := 10;
b NUMBER := 25;
c NUMBER := 15;
max NUMBER;
BEGIN
max := a;
IF b > max THEN
max := b;
END IF;
IF c > max THEN
max := c;
END IF;
DBMS_OUTPUT.PUT_LINE('Greatest number is ' || max);
END;
/

Q3

DECLARE
num NUMBER := 5;
fact NUMBER := 1;
i NUMBER;
BEGIN
FOR i IN 1..num LOOP
fact := fact * i;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Factorial is ' || fact);
END;
/

Q4

DECLARE
num NUMBER := 17;
i NUMBER := 2;
flag BOOLEAN := TRUE;
BEGIN
IF num <= 1 THEN
flag := FALSE;
ELSE
WHILE i <= num / 2 LOOP
IF MOD(num, i) = 0 THEN
flag := FALSE;
EXIT;
END IF;
i := i + 1;
END LOOP;
END IF;

IF flag THEN
DBMS_OUTPUT.PUT_LINE(num || ' is a Prime number');
ELSE
DBMS_OUTPUT.PUT_LINE(num || ' is NOT a Prime number');
END IF;
END;
/

Q5

DECLARE
my_date DATE := TO_DATE('2025-04-21', 'YYYY-MM-DD');
BEGIN
DBMS_OUTPUT.PUT_LINE('Day is: ' || TO_CHAR(my_date, 'Day'));
END;
/

Q6

DECLARE
i NUMBER := 1;
BEGIN
WHILE i <= 10 LOOP
DBMS_OUTPUT.PUT_LINE('Multiple: ' || (2 * i));
i := i + 1;
END LOOP;
END;
/

Q7

BEGIN
FOR i IN 1..50 LOOP
IF MOD(i, 2) = 1 THEN
DBMS_OUTPUT.PUT_LINE(i);
END IF;
END LOOP;
END;
/

Q8

DECLARE
fahrenheit NUMBER := 98;
celsius NUMBER := (fahrenheit - 32) * 5 / 9;
BEGIN
DBMS_OUTPUT.PUT_LINE('Celsius: ' || celsius);
fahrenheit := (celsius * 9 / 5) + 32;
DBMS_OUTPUT.PUT_LINE('Fahrenheit: ' || fahrenheit);
END;
/

Q9

CREATE OR REPLACE FUNCTION get_max(x NUMBER, y NUMBER)


RETURN NUMBER
IS
BEGIN
IF x > y THEN
RETURN x;
ELSE
RETURN y;
END IF;
END;
/

BEGIN
DBMS_OUTPUT.PUT_LINE('Max: ' || get_max(10, 20));
END;
/

Q10

CREATE TABLE customers (


id NUMBER(3) PRIMARY KEY,
name VARCHAR2(20),
salary NUMBER(5)
);

CREATE OR REPLACE TRIGGER trg_insert_customer


BEFORE INSERT ON customers
FOR EACH ROW
BEGIN
DBMS_OUTPUT.PUT_LINE('New customer inserted: ' || :NEW.name);
END;
/

Q11

DECLARE
CURSOR cust_cursor IS SELECT * FROM customers;
cust_row customers%ROWTYPE;
BEGIN
OPEN cust_cursor;
LOOP
FETCH cust_cursor INTO cust_row;
EXIT WHEN cust_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('ID: ' || cust_row.id || ', Name: ' || cust_row.name || ',
Salary: ' || cust_row.salary);
END LOOP;
CLOSE cust_cursor;
END;
/
Q12

DECLARE
v_id customers.id%TYPE := &Enter_ID;
v_name customers.name%TYPE;
BEGIN
SELECT name INTO v_name FROM customers WHERE id = v_id;
DBMS_OUTPUT.PUT_LINE('Customer Name: ' || v_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'invalid_id');
END;
/

Q13

DECLARE
CURSOR cust_cursor IS SELECT * FROM customers;
cust_rec customers%ROWTYPE;
BEGIN
OPEN cust_cursor;
LOOP
FETCH cust_cursor INTO cust_rec;
EXIT WHEN cust_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Customer: ' || cust_rec.name);
END LOOP;
CLOSE cust_cursor;
END;
/

Q14

CREATE TABLE customers_backup AS SELECT * FROM customers WHERE 1=0;

BEGIN
INSERT INTO customers_backup SELECT * FROM customers;
DBMS_OUTPUT.PUT_LINE('Records inserted into customers_backup');
END;
/

You might also like