Creating a WITH Statement

Use SQL Query Builder to create a WITH statement that you can reference from a SELECT statement. A WITH statement comprises one or more common table expressions and a SELECT statement. A common table expression defines a named result table that you can specify as a table in the FROM clause of a subsequent SELECT statement.

Prerequisites 

  1. Connect to the data source through a connection profile.
  2. Open a new or existing SQL file, and be sure to select the connection profile Type, Name, and Database.
  3. In SQL Query Builder, create a SELECT statement.
Note: Some syntactic variations might exist between your database and the SQL syntax that SQL Query Builder supports; consequently, some SQL Query Builder features might not work with your database.
  1. In the Outline pane, right-click SELECT Statement and select Add Common Table Expression (WITH).

    SQL Query Builder adds the WITH statement code in the SQL Source pane and to the tree in the Outline pane.

  2. In the Outline view, expand the WITH statement tree, click SELECT node contained inside the WITH node, and define its SELECT statement.

    SQL Query Builder creates a temporary table on which the main SELECT statement is based.

  3. Add a table.
    1. (Optional) Select the WITH node and enter a name for the temporary Table.
    2. Select columns from the table.
    3. (Optional) Select the WITH node and enter names for each Column in the temporary table.
  4. In the Outline view, click the SELECT statement at the bottom of the outline tree.

    The SELECT statement is based on the temporary table just defined.

  5. Add the temporary WITH table and/or any other tables and select columns from the tables.

    The WITH table is now listed as a choice in the Add Table dialog.

  6. (Optional) To run the SQL code, in the SQL Source pane, right-click and select Run SQL.

    View the results in the SQL Results tab.

  7. Click OK to exit SQL Query Builder and to return to the SQL File Editor, where the SQL code displays in the editor window.

    Clicking Cancel results in no changes to the SQL code in the SQL File Editor. You lose any changes you made in SQL Query Builder.

Related tasks
Creating a SELECT Statement
Creating a FULLSELECT (UNION) Statement
Creating Joins