- 静态 SQL
- 动态 SQL
静态 SQL 是属于 PL/SQL 语言的 SQL。也就是:
- 除了解释执行计划的,数据操作语句(Data Manipulation Language,DML)
- 事务控制(Transaction Control Language,TCL)语句
- SQL 函数
- SQL 伪列
- SQL 运算符
静态 SQL 符合目前 ANSI/ISO SQL 标准。
示例 1:演示用 PL/SQL 操作数据
若操作数据库的数据,则无需任何特别的符号,你可以直接在 PL/SQL 程序里包含 DML 操作,如 INSERT、UPDATE 和 DELETE 语句。你也可以直接在 PL/SQL 程序里包含 COMMIT 语句。
CREATE TABLE employees_temp
AS SELECT employee_id, first_name, last_name
FROM employees;
DECLARE
emp_id employees_temp.employee_id%TYPE;
emp_first_name employees_temp.first_name%TYPE;
emp_last_name employees_temp.last_name%TYPE;
BEGIN
INSERT INTO employees_temp VALUES(299, 'Bob', 'Henry');
UPDATE employees_temp
SET first_name = 'Robert' WHERE employee_id = 299;
DELETE FROM employees_temp WHERE employee_id = 299
RETURNING first_name, last_name
INTO emp_first_name, emp_last_name;
COMMIT;
DBMS_OUTPUT.PUT_LINE( emp_first_name || ' ' || emp_last_name);
END;
/
示例 2:演示在 PL/SQL 调用 SQL 函数——COUNT 函数
DECLARE
job_count NUMBER;
emp_count NUMBER;
BEGIN
SELECT COUNT(DISTINCT job_id)
INTO job_count
FROM employees;
SELECT COUNT(*)
INTO emp_count
FROM employees;
END;
/
示例 3:演示使用伪列——ROWNUM
CREATE TABLE employees_temp AS SELECT * FROM employees;
DECLARE
CURSOR c1 IS SELECT employee_id, salary FROM employees_temp
WHERE salary > 2000 AND ROWNUM 2000 ORDER BY salary DESC)
WHERE ROWNUM < 5; -- first 5 rows, in sorted order
BEGIN
-- Each row gets assigned a different number
UPDATE employees_temp SET employee_id = ROWNUM;
END;
/