DB2 for i – FAQ & Howtos (EN)

iDB2-FAQ-011 – How to get a CSV file from a SQL statement?

As you know, you can get a CSV file (or a TXT, XLS and so on file) from the result of a query with ACS SQL Script… but if you need to generate a CSV directly from a SQL script you could mix SQL and CL command, as in the following example where we get a CSV file an IFS directory from a query on a NETSTAT system view:

create table qtemp.netstat as
 (SELECT *
     FROM qsys2.netstat_info)
     with data;
 cl: CPYTOIMPF FROMFILE(QTEMP/NETSTAT)
     TOSTMF('/home/faq/netstat.csv')
     STMFCCSID(PCASCII) RCDDLM(CRLF) ADDCOLNAM(*SQL);  

And if you want schedule or call this script from another program you could execute it with RUNSQLSTM, as in this simple CL program

       PGM          
         RUNSQLSTM  SRCFILE(FAQ400/SRC) SRCMBR(XNETSTAT) COMMIT(*NONE) OUTPUT(*PRINT)   
       ENDPGM     

Related Posts
DB2 for i SQL – String Manipulation – POSSTR-LOCATE-LOCATE_IN_STRING (EN)

Introduction Often, in our applications, we need to work with text strings, and DB2 SQL can come in very useful Read more

DB2 for i & SQL – FAQ & Howto (Part. 1) (IT)

Database DB2 e SQL ... forse lo strumento più potente e completo che abbiamo sulla piattaforma IBM i: ecco una Read more

IBM i 7.4 Announcement (En)

Comes directly with the Easter egg this IBM announcement for the news of the IBM i 7.4 version, iNext version Read more

Generated Always Columns (EN)

Introduction "Generated Always Column": are columns, table fields, filled by DB2 engine: something like columns with a default value but Read more

Page: 1 2

Recent Posts

Create SQL Views to interpret QSYS2.DISPLAY_JOURNAL

The sources (SQL statement) of this post are available on Github at this link: https://github.com/Faq400Git/Create_Display_Journal_Table_View Personally I am a big…

1 year ago

How to extract program interface information (PCML) embedded in programs or service programs

What is PCML? Program Call Markup Language (PCML) is a tag language based on the Extensible Markup Language (XML) that…

1 year ago

How to use REST API on IBM i: the Open Banking example

The IBM i system, being a robust and versatile business server, can easily connect to the world of REST APIs,…

1 year ago

Writing ILE RPG code with ChatGPT? Let's try!

There is talk everywhere about this ChatGPT, OpenAI's new system that allows interaction with Artificial Intelligence systems to be more…

1 year ago

Multi-member file and SQL

I recommend this post by Aldo Succi from his blog "IBM i (AS/400) Fans Only" on the topic of Multimember…

1 year ago

Power coffee no. 19/2022 – Dates and times: default and null values

I would like to point out this Power Coffee by the legendary Marco Riva, IBM Champion and colleague. In this…

1 year ago