How to create a oracle sql script spool file
Asked Answered
T

4

7

I have a question about spooling the the results of my program. My sample sql script looks like this.

  whenever sqlerror exit failure rollback
  set heading off
  set arraysize 1
  set newpage 0
  set pages 0
  set feedback off
  set echo off
  set verify off

 declare
 ab varchar2(10) := 'Raj';
 cd varchar2(10);
 a number := 10;
 c number;
 d number;
 begin
 c := a+10;
 select ab,c into cd,d from dual;
 end;

 SPOOL 
 select cd,d from dual;
 SPOOL OFF
 EXIT;

The above script does not work, but I want to do something like this where in the begin end block we compute some values and i want to spool those results.

Thanks.

Transplant answered 21/12, 2011 at 13:49 Comment(0)
T
16

This will spool the output from the anonymous block into a file called output_<YYYYMMDD>.txt located in the root of the local PC C: drive where <YYYYMMDD> is the current date:

SET SERVEROUTPUT ON FORMAT WRAPPED
SET VERIFY OFF

SET FEEDBACK OFF
SET TERMOUT OFF

column date_column new_value today_var
select to_char(sysdate, 'yyyymmdd') date_column
  from dual
/
DBMS_OUTPUT.ENABLE(1000000);

SPOOL C:\output_&today_var..txt

DECLARE
   ab varchar2(10) := 'Raj';
   cd varchar2(10);
   a  number := 10;
   c  number;
   d  number; 
BEGIN
   c := a+10;
   --
   SELECT ab, c 
     INTO cd, d 
     FROM dual;
   --
   DBMS_OUTPUT.put_line('cd: '||cd);
   DBMS_OUTPUT.put_line('d: '||d);
END; 

SPOOL OFF

SET TERMOUT ON
SET FEEDBACK ON
SET VERIFY ON

PROMPT
PROMPT Done, please see file C:\output_&today_var..txt
PROMPT

Hope it helps...

EDIT:

After your comment to output a value for every iteration of a cursor (I realise each value will be the same in this example but you should get the gist of what i'm doing):

BEGIN
   c := a+10;
   --
   FOR i IN 1 .. 10
   LOOP
      c := a+10;
      -- Output the value of C
      DBMS_OUTPUT.put_line('c: '||c);
   END LOOP;
   --
END; 
Timmytimocracy answered 21/12, 2011 at 14:16 Comment(6)
What happens if there is a cursor in the block and it loops inside the begin-end. Like 'loop c := a+10; select c into :d from dual; end loop; end; SPOOL select :d from dual; SPOOL OFF EXIT;' So, now will it give all the results or else the final output stored in :d. How to spool if there is a loop.Transplant
You could use DBMS_OUTPUT within the cursor loop to output the value at every iteration of the cursor. See edit..Timmytimocracy
Hi Ollie, i have one more question. The above code works fine when i run from SQL*plus. But, i have a shell script which invokes this SQL script and spools the output to text file. I registered the shell script in Oracle apps, and when i submit a request from apps to run. It shows normal and running but it never gets completed. Do, i have to make any changes to the above script when invoking from a shell script.Transplant
If you are running it in the background, I'd worry about where you expect the output to go. Under these circumstances you might be better off using the UTL_FILE package to write the output to a file directly from within the PL/SQL block, you can then read that file using whatever tool you want (PL/SQL, Java, UNIX, etc.). it's pretty simple to do and you'd have less complexity than spooling output through SQLPlus and then into a shell script etc. docs.oracle.com/cd/B19306_01/appdev.102/b14258/u_file.htmTimmytimocracy
To run this from a script in SQL Plus (like @myscript.sql) I had to add "/" after the "END;" of the Anonymous block.Spaceband
Does anyone have a super simple spool example? I just want to export a single line table, but I'm going to have to do this same export several times so would love to not use (right click -> export ...) every time. I can't find an example that doesn't involve 30+ lines of code.Immensurable
A
2

With spool:

  set heading off
  set arraysize 1
  set newpage 0
  set pages 0
  set feedback off
  set echo off
  set verify off

variable cd varchar2(10);
variable d number;

 declare
 ab varchar2(10) := 'Raj';
 a number := 10;
 c number;
 begin
 c := a+10;
 select ab,c into :cd,:d from dual;
 end;

 SPOOL 
 select :cd,:d from dual;
 SPOOL OFF
 EXIT;
Avowal answered 21/12, 2011 at 14:22 Comment(2)
What happens if there is a cursor in the block and it loops inside the begin-end. Like 'loop c := a+10; select c into :d from dual; end loop; end; SPOOL select :d from dual; SPOOL OFF EXIT;' So, now will it give all the results or else the final output stored in :d. How to spool if there is a loop.Transplant
Won't work in that way. You can spool SQL selects, not PL/SQL. For output in pl/sql code, you can use dbms_output package, as Ollie has indicated.Avowal
B
-1

To spool from a BEGIN END block is pretty simple. For example if you need to spool result from two tables into a file, then just use the for loop. Sample code is given below.

BEGIN

FOR x IN 
(
    SELECT COLUMN1,COLUMN2 FROM TABLE1
    UNION ALL
    SELECT COLUMN1,COLUMN2 FROM TABLEB
)    
LOOP
    dbms_output.put_line(x.COLUMN1 || '|' || x.COLUMN2);
END LOOP;

END;
/
Bumboat answered 21/8, 2017 at 6:18 Comment(0)
N
-3

In order to execute a spool file in plsql Go to File->New->command window -> paste your code-> execute. Got to the directory and u will find the file.

Nainsook answered 20/6, 2012 at 16:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.