首页 > 解决方案 > 使用 for 循环假脱机到 .csv 文件

问题描述

需要使用循环将输出假脱机到 .csv 文件中,仅供参考...我在 4 个 diff 分区中有数据。

但不知道如何进行。

代码就像。

begin
FOR var_1 in 1 .. 4
LOOP
set linesize 1000
set feedback off
set underline off
spool C:\Users\file.csv replace
        SELECT cust_no FROM customer PARTITION (customer_PR'||var_1||')
        WHERE city='ba' AND first_name='john'
        AND salary=1000;
spool off;
END LOOP;
END;
/

结果:-

Error report -
ORA-06550: line 4, column 5:
PL/SQL: ORA-00922: missing or invalid option
ORA-06550: line 4, column 1:
PL/SQL: SQL Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

不确定我是否正确假脱机,还请检查在我的选择语句中正确使用的 for 循环的索引变量。

标签: sqloracleplsqlsqlplus

解决方案


您不需要 4 个不同的查询,只需在查询中列出所有分区:

set linesize 1000
set feedback off
set underline off
spool C:\Users\file.csv replace
SELECT cust_no FROM customer PARTITION (customer_PR1, customer_PR2, customer_PR3, customer_PR4)
        WHERE city='ba' AND first_name='john'
        AND salary=1000;
spool off;

推荐阅读