Feedback Survey

31BI How to Use the
SQL Descriptor
Greg Cannella
www.common.org
#COMMONS17
When you run an SQL
statement, you get a result set.
The SQL descriptor provides
every descriptive detail about
the result set. Think of it as a
DSPFFD for an SQL
statement.
#COMMONS17
www.common.org
Agenda
• Why SQL descriptor
• Descriptor or SQLDA
• Descriptor for headings
• Descriptor for data
#COMMONS17
www.common.org
Why the Descriptor
• Prevent hard coding and a story about my friend
George
#COMMONS17
www.common.org
Descriptor or SQLDA
• SQLDA Was available pre V5R4 of the OS
• Descriptor became available in version 5.4
• Both still work
• Descriptor provides more information
• It is easier for IBM to extend without breaking existing
applications
#COMMONS17
www.common.org
The End Result
#COMMONS17
www.common.org
The End Result
<table class="table-all-lines">
<tr><th></th><th>Hour</th><th>Time</th><th>Picks
Released</th><th>Picks to do Today</th><th>Comp Last
Hour</th><th>Remaining Picks</th><th>Already
Picked</th><th>Pace</th><th>Pace Required</th></tr>
<tr><td align="Right"></td><td
align="Right">1.01</td><td>6.:01</td><td align="Right"></td><td
align="Right">1,053</td><td align="Right">134</td><td
align="Right">1,053</td><td align="Right">134</td><td
align="Right">325.8</td><td align="Right">87</td></tr>
<tr><td align="Right"></td><td
align="Right">2.01</td><td>7.:01</td><td align="Right"></td><td
align="Right">1,059</td><td align="Right">234</td><td
align="Right">871</td><td align="Right">368</td><td
align="Right">252.0</td><td align="Right">72</td></tr>
</table>
#COMMONS17
www.common.org
The Setup
// These
exec sql
exec sql
exec sql
first three lines you already know.
prepare handleT from :SQLStatement;
declare OpenHandleT cursor for handleT;
Open OpenHandleT;
// These three lines are added for the descriptor
// Define max number of columns and allocate memory.
exec SQL ALLOCATE DESCRIPTOR 'Values' WITH MAX 200;
// Fill the descriptor with data.
exec SQL describe handleT using descriptor 'Values';
// Find out how many columns exist.
exec SQL GET DESCRIPTOR 'Values' :TotalColumns = COUNT;
#COMMONS17
www.common.org
Descriptor for Headings
•Loop through columns
#COMMONS17
www.common.org
Descriptor for Data
• Fetch a row of data
exec sql fetch next from OpenHandleT into
SQL DESCRIPTOR 'Values';
• Note: “into SQL DESCRIPTOR ‘Values’”
#COMMONS17
www.common.org
Descriptor for Data – Step 1
• Values = The name of my descriptor
• Value :x = The current column I am parsing
#COMMONS17
www.common.org
Descriptor for Data
• The variable that you put the resulting data must match
following items:
•
•
•
•
•
Data type
Length
Precision
Scale
CCSID
• This means you need code to handle every
combination that might be in an SQL column.
#COMMONS17
www.common.org
Descriptor for Data – Step 2
• Retrieve value into a field of the exact length and
precision of the data
#COMMONS17
www.common.org
Putting It All Together – Part 1
• Declare and open your cursor like always
• Allocate Descriptor
• Use describe statement
• Get any table level values that you need such as the
number of columns (COUNT)
• Loop through the header values such as the columns
names (DB2_COLUMN_NAME)
#COMMONS17
www.common.org
Putting It All Together – Part 2
• Loop through all rows using the fetch into SQL
DESCRIPTOR statement
• Loop through each column using GET DESCRIPTOR getting the
attributes that describe the data
• Setup giant select statements based on type, length, precision and
scale
• Get the single value from the current row and column using GET
DESCRIPTOR – DATA
• Close the cursor
#COMMONS17
www.common.org
Show Live Examples
#COMMONS17
www.common.org
#COMMONS17
www.common.org
#COMMONS17
www.common.org
#COMMONS17
www.common.org
Thank You
Questions?
www.common.org
#COMMONS17
Don’t Forget Your Session Surveys
• Sign in to the Online Session Guide
(www.common.org/sessions)
• Go to your personal schedule
• Click on the session that you attended
• Click on the Feedback Survey button located above the
abstract
Completing session surveys helps us plan future
programming and provides feedback used in speaker
awards. Thank you for your participation.
#COMMONS17
www.common.org
Link
• Link to IBM documentation
• https://www.ibm.com/support/knowledgecenter/ssw_ib
m_i_72/sqlp/rbafydynmic.htm
#COMMONS17
www.common.org