How can PL/SQL export table data?

In PL/SQL, you can export table data using the following methods:

  1. Create a new table by selecting and inserting data in one statement.
DECLARE
  -- 声明变量
  v_data VARCHAR2(4000);
  v_file UTL_FILE.FILE_TYPE;
BEGIN
  -- 打开文件
  v_file := UTL_FILE.FOPEN('DIRECTORY_PATH', 'filename.txt', 'W');

  -- 查询数据并导出到变量中
  SELECT column1 || ',' || column2 || ',' || column3 -- 将需要导出的列连接为一个字符串
  INTO v_data
  FROM your_table;

  -- 将变量的值写入到文件中
  UTL_FILE.PUT_LINE(v_file, v_data);

  -- 关闭文件
  UTL_FILE.FCLOSE(v_file);
END;
/
  1. output messages from the database management system
SET SERVEROUTPUT ON; -- 打开输出

BEGIN
  -- 查询数据并导出到控制台
  FOR rec IN (SELECT * FROM your_table) LOOP
    DBMS_OUTPUT.PUT_LINE(rec.column1 || ',' || rec.column2 || ',' || rec.column3); -- 将需要导出的列连接为一个字符串
  END LOOP;
END;
/

Please choose the appropriate method based on actual needs and make necessary adjustments as needed. Also, please modify the table names, column names, directory paths, and file names in the sample code according to the actual situation.

bannerAds