Editing and running SQL statements

Use the IBM® Z Database Assistant SQL editor to write, execute, and manage SQL statements and stored procedures. You can also use the editor to evaluate new SQL queries and tune them for optimal performance before you deploy them.

Before you begin

Tip: You can also use natural-language queries in the Assistant chat to generate SQL statements. For more information, see Generating SQL statements in Db2 by using the Assistant chat.

About this task

The SQL editor provides a comprehensive environment for working with SQL statements and stored procedure calls including syntax highlighting, auto-validation, auto-completion, and formatting. You can work with multiple query editor tabs simultaneously, and you can easily run either a collection of queries or individual queries. You can use the copy function to export the results of running SQL queries as a CSV file. You can manage host variables and parameter markers through the editor.

Procedure

  1. From the side navigation panel, select the Db2 for z/OS® subsystem that you want to work with and open the SQL editor view.
    An empty query editor tab opens, which is titled query1.sql by default. You can open additional editor tabs by clicking the plus sign (+) to the right of the current editor tab.
  2. Position your cursor in the query editor tab and start typing an SQL statement.
    The auto-completion feature automatically displays relevant code snippets based on the characters that you've typed.
  3. Click a code snippet to populate the query editor tab with the basic parameters associated with the SQL statement that you are creating.
    As you're writing a statement, you can use the following productivity features:
    • Click the Undo () and Redo () icons to easily remove and re-add changes to a statement.
    • Click the Format SQL icon () to restructure a statement into a format that is more easy to parse.
    • Click the Set default schema icon () to set the schema or to change the existing schema to a new schema. The new schema becomes the default schema for that SQL editor tab.
    • Highlight a section of SQL and click the Copy code icon () to copy the SQL to the clipboard.
  4. Finish writing your statement. When you're ready to run it, click Run all to run all of the statements in the active query editor tab.
    If you've created multiple SQL statements in the same query editor tab, you can run a subset of those statements by highlighting them and clicking Run selected from the action menu.

    If you're running an SQL statement or calling a stored procedure that contains host variables or parameter markers, you'll be prompted to provide values for input variables and parameters. After you provide these values, click Run.

    The query runs and the results are displayed in the Query result tab below the editing tab. The query is also added to the Recent queries side panel, where it can be inserted into the query editor tab by clicking it. The query history persists only for the duration of the current login session.

  5. Click a statement in the Statement column to display the following information:
    • The Details tab displays information about the execution of the statement.
    • The Results tab displays the data that is returned by the statement. This tab provides a search facility for locating specific values and an export facility for saving the results as a CSV file.
    • The Parameters tab displays any host variables or parameter markers that were defined for the statement.
  6. Click the back arrow icon (<) to return to the Query result tab.
  7. To delete the contents of the active query editor tab, select Clear SQL from the action menu.

What to do next

You can use the SQL editor to evaluate and tune the performance of queries before you deploy them. See Optimizing new SQL queries for more information.