Conditional Variables

AIX HP-UX Linux OS/2 OS/390 OS/400 PTX SUN Win NT
X X X X X X X X X

A conditional variable is one that is set based on the value of another variable or string. This is also called a ternary operation.

The syntax to conditionally set a variable is:

condVar = testVar ? trueValue : falseValue

Where:

condVar
The conditional variable to be set.

testVar
A test variable used to determine the condition. An empty string is evaluated as false.

trueValue
Is the value to use if the test variable is true.

falseValue
Is the value to use if the test variable is false.

Example 1: A conditional variable defined with two possible values

varA = varB ? "value_1" : "value_2"
varB is the test. So, varA is assigned either "value_1" or "value_2", depending on whether varB exists and does not contain a NULL value.

Example 2: A conditional variable defined with a variable reference

varname = ? "$(value_1)"

In this case, varname is null if value_1 is null, otherwise varname is set to value_1.

Example 3: A conditional variable used with a LIST statement and WHERE clause

%DEFINE{
%list " AND " where_list
where_list    =  ? "custid = $(cust_inp)"
where_list    =  ? "product_name LIKE '$(prod_inp)%'"
where_clause  =  ? "WHERE $(where_list)"
%}
 
%FUNCTION(DTW_SQL) mySelect() {
   SELECT * FROM prodtable $(where_clause)
%}

Conditional and LIST variables are most effective when used together. The above example shows how to set up a WHERE clause in the DEFINE block. The variables cust_inp and prod_inp are HTML input variables passed from the Web browser, usually from an HTML form. The variable where_list is a LIST variable made of two conditional statements, each statement containing a variable from the Web browser.

If the Web browser returns values for both variables cust_inp and prod_inp, for example, IBM and 755C, the where_clause is:

WHERE custid = IBM AND product_name LIKE '755C%'

If either variable cust_inp or prod_inp is null or not defined, the WHERE clause changes to omit the null value. For example, if prod_inp is null, the WHERE clause is:

WHERE custid = IBM

If both values are null or undefined, the variable where_clause is null and no WHERE clause appears in SQL queries containing $(where_clause).


[ Top of Page | Previous Page | Next Page | Index ]