need to spool output into.csv file using loop, fyi...I have data in 4 diff partition.
but no sure how to proceed.
code be like.
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;
/
result:-
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:
not sure if I am spooling properly, also please check the index variable of for loop in used correctly in my select statement.
You don't need 4 different queries, just list all your partitions in the query:
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;
Spool is a SQL*Plus command and PL/SQL doesn't "understand" it. You'd rather switch to UTL_FILE
package, if it has to be PL/SQL.
Though, if that's the only code you have, I'd suggest you to simply use 4 separate SELECT
statements, each using its own partition, and spool the result into a CSV file.
If you did that, you'd be done HOURS ago.
spool
is a SQL*Plus command which might be combined with an embedded PL/SQL code containing a cursor such as SYS_REFCURSOR
with a dynamic SQL as
rec=$(sqlplus -S /nolog << EOF
conn hr/hr
whenever sqlerror exit sql.sqlcode
SET LINESIZE 1000
SET FEEDBACK OFF
SET UNDERLINE OFF
SET SERVEROUTPUT ON
SPOOL file.csv REPLACE
DECLARE
v_cursor SYS_REFCURSOR;
v_sql VARCHAR2(32767);
cust customer.cust_no%TYPE;
BEGIN
FOR var_1 in 1..4
LOOP
v_sql :=
'SELECT cust_no
FROM customer PARTITION(customer_pr'||var_1||')
WHERE city = :ct
AND first_name = :fn
AND salary = :sl';
OPEN v_cursor FOR v_sql USING 'ba','john',1000;
LOOP
FETCH v_cursor INTO cust;
DBMS_OUTPUT.PUT_LINE(cust);
EXIT WHEN v_cursor%NOTFOUND;
END LOOP;
END LOOP;
CLOSE v_cursor;
END;
/
SPOOL OFF
EOF
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.