SQL Plus: Generating Delimited Files

Hoi!
Say you need to dump a special set of data into a file..

Sure you can do that with fancy SQL clients which have graphical user interfaces. However, sometimes you do need to export your data set into a plain text file in a *certain* format, via sqlplus.

In some cases, for instance, if you ‘re going to use that file to feed another system, you ‘ll have to follow that 3rd systems data delivery rules. There can be some restrictions such as

  • Maximum line size
  • End of Line characters
  • Default Character Set
  • Pages
  • Spaces

etc.

So, since we are using sqlplus for our action, better prepare our query as a script.. I won ‘t explain all of these definitions, but it ‘s quite easy to find out what they do :)

Let ‘s write our .sql script by starting these SETs:

SET LINESIZE 300;
SET TERMOUT OFF;
SET ECHO OFF;
SET NEWP 0;
SET SPACE 0;
SET PAGES 0;
SET FEED OFF;
SET HEAD OFF;
SET TRIMS OFF;
SET TAB OFF;
SET SERVEROUTPUT ON;
SET SQLBL OFF;
SET RECSEP OFF;

If you think you don ‘t need some of them, just put a dash before the line to make it disable.

As you can imagine, first one is the one that you defined the maximum length of a line in your output file.. Needless to say, you should read the specs of that 3rd system before setting it :)

Then we need to enter a path to address the exported file ‘s location.

 

spool D:\EXPORTS\export.txt

 

you can place your sql query after this line. For example

 

SELECT 1
FROM dual;

 

then we will have to tell that the spooling has to stop, and script ‘s got to exit.

 

spool OFF;
exit;

 

Now we can save this file as an .sql file.

It would be a wise idea to test it. Run sqlplus on your terminal screen.

Most of us are using GUI tools for our daily works. If you don ‘t know how to run it, believe me it ‘s quite normal :)

On Windows, START > RUN > CMD

It will open a terminal window. Then use that syntax with your own parameters.

 

c:\> sqlplus user/pass@database_IP:port/service

 

On GNU/Linux, start eterm, xterm or any terminal you like :)

Can you see SQL> prompt? If yes, you are almost done. Otherwise you ‘ll have to google it :)

Well, let ‘s try to execute our script now.

 

SQL> @c:\example.sql

 

This should work.. Now we can check our spool output folder..

If you are using a Windows and everything was fine so far, maybe it ‘d be better to create a .BAT to make this whole process easier.

Write these lines in notepad,

# sample BATCH file
SET NLS_CHARSET = UTF8
c:\> sqlplus user/pass@database_IP:port/service @c:\example.sql
exit

..save the file with the .BAT extension.

Now you can use your Task Scheduler to execute that batch file automatically.

Hope it helps.. Next time maybe we can talk about configuring your Task Scheduler.

Cheers!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s