This content originally appeared on DEV Community and was authored by Pranav Bakare
In Oracle PL/SQL, both normal variables and bind variables serve different purposes. Below are examples of each, showcasing their use in the context of PL/SQL.
- Normal Variable in PL/SQL
A normal variable in PL/SQL is defined within a PL/SQL block and can hold values used for computation or logic.
Example: Using Normal Variables
DECLARE
v_employee_name VARCHAR2(100); -- Normal variable
v_employee_id NUMBER := 101; -- Initializing normal variable
BEGIN
-- Fetching employee name from the database
SELECT employee_name INTO v_employee_name
FROM employees
WHERE employee_id = v_employee_id;
-- Output the employee name
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_employee_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with ID: ' || v_employee_id);
END;
Explanation:
Declaration: v_employee_name and v_employee_id are normal variables.
Usage: The SELECT statement fetches the employee name based on the employee ID stored in the normal variable v_employee_id.
Output: The result is printed using DBMS_OUTPUT.PUT_LINE.
- Bind Variable in PL/SQL
Bind variables are placeholders in SQL statements that can be used in conjunction with the EXECUTE command or when using tools that support binding (like Oracle SQL Developer).
Example: Using Bind Variables
In PL/SQL, you can use bind variables within SQL statements executed in environments like SQL*Plus, Oracle SQL Developer, or through programming interfaces like JDBC or ODBC.
Using SQL*Plus or SQL Developer:
VARIABLE emp_id NUMBER;
VARIABLE emp_name VARCHAR2(100);
-- Assign a value to the bind variable
EXEC :emp_id := 101;
-- Use the bind variable in a SQL query
SELECT employee_name INTO :emp_name
FROM employees
WHERE employee_id = :emp_id;
-- Output the employee name
PRINT emp_name;
Explanation:
Bind Variable Declaration: VARIABLE emp_id NUMBER; and VARIABLE emp_name VARCHAR2(100); declare bind variables.
Assignment: EXEC :emp_id := 101; assigns a value to the bind variable.
Usage: The SELECT statement uses the bind variable :emp_id to fetch the employee name and stores it in the bind variable :emp_name.
Output: The result is printed using the PRINT command.
Key Differences Highlighted in Examples:
Normal Variables: Declared and used directly within a PL/SQL block.
Bind Variables: Declared in a SQL*Plus or SQL Developer session, and used with a colon (:) prefix to reference them in SQL statements. They enable the reuse of SQL execution plans and improve security by preventing SQL injection.
These examples illustrate how normal and bind variables function in Oracle PL/SQL, each serving its distinct purpose in database interactions.
This content originally appeared on DEV Community and was authored by Pranav Bakare
Pranav Bakare | Sciencx (2024-10-25T04:08:57+00:00) Normal variables and bind variables in ORACLE PLSQL. Retrieved from https://www.scien.cx/2024/10/25/normal-variables-and-bind-variables-in-oracle-plsql/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.