12. Report-Writer Statements : Report Setup Statements : .Query Statement—Indicate Start of a Query : Examples
 
Share this page                  
Examples
1. You set up the query as:
.query
    select *
        from emp
        where salary > $sal
        and dept = '$dept'
You invoke the command:
report mydb myrep (sal = 50000, dept = 'toy')
Enclose toy within single quotes to identify it to Report-Writer as a string. Report-Writer strips off these quotes before passing it to the query.
Note:  If your operating system requires it, enclose the parenthetical clause within double quotes:
report mydb myrep "(sal = 50000, dept = 'toy')"
Report-Writer executes the following query:
select *
    from emp
    where salary > 50000
    and dept = 'toy'
2. Consider a table called account owned by davis with columns including customer number, customer name, checking, and savings. You have separate fields for checking and savings accounts on one row, because most customers have both a savings and a checking account with the bank.
If you want to write one report specification that prints either the savings or checking account balances with a single query, you could code a .query statement similar to the following, using the select column_list as resultcolumn_list construct:
.delimid
.declare account_type = c10 with prompt
    'Please enter the type of account:'
.query
    select "customer number", "customer name",
        $account_type as val
      from davis."bank accounts"
The column names and table name in the preceding code must be double‑quoted, according to standard rules for specifying delimited identifiers.
You can invoke the preceding query with the command:
report otherdb repname
At execution, Report-Writer issues the following prompt and, in this example, the user enters the column name, savings:
Please enter the type of account: savings
The following query would be executed, which selects values from the savings column and assigns them to the result column name, val:
select "customer number", "customer name", savings as val
    from davis."bank account"
To print the value of the column, savings, refer to the result column name, val:
.println val