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

PLSQL Lab Programs-Dbms

Important plsql notebook

Uploaded by

ameefa703
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)
17 views

PLSQL Lab Programs-Dbms

Important plsql notebook

Uploaded by

ameefa703
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

9.

WRITE PL/SQL PROCEDURE FOR AN APPLICATION USING


EXCEPTION HANDLING

SQL> BEGIN
2 DECLARE
3 numerator NUMBER := 10;
4 denominator NUMBER := 0;
5 result NUMBER;
6 BEGIN
7 result:= numerator / denominator;
8 DBMS_OUTPUT.PUT_LINE('Result: ' || result);
9 EXCEPTION
10 WHEN ZERO_DIVIDE THEN
11 DBMS_OUTPUT.PUT_LINE('Error: Division by zero');
12 END;
13 END;
14 /

Output:
Error: Division by zero

PL/SQL procedure successfully completed.


10. WRITE PL/SQL PROCEDURE FOR AN APPLICATION USING
CURSORS
DECLARE
total_rows number(2);
BEGIN
UPDATE EMP
SET SAL=SAL+500;
IF sql%notfound THEN
dbms_output.put_line('no customers selected');
ELSIF sql%found THEN
total_rows:=sql%rowcount;
dbms_output.put_line(total_rows||'customers');
END IF;
END;
/

Output:

14customers

PL/SQL procedure successfully completed.


11. WRITE PL/SQL PROCEDURE FOR AN APPLICATION USING
FUNCTION

SQL> CREATE OR REPLACE FUNCTION factorial(x NUMBER)


2 RETURN NUMBER
3 IS
4 f NUMBER;
5 BEGIN
6 IF x = 0 THEN
7 f := 1;
8 ELSE
9 f := x * factorial(x - 1);
10 END IF;
11 RETURN f;
12 END;
13 /

OUTPUT:

Function created.
SQL> DECLARE
2 num NUMBER;
3 result NUMBER;
4 BEGIN
5 num := 5;
6 result := factorial(num);
7 DBMS_OUTPUT.PUT_LINE('Factorial of ' || num || ' is ' || result);
8 END;
9 /

OUTPUT:

Factorial of 5 is 120

PL/SQL procedure successfully completed.

12. WRITE PL/SQL PROCEDURE FOR AN APPLICATION USING


PACKAGE
SQL> CREATE OR REPLACE PACKAGE my_package AS
2 PROCEDURE my_procedure(p_param1 NUMBER);
3 FUNCTION calculate_sum(x NUMBER, y NUMBER) RETURN
NUMBER;
4 -- Other declarations...
5 END my_package;
6 /
OUTPUT:

Package created.

SQL> DECLARE
2 -- Declare variables to store results
3 sum_result NUMBER;
4 product_result NUMBER;
5 BEGIN
6 -- Call the procedure and pass output parameter
7 math_operations.add_numbers(5, 7, sum_result);
8 -- Display the result of the add_numbers procedure
9 DBMS_OUTPUT.PUT_LINE('Sum Result: ' || sum_result);
10
11 -- Call the function and retrieve the result
12 product_result := math_operations.multiply_numbers(3, 4);
13 -- Display the result of the multiply_numbers function
14 DBMS_OUTPUT.PUT_LINE('Product Result: ' || product_result);
15 END;
16 /
OUTPUT:
Sum Result: 12
Product Result: 12

PL/SQL procedure successfully completed.

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