Using Variables and Comments in a Query

 

You can include variables and comments in an SQL query. You can also use lookup variables, which refer to other queries or results to produce a drop-down menu of substitution values.

To write an SQL query with variables and comments:

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 field, type an SQL statement. To include variables in your query, use one of the following formats:

·      ?

·      :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.

4.         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 you want to draw the drop-down menu from. Lookup variables beginning with "qm." or "rm." allow the user to select multiple values for the variable. Lookup variables beginning with "q." or "r." only allow the user to select one value.

5.         To include comments in your query, use one of the following formats:

·      -- comment

·      // comment

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 page 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. 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. 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.

13.      If you do not have SQL/PA installed, "Not Installed" will appear next to SQL/PA in the SQL/PA section.

14.      Optional: Click Edit SQL to edit the SQL query.

15.      You can now run or save the query.