You can use lookup variables in a query to present the user with a drop-down menu of possible substitution values for the variable at run time. Lookup variables create a drop-down menu from the saved query or results set that you use in the variable name. In the Query or result, the lookup variable will be the first column returned and it will be separated by a space from the other columns if there are any. Depending on the data type, it will be quoted automatically. Lookup variables only work with saved queries or results.
To write an SQL query with lookup variables:
1. In the desired connection branch of the DB2 Web Query Tool navigation tree, click SQL Queries. A drop down menu appears.
2. On the drop-down menu, click New. The Edit Query page opens.
3. In the Enter an SQL Statement(s) field, type an SQL Query.
4. Optional: To include lookup variables in your query, use one of the following formats:
· [q.groupname.queryname]
· [r.groupname.resultname]
· [qm.groupname.queryname]
· [rm.groupname.resultname]
Use the name of the query or result from which you want to draw the drop-down menu. Lookup variables beginning with "qm." or "rm." allow you to select multiple values for the variable. Lookup variables beginning with "q." or "r." allow you to select one value.
To define a variable in a query, use one of the following syntax methods.
· ?
· :variable
· [variable]
· &variable
For example, in the following SQL statement, &userid is a variable:
select * from table.sample where user=&userid
You can also use any of the following system variables:
· wqsys.time - Substitutes the current time.
· wqsys.date - Substitutes the current date.
· wqsys.timestamp - Substitutes the current timestamp.
· wqsys.user - Substitutes the user name under which you are logged in.
· wqsys.sqlid - Substitutes the SQL ID under which you are logged in.
· wqsys.database - Substitutes the name of the database you are accessing.
5. Optional: To include comments in your query, use one of the following formats:
· -- comment
· // comment
Note: Only the text following the comment symbols is treated as a comment. Any text preceding the comment symbols on the same line is treated as part of the SQL statement.
6. Click Next. The Query Options screen opens.
7. In the Settings section, select your Auto-Commit preference.
· Select Off to prevent your changes from being automatically committed to the database. When this option is selected, commits must be made to the database manually.
Note: If you select not to have your changes automatically committed, your database will be left in an uncommitted state. This can cause problems.
· Select When Finished to have your changes committed to the database after all SQL statements have been run.
· Select After Each Statement to have your changes committed to the database after each SQL statement is run.
8. In the Result Name field, type the name that you want to use for the results set.
Tip: DB2 Web Query Tool provides a default results set name. You can replace this name with one that you prefer.
9. In the Max Rows field, type the maximum number of rows to return in the results set.
Tip: To return all rows to the results set, type "0" in the Max Rows field.
10. In the Timeout field, type a timeout limit.
11. From the Action drop-down, select the action that you want to be taken when LOB data is encountered.
· Select Omit to have LOBs omitted.
· Select Retrieve to have LOBs retrieved.
· Select Embed to have LOBs embedded in the results set.
12. If you have SQL\PA installed on you system and your query has not been analyzed by SQL/PA, "Not Analyzed" will appear next to SQL/PA in the SQL/PA section. To analyze your query with SQL/PA, click Analyze in the SQL/PA section of the Settings section. SQL/PA analyzes one statement at a time. Therefore, if you have more than one SQL statement embedded in your query, you will see results in a cumulative format. To set your SQL/PA analysis options, click Analyze options.
Note: If you do not have SQL/PA installed, "Not Installed" will appear next to SQL/PA in the SQL/PA section.
13. Optional: Click Edit SQL to edit the SQL query.
14. Click each lookup variable that is displayed in the Parsed Query section and define its value.
15. Type the value of the variable or select a value from the drop down menu, if available.
· Select Use to substitute the value specified for only that instance of the variable name.
· Select All References to substitute the value specified for every instance of that variable name in the query.
· Select Ignore to treat the variable as a string.
Tip: When you click a lookup variable, a drop down menu appears from which you can select a value for the variable. If the variable has multiple selection defined for it, you may be able to select multiple values for the variable by holding down the Control key.
16. Run or save the query. To run the query, click Run.
Note: DB2 Web Query Tool does not validate SQL. Be certain to use correct SQL syntax, table names, and column names when writing your query.
DB2 Web Query Tool runs the query, and displays the Query Data page. You can now run or save the query. You can view the message page, and specify how to display or export the query results if desired.