13
Wed, Nov
5 New Articles

TechTip: Add Column Headings to Your Export Files

CL
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

iSeries programmers are spending an increasing amount of time preparing data for PC users. Often, data is prepared for use in a spreadsheet such as Lotus 1-2-3 or Excel. Ease of creation, quality of output, and security are important factors in this process.

The Client Access data transfer utility has always been a good tool for this task with the caveat that it has to be installed on a user's PC--along with a host of security concerns when users are able to download data. For those using the Integrated File System (IFS), the alternative has been to use the Copy to Import File (CPYTOIMPF) command to convert a database file to a comma- or tab-delimited stream file (i.e., a PC file) on the IFS. This method has two strengths: It's AS/400-based without requiring any special tools on the user's desktop, and it's secure. However, its main weakness is that the command supplies only raw data, so either the programmer must add headings or the user must research what each column is.

In case you're not familiar with it, here's how CPYTOIMPF works. Suppose you have table ORDERS (exported from Microsoft's Northwind.MDB sample database), as shown below:

CREATE TABLE ORDERS (
    ORDERID         INTEGER    NOT NULL,
    CUSTOMERID      VARCHAR(5) NOT NULL,
    EMPLOYEEID      INTEGER,
    SHIPVIA         INTEGER,
    FREIGHT         DECIMAL(9,2),
    SHIPNAME        VARCHAR(40),
    SHIPADDRESS     VARCHAR(60),     SHIPCITY        VARCHAR(15),
    SHIPREGION      VARCHAR(15),
    SHIPPOSTALCODE  VARCHAR(10),
    SHIPCOUNTRY     VARCHAR(15),
    ORDERDATE       DATE,
    REQUIREDDATE    DATE,
    SHIPPEDDATE     DATE,
CONSTRAINT ORDERS_PK PRIMARY KEY(ORDERID))


You want to export this table to a text file that spreadsheet users can open. Joe, a PC user, has his home drive H: mapped to folder homeusersjoe on the IFS. If you want a text file version of the ORDERS table to appear on Joe's H: drive, simply type this:

CPYTOIMPF FROMFILE(ORDERS)
          TOSTMF('/home/users/joe/orders.csv')
          MBROPT(*REPLACE)
          STMFCODPAG(*PCASCII)
          RCDDLM(*CRLF)
          DTAFMT(*DLM)
          FLDDLM(',')


In this case, CPYTOIMPF will convert the ORDERS table into a text-based, comma-delimited export file called orders.csv in folder /home/users/joe, which a user can easily work with. It would be nice, however, if CPYTOIMPF had an option to read the file's field names or column headings and place them with the data.

Enter the Retrieve Column Headings (RTVCOLHDG) utility. It works in conjunction with the CPYTOIMPF command to add column headings to export files so that users can see what each column contains. The headings will contain the column headings defined by the file's DDS COLHDG keyword or by SQL's LABEL ON COLUMN statement.

The table below shows the list of command parameters for RTVCOLHDG:

Parameter
Description
FILE
File and library reference containing columns to be retrieved
STMF
Name of stream file
Note: If the stream file exists, it will automatically be replaced.
RCDFMT
Record format to be used
USECOL
Use field column headings when available
The default is YES. Specify NO if you want to use actual field names rather than column headings. Further, in the absence of column headings, defined long field names are used rather than system field names.
HDGCONST
Heading constants to be placed at the top of the output file before the stream file headings are copied
A maximum of twelve lines may be specified. String delimiters are not automatically placed in this section of text.
RCDDLM
Record delimiter to use in output file
The default is a carriage return followed by a line-feed character.
STRDLM
String delimiter to use in output file
The default is a double quote.
FLDDLM
Field delimiter to use in output file
The default is a comma.


Now, in the ORDERS table example, suppose the table was defined with the following column headings:

LABEL ON COLUMN ORDERS (
ORDERID     IS '                    Order               ID',
CUSTOMERID  IS '                    Customer            ID',
EMPLOYEEID  IS '                    Employee            ID',
SHIPVIA     IS '                    Ship                Via',
FREIGHT     IS '                    Freight',
SHIPNAME    IS '                    Ship                Name',
SHIPADDRESS IS '                    Ship                Address',
SHIPCITY    IS '                    Ship                City',
SHIPREGION  IS '                    Ship                Region',
SHIPPOSTALCODE 
             IS 'Ship                Postal              Code',
SHIPCOUNTRY  IS '                    Ship                Country',
ORDERDATE    IS '                    Order               Date', 
REQUIREDDATE IS '                    Required            Date', 
SHIPPEDDATE  IS '                    Shipped             Date')


Here's an example of using RTVCOLHDG to build a stream file containing the column headings:

RTVCOLHDG FILE(ORDERS)
          STMF('/home/users/joe/orders.csv')
          RCDFMT(*FIRST)
          USECOL(*YES)
          HDGCONST(('Orders Table') (' '))
          FLDDLM(',') 

Use the DSPF command to view the stream file:

DSPF '/home/users/joe/orders.csv'


Here's the output RTVCOLHDG produces:

 ************Beginning of data**************
Orders Table                                           

"","","","","","","","","","Ship","","","",""          
"Order","Customer","Employee","Ship","Freight","Ship","
"ID","ID","ID","Via","","Name","Address","City","Region
 ************End of Data******************** 


The first two lines consist of the heading constants specified on the command. The next three lines show the column headings as defined in the file. Now, the data has to be appended to this stream file using CPYTOIMPF. However, we need to specify MBROPT(*ADD) to append data to the existing stream file rather than replace it.

CPYTOIMPF FROMFILE(ORDERS)
          TOSTMF('/home/users/joe/orders.csv')
          MBROPT(*ADD)
          RCDDLM(*CRLF)

 

Formatting the Results in Excel

Figure 1 shows how the orders.csv file appears in Excel.

http://www.mcpressonline.com/articles/images/2002/ColHdgV400.png

Figure 1: Using RTVCOLHDG produces these results in Excel. (Click images to enlarge.)


You may have noticed that the columns are nicely spaced and assumed it took a long time to size each column. Actually, there's an easy way to make Excel autofit all of the columns, and it only takes two seconds to show users:

1. Click the box to the left of the A and above the 1 to highlight the entire spreadsheet. In Figure 2, this box has a red dot in it. (Alternatively, you can press Control+A.)

2. Move your cursor to the heading row between the A and the B (circled in yellow in Figure 2). The cursor icon will turn to a crosshair when positioned correctly. Double-click, and the columns will be sized to fit the largest value in each column. (Alternatively, choose Format->Column->AutoFit Selection.)

http://www.mcpressonline.com/articles/images/2002/ColHdgV401.jpg

Figure 2: Highlight the entire spreadsheet to "autofit" column width.

Production Use

Figure 3 contains a sample CL program demonstrating how I've used RTVCOLHDG in production environments. I follow these general steps:

1. The user enters applicable criteria via command prompt or RPG program and passes the information to the CL program.
2. A SELECT statement is run to get the data using either Query Management Queries or a utility such as RUNSQL. I've used the RUNSQL utility just so the SELECT would be visible in the CL program. The results are dumped into QTEMP. A program could be used to extract data as well.
3. If needed, the SQL LABEL ON statement is issued to override any column headings that contain poor descriptions (such as calculated fields in an SQL statement).
4. The RTVCOLHDG command creates the stream file and populates it with heading information.
5. The CPYTOIMPF command appends the data from the temp file to the stream file. The stream file can be FTPed, emailed, or left on the IFS for users to access, depending on your system's capabilities.

/* */
/* CUSTSALES */
/* */
/* Send customer sales history to comma delimited */
/* export file. */
/* */

PGM PARM(&CUSTOMERID)


/* User Passes Customer ID */
DCL &CUSTOMERID *CHAR 5


/* Misc Stuff */
DCL &SYSDATE *CHAR 6
DCL &SYSTIME *CHAR 6
DCL &SYSDATEF *CHAR 10
DCL &SYSTIMEF *CHAR 8
DCL &STMF *CHAR 80 VALUE('/home/msansoterr/Customer_Sales.CSV')

RTVSYSVAL QDATE &SYSDATE
RTVSYSVAL QTIME &SYSTIME

CVTDAT &SYSDATE &SYSDATEF FROMFMT(*JOB) TOFMT(*USA)
CHGVAR &SYSTIMEF ((%SST(&SYSDATEF 1 2) || ':' || +
%SST(&SYSDATEF 3 2) || ':' || +
%SST(&SYSDATEF 5 2)))


/* Delete work file, if it exists */
DLTF QTEMP/TEMPDATA
MONMSG CPF2105


/* Run SQL to retrieve customer's sales history +
This work file processing is usually done by a QM Query +
or program (COBOL/RPG). +
+
NOTE: The CREATE TABLE AS is available with V5R2 */

RUNSQL (' +
CREATE TABLE QTEMP/TEMPDATA AS ( +
SELECT Year(A.ShippedDate) * 100 + +
Month(A.ShippedDate) AS Period, +
B.ProductID, C.ProductName, +
SUM(B.Quantity) As Qty, +
Avg(B.UnitPrice) AS AvgPrice, +
Sum(Round(B.Quantity * B.UnitPrice,2)) AS ExtPrice +
FROM Orders A +
JOIN OrderDetails B On B.OrderID=A.OrderID +
JOIN Products C On C.ProductID=B.ProductID +
WHERE CustomerID=''' || &CUSTOMERID || ''' +
GROUP BY Year(A.ShippedDate) * 100 + +
Month(A.ShippedDate), +
B.ProductID, C.ProductName +
ORDER BY 1 DESC, 2, 3) +
WITH DATA')


/* Place meaningful column labels on the work table */
RUNSQL (' +
LABEL ON COLUMN QTEMP/TEMPDATA ( +
PERIOD IS ''Period'', +
PRODUCTID IS ''Product ID'', +
PRODUCTNAME IS ''Product Name'', +
QTY IS ''Qty'', +
AVGPRICE IS ''Average Price'', +
EXTPRICE IS ''Sales Amount'')')


/* Retrieve column headings into a stream file +
Include a title, customer id and date as well */
RTVCOLHDG FILE(QTEMP/TEMPDATA) +
STMF(&STMF) +
HDGCONST('Customer Sales History' +
' ' +
('Customer ID:'||&CustomerID) +
' ' +
('Run Date: ' || &SysDateF || ' ' || &SysTimeF) +
' ')


/* Append Data to Headings using CPYTOIMPF */
CPYTOIMPF FROMFILE(QTEMP/TEMPDATA) TOSTMF(&STMF) +
MBROPT(*ADD) RCDDLM(*CRLF)


/* Additional processing to e-mail, FTP, etc. the output +
file goes here.... */

ENDPGM

Figure 3: This sample CL program shows how to use RTVCOLHDG in production environments.

Caveats

Watch out for a couple of things when using this technique:

1. Strings with embedded double quotes (") cause problems when the double quote is the string delimiter. You can easily resolve this by replacing a double quote with two double quotes or a substitution character. When using temporary files, this is done most easily with an SQL UPDATE statement using the TRANSLATE function:

UPDATE WorkTable SET Field1=TRANSLATE(Field1,'''','"')


2. When a column containing string values has only numeric data, Excel will treat the data as numeric. This is particularly bothersome when dealing with ZIP codes because leading zeros will often be truncated. This problem can usually be solved by having the user format the cells to show leading zeros.

Export Etiquette

It's important that users be able to access iSeries application data quickly and without formatting hassles. The CPYTOIMPF command provides a way to give users delimited data in a stream file format for easy spreadsheet access. The RTVCOLHDG utility provides an appropriate complement by allowing the users to see a definition for what each column holds.

Michael Sansoterra is a DBA for Broadway Systems in Grand Rapids, Michigan. He can be contacted at This email address is being protected from spambots. You need JavaScript enabled to view it..


BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: