Because everything about the query can change during one invocation of the program, the number and type of host variables needed to store the retrieved rows cannot be known beforehand. The lack of knowledge regarding what is being retrieved adds considerable complexity to your application programs.
The steps needed to code varying-list SELECT dynamic SQL will vary according to the amount of information known about the SQL beforehand. In general, though, you will need to build your SQL statement from the input given through your online screen. Only the columns for which values have been input should be built into your SQL WHERE clauses. Then you would move that SQL statement to a string variable, declare a cursor and then prepare that cursor into the SQLDA. Following that you would load the appropriate storage addresses into the SQLDA and then fetch the results from the cursor referencing the SQLDA in the FETCH statement.
I cannot teach all of the nuances of dynamic SQL in a short answer to a question in this type of forum. Keep in mind that to implement a dynamic varying-list SELECT you will need to learn about the PREPARE statement, the SQLDA, using FETCH with the SQLDA, and how to store host variable addresses in the SQLDA. Read up about this "stuff" in Chapter 23 of the IBM DB2 V7 manual named "Application Programming and SQL Guide" (SC26-9933) which can be downloaded from the IBM web site for free. http://www-3.ibm.com/software/data/db2/library/
Dig Deeper on IBM DB2 management
Related Q&A from Craig S. Mullins
Our expert suggests that the best way to pass the SAP BW exam is to have hands-on job experience. But that's not all he has to say about ... Continue Reading
To export data from a DB2 table to a flat file, you need to run an export specifying the proper file format. The export utility exports data from a ... Continue Reading
Craig Mullins recommends two specific resources for learning how to create and support Binary Large Objects (BLOB) in DB2. Continue Reading
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.