在PL/SQL中运行SQL语句有多种方式:
使用SQLPlus:在SQLPlus命令行界面中,输入SQL语句并以分号(;)结尾,然后按下回车键执行。
使用PL/SQL块:在PL/SQL块中,使用EXECUTE IMMEDIATE语句执行SQL语句。示例代码如下:
BEGIN
EXECUTE IMMEDIATE 'SELECT * FROM employees';
END;
DECLARE
CURSOR c_emp IS SELECT * FROM employees;
emp_rec employees%ROWTYPE;
BEGIN
OPEN c_emp;
LOOP
FETCH c_emp INTO emp_rec;
EXIT WHEN c_emp%NOTFOUND;
-- 处理emp_rec
END LOOP;
CLOSE c_emp;
END;
CREATE OR REPLACE PROCEDURE get_employee(employee_id NUMBER) AS
emp_rec employees%ROWTYPE;
BEGIN
SELECT * INTO emp_rec FROM employees WHERE employee_id = employee_id;
-- 处理emp_rec
END;
以上是一些常见的在PL/SQL中运行SQL语句的方式,具体的选择取决于具体的需求和场景。