Functions in PLSQL
In PL/SQL (Procedural Language/Structured Query Language), functions are subprograms that can be stored and executed on the database server. They allow you to perform specific tasks, typically returning a single value. Functions are similar to procedures, but they differ primarily in that a function returns a value while a procedure does not.
Key Features of Functions in PL/SQL:
Return Type:
Functions must have a return type. This can be any valid PL/SQL data type.
Syntax: The general syntax for creating a function is:
CREATE [OR REPLACE] FUNCTION function_name
[ (parameter_list) ]
RETURN return_datatype
IS | AS
[declaration_section]
BEGIN
executable_section
RETURN return_value;
EXCEPTION
exception_section
END [function_name];
Parameter Modes:
IN: The parameter can be read, but not modified.
OUT: The parameter can be written to, but not read.
IN OUT: The parameter can be read and written to.
OUT: The parameter can be written to, but not read.
IN OUT: The parameter can be read and written to.
Example of a Simple PL/SQL Function
CREATE OR REPLACE FUNCTION hello_user
CREATE OR REPLACE FUNCTION hello_user
(user_name IN VARCHAR2)
RETURN VARCHAR2
IS
BEGIN
RETURN 'Hello ' || user_name;
END hello_user;
/
BEGIN
dbms_output.put_line(hello_user('Peter'));
END;
/
Example of a Simple PL/SQL Function
Here is an example of a simple PL/SQL function that calculates the square of a number:
CREATE OR REPLACE FUNCTION calculate_square (number IN NUMBER)
RETURN NUMBER
IS
result NUMBER;
BEGIN
result := number * number;
RETURN result;
END;
/
Usage of the Function
Once the function is created, you can use it in PL/SQL blocks, SQL statements, or even in other functions or procedures. For example:
DECLARE
num NUMBER := 5;
square NUMBER;
BEGIN
square := calculate_square(num);
DBMS_OUTPUT.PUT_LINE('The square of ' || num || ' is ' || square);
END;
/Key Points:
- Scalability: Functions help modularize your code, making it more manageable and reusable.
- Performance: They can improve performance by reducing the amount of code sent to the server.
- Maintainability: Functions make your code easier to maintain by isolating functionality.
- Return Type: Functions return a single value, while procedures do not.
- Usage: Functions can be used in SQL statements, whereas procedures cannot.
- Return Clause: Functions must have a RETURN clause, whereas procedures do not.
Understanding and using functions in PL/SQL effectively can help in creating robust and efficient database applications.
Example: factorial of a number
CREATE OR REPLACE FUNCTION factorial (n IN NUMBER)
RETURN NUMBER
IS
result NUMBER := 1; -- Initialize result to 1
BEGIN
IF n < 0 THEN
RETURN NULL; -- Return null for negative numbers (not defined)
ELSIF n = 0 OR n = 1 THEN
RETURN 1; -- Return 1 for factorial of 0 or 1
ELSE
FOR i IN 1..n LOOP
result := result * i; -- Calculate factorial
END LOOP;
RETURN result; -- Return the factorial value
END IF;
END;
/
DECLARE
num NUMBER := 5;
fact NUMBER;
BEGIN
fact := factorial(num);
DBMS_OUTPUT.PUT_LINE('The factorial of ' || num || ' is ' || fact);
END;
/
You can use PL/SQL functions inside SQL queries to enhance the functionality of your queries. Functions can be called in the SELECT clause, the WHERE clause, or any place where an expression is allowed. Here’s an example to illustrate this.
Example: Using a Function Inside a SQL Query
Let's create a function that calculates the square of a number, and then use this function in a SQL query.
CREATE OR REPLACE FUNCTION sqr(num IN NUMBER)
RETURN NUMBER
IS
result NUMBER:=1;
BEGIN
result:=num*num;
RETURN result;
END;
/
CREATE TABLE numbers (
num NUMBER
);
INSERT INTO numbers (num) VALUES (1);
INSERT INTO numbers (num) VALUES (2);
INSERT INTO numbers (num) VALUES (3);
INSERT INTO numbers (num) VALUES (4);
INSERT INTO numbers (num) VALUES (5);
COMMIT;
SELECT num, sqr(num) AS square
FROM numbers;
/
Comments
Post a Comment