Basically what you are describing is the classic example for using dynamic SQL. There are multiple types and variations...
By submitting your personal information, you agree that TechTarget and its partners may contact you regarding relevant content, products and special offers.
to implementing dynamic SQL statements, but for your case you should read up on and implement varying-list SELECT dynamic SQL. You can use a varying-list dynamic SELECT to explicitly prepare and execute SQL SELECT statements when you do not know in advance which columns will be retrieved by an application program. Varying-list SELECT provides the most flexibility for dynamic SELECT statements. You can change tables, columns, and predicates "on the fly."
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.
For More Information
- Dozens more answers to tough DB2 questions from Craig Mullins are available.
- The Best IBM DB2 Web Links: tips, tutorials, scripts, and more.
- Have a DB2 tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
- Ask your technical DB2 questions -- or help out your peers by answering them -- in our live discussion forums.
- Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, object-oriented and data warehousing gurus are waiting to answer your toughest questions.
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.