Using SQL Builder for Database Queries

SQL Builder is a powerful visual tool that you can use to build, test, and save SQL statements for use in application data queries. You can copy a SQL code block directly into your application or insert it into a cfquery tag. You can also use SQL Builder to test your queries.

To open SQL Builder, do one of the following:

The SQL Builder user interface

The following graphic shows the SQL Builder user interface:

The SQL Builder is divided into these sections:

Writing SQL statements

When SQL Builder opens, it displays a SELECT statement by default, since this is the most common type of query. If you have not yet selected a table for your query, you are prompted to select one.

SQL Builder supports these types of SQL statements:

To change the type of SQL statement:

  1. Open SQL Builder.

    For instructions, see the opening SQL Builder procedure earlier in this section.

  2. Select a statement type from the SQL Builder toolbar.

Building a SELECT statement

SQL SELECT statements let you specify data from which to build a recordset.

To create a SELECT statement:

  1. Open SQL Builder.

    For instructions, see the opening SQL Builder procedure earlier in this section.

  2. Select a table to query.
  3. Do one of the following:
  4. Drag the columns to display onto the Column section of the Properties pane.
  5. (Optional) Set additional query attributes in the Properties pane.
  6. Save the query or insert it into a page.

To use a variable in a SQL WHERE clause:

  1. Open SQL Builder.

    For instructions, see the opening SQL Builder procedure earlier in this section.

  2. In the Properties pane, click in the row for a column, in the Criteria column.
  3. In the Select box, select a variable syntax.
  4. Double-click on a variable placeholder name and enter a value.

To save a query:

  1. Open SQL Builder.

    For instructions, see the opening SQL Builder procedure earlier in this section.

  2. On the Query toolbar, click the Save Query button.
  3. In the dialog box that appears, enter a name for the query.
  4. Click Save.

Saved queries are stored on the database server. They can be edited and used by anyone who has access to the server.


Note

ColdFusion Studio does not update the Properties and Table panes when you make changes in the SQL pane. If you edit a SQL statement in the SQL pane and save it, and then modify the Table pane or change any values in the Properties pane, a new SQL statement is generated which overwrites the changes that you made in the SQL pane.


Inserting SQL into a page

This section describes how to insert new and saved queries into a page in the Editor.

To insert a new query, do one of the following:

To insert a saved query, do one of the following:

  1. In the Resources window, click the Database tab.
  2. Open a data source.
  3. Open the Queries folder in the data source.
  4. Drag and drop the query onto the page in the Editor.

or

  1. On the CFML Basic QuickBar, click the CFQUERY button.

    You can also select CFQUERY from the Tag Chooser.

  2. Enter a name for the query.
  3. Click the Insert Query button.
  4. In the select dialog box, select a server.
  5. In the data source that you want to use, open the Queries folder.
  6. Select a query and click Insert.
  7. In the CFQUERY tag editor, click Apply.

    The query appears in the Editor.

Testing and editing queries

You can refine SQL statements after constructing them.

Testing queries

Before inserting a query into an application, we recommend that you test it in SQL Builder. The following procedure explains how to run a query for testing.


Caution

Only test SELECT statements. When you click the Run Query button, the SQL statement is processed. If you run an INSERT, UPDATE, or DELETE statement, the changes that you coded in the SQL statement are made in the data source.


To run a query in SQL Builder:

  1. Click the Run Query button.

    The Run Query button looks like this:

  2. ColdFusion Studio prompts you to enter values for the variables in the query.

Editing queries

If you save a query, you can edit it later. However, pages containing the query are not automatically updated. To make the changes take effect, you must reinsert the query into the pages.

To edit a query:

  1. Open the query folder for the data source that you want to use.
  2. Double-click a query.
  3. Make changes in the Table pane and the Properties pane.
  4. Save the updated query.

Note

ColdFusion Studio does not update the Properties and Table panes when you make changes in the SQL pane. If you write a query in the SQL pane and save it, the Properties and Table panes show no changes. But the SQL pane displays the SQL generated from changes made in the Properties and Table panes.


LiveDocs comments are not longer enabled for ColdFusion 5.0. Please use one of the following resources instead.

ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 5.0