Govur University Logo
--> --> --> -->
...

How can you execute SQL statements within a PL/SQL block?



In PL/SQL (Procedural Language/Structured Query Language), you can execute SQL statements within a PL/SQL block using various constructs and techniques. Here is an in-depth explanation of how you can execute SQL statements within a PL/SQL block:

1. SQL Statements within PL/SQL Block:
You can directly include SQL statements within the body of a PL/SQL block. SQL statements are typically used to interact with the database, retrieve or manipulate data, and perform various database operations. Here's an example of executing a SQL SELECT statement within a PL/SQL block:

```
sql`DECLARE
-- Declare variables to hold result data
emp_name VARCHAR2(100);
emp_salary NUMBER;

BEGIN
-- Execute SQL SELECT statement
SELECT employee_name, salary INTO emp_name, emp_salary
FROM employees
WHERE employee_id = 100;

-- Process the retrieved data
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_name);
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || emp_salary);
END;
/`
```
2. Implicit Cursor:
PL/SQL provides an implicit cursor, which is a default cursor that allows you to process query results without explicitly declaring a cursor variable. The implicit cursor is used when executing SQL statements that return a single row or a single value. Here's an example of using the implicit cursor to execute a SQL SELECT statement:

```
sql`DECLARE
-- Declare variables to hold result data
emp_name employees.employee_name%TYPE;
emp_salary employees.salary%TYPE;

BEGIN
-- Execute SQL SELECT statement
SELECT employee_name, salary INTO emp_name, emp_salary
FROM employees
WHERE employee_id = 100;

-- Process the retrieved data
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_name);
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || emp_salary);
END;
/`
```
3. Explicit Cursor:
Explicit cursors are used when executing SQL statements that return multiple rows or when you need more control over the processing of query results. Explicit cursors require declaring a cursor variable, opening the cursor, fetching the rows, and closing the cursor. Here's an example of using an explicit cursor to execute a SQL SELECT statement:

```
sql`DECLARE
-- Declare cursor variable
CURSOR emp_cursor IS
SELECT employee_name, salary
FROM employees
WHERE department_id = 10;

-- Declare variables to hold result data
emp_name employees.employee_name%TYPE;
emp_salary employees.salary%TYPE;

BEGIN
-- Open the cursor
OPEN emp_cursor;

-- Fetch rows from the cursor
LOOP
FETCH emp_cursor INTO emp_name, emp_salary;
EXIT WHEN emp_cursor%NOTFOUND;

-- Process the retrieved data
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_name);
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || emp_salary);
END LOOP;

-- Close the cursor
CLOSE emp_cursor;
END;
/`
```
4. Dynamic SQL:
Dynamic SQL allows you to construct SQL statements at runtime and execute them within a PL/SQL block. It provides flexibility in generating dynamic queries or when the SQL statement needs to change based on certain conditions or inputs. Dynamic SQL involves using the EXECUTE IMMEDIATE or OPEN-FOR statements. Here's an example of executing a dynamic SQL SELECT statement:

```
sql`DECLARE
-- Declare variables to hold result data
emp_name VARCHAR2(100);
emp_salary NUMBER;

-- Declare SQL statement
sql_stmt VARCHAR2(200);

BEGIN
-- Construct the SQL statement dynamically
sql_stmt := 'SELECT employee\_name, salary FROM employees`
```