Sure! Here's an example of a PL/SQL program that demonstrates functions:
```sql
-- Create a function to calculate the factorial of a number
CREATE OR REPLACE FUNCTION factorial(n NUMBER) RETURN NUMBER IS
result NUMBER := 1;
BEGIN
IF n > 0 THEN
FOR i IN 1..n LOOP
result := result * i;
END LOOP;
END IF;
RETURN result;
END;
/
-- Test the factorial function
DECLARE
num NUMBER := 5;
fact NUMBER;
BEGIN
fact := factorial(num);
DBMS_OUTPUT.PUT_LINE('Factorial of ' || num || ' is ' || fact);
END;
/
```
This program creates a function called `factorial` that takes a number as input and returns its factorial. It then tests the function by calculating the factorial of 5 and displaying the result using `DBMS_OUTPUT.PUT_LINE`.
To run this program, you can use a PL/SQL development tool like Oracle SQL Developer or SQL*Plus. Simply copy and paste the code into the tool and execute it.
The output of this program should be:
```
Factorial of 5 is 120
```
Write a PL/SQL program to demonstrate Functions
1 answer