EXECUTE IMMEDIATE in Oracle SQL

mrcaption49

Pranav Bakare

Posted on October 21, 2024

EXECUTE IMMEDIATE in Oracle SQL

In Oracle SQL, EXECUTE IMMEDIATE is primarily used in PL/SQL blocks to execute dynamic SQL statements. This feature allows for greater flexibility, as you can construct and run SQL commands that are determined during the execution of the program, rather than hardcoding them.

What is EXECUTE IMMEDIATE?

In Oracle, EXECUTE IMMEDIATE allows you to run a dynamically constructed SQL statement or PL/SQL block. It can be used for:

  1. Data Definition Language (DDL) statements (like CREATE, ALTER, DROP), which cannot be directly executed within a PL/SQL block.

  2. Data Manipulation Language (DML) statements (SELECT, INSERT, UPDATE, DELETE) that need to be constructed dynamically.

  3. Anonymous PL/SQL blocks when you want to execute them dynamically.

Syntax of EXECUTE IMMEDIATE

EXECUTE IMMEDIATE dynamic_sql_string
[INTO {variable[, variable]...}] -- Used for SELECT statements to fetch values
[USING {bind_argument[, bind_argument]...}] -- Bind variables for input/output
[RETURNING INTO {variable[, variable]...}]; -- Retrieve values from DML operations

Example 1: Executing a DDL Statement

Oracle doesn't allow direct DDL execution (like CREATE TABLE) inside PL/SQL blocks, but you can bypass this limitation using EXECUTE IMMEDIATE:

BEGIN
EXECUTE IMMEDIATE 'CREATE TABLE test_table (id NUMBER, name VARCHAR2(50))';
DBMS_OUTPUT.PUT_LINE('Table created successfully.');
END;
/

Explanation:

This block dynamically creates a table named test_table. The SQL statement is passed as a string, and EXECUTE IMMEDIATE runs it during execution.

Example 2: Executing a DML Statement with USING

You can pass variables to dynamically constructed DML statements. For example, inserting data into the EMPLOYEES table:

DECLARE
emp_id NUMBER := 1004;
emp_name VARCHAR2(50) := 'Alex';
emp_salary NUMBER := 65000;
BEGIN
EXECUTE IMMEDIATE 'INSERT INTO employees (employee_id, first_name, salary) VALUES (:1, :2, :3)'
USING emp_id, emp_name, emp_salary;
DBMS_OUTPUT.PUT_LINE('Employee inserted successfully.');
END;
/

Explanation:

The placeholders :1, :2, and :3 in the SQL statement are substituted with emp_id, emp_name, and emp_salary respectively, using the USING clause.

Example 3: Executing a SELECT Statement with INTO

You can use EXECUTE IMMEDIATE with INTO to fetch values from a dynamically created SELECT statement:

Sample Data:

Assuming your EMPLOYEES table contains the following data:

Code Example:

DECLARE
emp_name VARCHAR2(50);
emp_salary NUMBER;
emp_id NUMBER := 1001;
BEGIN
EXECUTE IMMEDIATE 'SELECT first_name, salary FROM employees WHERE employee_id = :1'
INTO emp_name, emp_salary
USING emp_id;

DBMS_OUTPUT.PUT_LINE('Employee: ' || emp_name || ', Salary: ' || emp_salary);
Enter fullscreen mode Exit fullscreen mode

END;
/

Explanation:

This code dynamically retrieves the FIRST_NAME and SALARY of the employee with EMPLOYEE_ID 1001.

The INTO clause stores the fetched data into emp_name and emp_salary.

The USING clause binds the emp_id variable to the placeholder :1 in the SQL string.

Example 4: Using RETURNING INTO for DML

When using DML statements like INSERT, UPDATE, or DELETE, you can capture output values using RETURNING INTO:

DECLARE
new_emp_id NUMBER;
BEGIN
EXECUTE IMMEDIATE 'INSERT INTO employees (employee_id, first_name, salary)
VALUES (employees_seq.NEXTVAL, ''Chris'', 80000) RETURNING employee_id INTO :1'
USING OUT new_emp_id;

DBMS_OUTPUT.PUT_LINE('New Employee ID: ' || new_emp_id);
Enter fullscreen mode Exit fullscreen mode

END;
/

Explanation:

This block inserts a new row into the EMPLOYEES table, dynamically using a sequence for EMPLOYEE_ID.

The RETURNING INTO clause captures the newly generated EMPLOYEE_ID and stores it in the new_emp_id variable.

The USING OUT clause binds new_emp_id as an output variable.

Advantages of EXECUTE IMMEDIATE:

  1. Flexibility: Allows for SQL commands to be dynamically created at runtime.

  2. Dynamic DDL Execution: Enables running DDL commands from within PL/SQL, which isn’t possible directly.

  3. Efficient Use of Variables: Bind variables in USING help in passing inputs to dynamic SQL, improving readability and security.

Key Points to Remember:

  1. Security: Always use bind variables (placeholders :1, :2, etc.) with USING to prevent SQL injection.

  2. Performance: Dynamic SQL incurs additional parsing overhead. Use it judiciously.

  3. Error Handling: Since the dynamic SQL statement is constructed at runtime, always include appropriate error handling to catch and debug issues.

Conclusion:

EXECUTE IMMEDIATE in Oracle SQL is a powerful tool for executing SQL statements dynamically, whether they are DDL, DML, or SELECT queries. It provides a way to write more flexible and adaptable PL/SQL programs that can handle varying requirements and inputs at runtime.

💖 💪 🙅 🚩
mrcaption49
Pranav Bakare

Posted on October 21, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

EXECUTE IMMEDIATE in Oracle SQL
opensource EXECUTE IMMEDIATE in Oracle SQL

October 21, 2024