Reputation: 2663
I'm familiar with Java JDBC and use it often to run simple SQLs. However, I want to run something like the below. This is more PL/SQL than regular SQL, hence my problems. I am not running this from the Oracle box but from my own computer. Can someone help me to run this kind of PL/SQL using Java?
spool C:\count.txt;
DEFINE date="TO_DATE ('08-29-2011','mm-dd-yyyy')";
SET NEWPAGE 0;
SET SPACE 0;
SET LINESIZE 500;
SET PAGESIZE 0;
SET ECHO OFF;
SET FEEDBACK OFF;
SET VERIFY OFF;
SET HEADING OFF;
SET TRIMSPOOL ON;
alter session set nls_date_format='yyyy-mm-dd hh24:mi';
select 'TABLE1', count(*) from SCHEMA.TABLE1 where modifyts < &date;
select 'TABLE2', count(*) from SCHEMA.TABLE2 where modifyts < &date;
select 'TABLE3', count(*) from SCHEMA.TABLE3 where modifyts < &date;
spool off;
Upvotes: 1
Views: 3062
Reputation: 231661
SPOOL
, DEFINE
, and SET
are all SQL*Plus commands. They are not valid in PL/SQL or in SQL. You cannot, therefore, run this sort of script through a tool other than SQL*Plus (or a tool that supports SQL*Plus commands like SQL Developer or Toad).
You could, of course, have your Java application call out to the operating system to invoke the SQL*Plus executable (assuming it is installed on the machine that the Java application is running on) and pass the script to SQL*Plus. But that's generally way more complexity than you need. It would make more sense to either just use SQL*Plus or to issue just the SELECT
statements from your Java application and use Java's file I/O classes to write the results to a file.
Upvotes: 10