How to execute an SQL script in PL/SQL?

To execute SQL scripts in PL/SQL, you can use the following methods:

  1. Immediately execute
DECLARE
  script VARCHAR2(4000);
BEGIN
  -- 读取脚本文件内容
  script := '脚本内容';
  
  -- 执行脚本
  EXECUTE IMMEDIATE script;
END;
/
  1. Database Management System Structured Query Language
DECLARE
  script CLOB;
  cursor_id INTEGER;
  status INTEGER;
BEGIN
  -- 读取脚本文件内容
  script := '脚本内容';
  
  -- 创建游标
  cursor_id := DBMS_SQL.OPEN_CURSOR;
  
  -- 解析脚本
  DBMS_SQL.PARSE(cursor_id, script, DBMS_SQL.NATIVE);
  
  -- 执行脚本
  status := DBMS_SQL.EXECUTE(cursor_id);
  
  -- 关闭游标
  DBMS_SQL.CLOSE_CURSOR(cursor_id);
END;
/

Please make sure to replace the placeholder script content with the actual SQL script content. Also, executing the SQL script requires sufficient permissions, otherwise you may encounter permission errors.

bannerAds