0
votes

Am trying to execute a SQL Script using cx_Oracle which roughly looks like

SPOOL /tmp/test.log SET echo ON SET serveroutput ON SIZE UNLIMITED BEGIN #SOME_PL/SQL_STATEMENTS END; / SPOOL OFF

where am trying to write result of PL/SQL statements into a file. I came to know that SQLPlus commands are not supported in cx_Oracle. Is there any way to use SPOOL using cx_Oracle?

1
what if you went the other way - using Python in your SQLcl (java based, modern SQLPlus) script? blogs.oracle.com/letthesunshinein/… you'd have full access to SQLPlus commands like SPOOL - thatjeffsmith
Is Jython an option instead of Python ? If so I can put an example here. - Kris Rice
Or why not simply use python's File Objects ? - Kaushik Nayak
@KrisRice Please do. - Sathyanarayanan Ganesan
@KaushikNayak That option is there. Just want to explore other options as well. - Sathyanarayanan Ganesan

1 Answers

2
votes

There is no direct way to use SPOOL within cx_Oracle.

The problem is that SPOOL is SQL*PLUS specific command. It is not an Oracle SQL command. Therefore the actual spooling is handled on client side. When you do things like

SPOOL ON
SET PAGESIZE 0
SET SERVEROUTPUT ON
COLUMN username FORMAT A20

all of there commands are SQL*PLUS commands, a local (client-side) commands and they are not being send to Oracle server nor changing the behaviour of Oracle session at all.

These commands are there only to manipulate the way how the output from Oracle server is going to be shown on screen (or written to file or printer in case of SPOOL).

You can identify a local command by the fact that it does not have to be ended with semicolon ; (in most cases).

Full reference of SQL*PLUS commands can be found here: https://docs.oracle.com/database/121/SQPUG/ch_twelve001.htm#SQPUG023