Statement execution

This section describes the following features:

Execute the current statement

A specific SQL statement can be executed without selecting it. It is enough to place the mouse pointer in the statement and press Ctrl+K or Ctrl+E, and dbForge SQL Complete will define the statement boundaries itself.

Execute to cursor

This handy feature allows you to execute a script to the current position of the cursor.

To execute a statement to a cursor, position your cursor in the required place of the SQL document and select the Execute To Cursor command from the SQL Complete menu.

Execute to cursor

When the statement contains multiple queries or T-SQL commands, the Execute To Cursor option will run all statements above the cursor, including the one where the cursor is positioned. Any statements below the cursor will not be executed.

Note

To better understand the Execute To Cursor option, study the following examples:

Example 1.

Execute to cursor example

The SELECT Address and SELECT Culture queries will be executed. Please note, that the WHERE clause of the SELECT Culture query will also be executed. The SELECT Customer query won’t be executed.

Example 2.

Execute to cursor example

The SELECT Address, SELECT Culture, and SELECT Customer queries will be executed.

Example 3.

Execute to cursor example

The SELECT Address and SELECT Culture queries will be executed whereas the SELECT Customer query won’t be executed.

Stop execution

Multi-database execution

With SQL Complete for SQL Server, you can execute a script against multiple databases. To use the multi-scripts feature, right-click anywhere in the SQL document and select Run On Multiple Targets.

Run On Multiple Targets

The Run On Multiple Targets wizard will open.

Run On Multiple Targets wizard

Here you need to:

1. Specify the database server connection.

Note

You can execute a script against multiple databases within a single operation only if those databases are hosted on the same database server. However, if you need to execute a script against multiple databases that reside on different servers, you will have to do it in several steps by switching the connection in the wizard.

2. In the File Name field, provide a path to the script file or leave the field empty to execute a script from the SSMS SQL document.

3. Select the databases against which you want to execute the script.

For convenience, you can hide the system databases from the list.

4. Select the Script Execution Mode:

Execute in series - SQL Complete executes the script against the first database in the list. After the execution is completed, the script will be executed against the next database in the list.

Execute in parallel - the script is executed against all the selected databases simultaneously.

SQL Query History

You do not have to save all the queries that have already been executed or need to be executed regularly (e.g., once a month) in a separate file. Instead, you can easily retrieve them from the SQL Query History. It stores the main information about the executed SQL statements for a particular period of time. This helps the user view, edit, and search the queries that were run on the database. It is also possible to see who executed a query and when, as well as get other valuable information.

The SQL Query History information can come in handy for a number of cases. For instance, there might be a need to investigate a particular work case, check the backup history, or recover a specific query if your SQL Server crashes.

The Execution History document is divided into two views:

  • SQL Query History grid
  • Preview pane

The SQL Query History grid consists of the following columns:

  • Status - shows whether the statement was executed successfully or not
  • Query Text - displays the text of the query
  • Database - gives the database name on which the statement was executed
  • Size (in bytes) - indicates the size of the query
  • Executed On - shows the execution timestamp
  • Duration - shows the duration of execution
  • File - provides the statement file name
  • Server - gives the server name where the statement was executed
  • User - shows who initiated the execution

In the Preview pane, you can select a single statement or section of code and copy it using Ctrl+C.

SQL Query History

How to edit an executed SQL query

To edit an executed SQL query, right-click it in the SQL Query History list and select To New SQL Window. This will open the query in a new SQL document, where you make any necessary modifications to its text.

Alternatively, simply double-click the required query in the SQL Query History list.

Editing Query

SQL Query History filters

Each column has a separate set of filters to help you navigate many entries. To open the filter window, point to the column name and click the funnel icon:

SQL Query History column filters

1. Status

Choose the status of the queries to be displayed in the SQL Query History grid.

SQL Query History column filters

2. Query Text

Filter the entries either by choosing from the options on the Values tab or using Text Filters. When using the latter, select the filter from the list and enter the value you are looking for.

SQL Query History column filters

3. Size (in bytes)

Select the minimum and maximum values to filter the entries. To do that, either use the slider on the Values tab or switch to Numeric Filters to fine-tune the filter.

SQL Query History column filters

4. Executed On

Select the date range from the list on the Values tab or configure the filter on the Date Filters tab.

SQL Query History column filters

5. Duration

Select the query duration from the list on the Values tab or configure the filter using the Date Filters tab.

SQL Query History column filters

6. File

Select the desired file from the list on the Values tab. Alternatively, go to the Text Filters tab and configure the filter according to your needs.

SQL Query History column filters

7. Server

Choose one or more servers from the list on the Values tab or go to the Text Filters tab and configure it according to your needs.

SQL Query History column filters

8. User

Choose a user from the list on the Values tab. Alternatively, configure Text Filters according to your needs.

SQL Query History column filters

Above the grid, you will find:

  • a search bar, where you can search out required queries by text
  • From: and To: fields to specify the desired log output timeframe
  • the number of query executions found in your search

Export to CSV allows you to export the SQL Query History as a CSV file:

1. Click Export to CSV.

2. Select a folder to store the CSV file.

3. Enter the file name and click Save.

To learn more about various methods for obtaining SQL Query History, refer to the How to Check SQL Server Query History blog article.

The SQL Query History shortcut menu offers the following options:

Execution shortcut menu

  • Sort Ascending - apply ascending sort order to any column
  • Sort Descending - apply descending sort order to any column
  • Clear All Sorting - reset sorting to default
  • Group By This Column - group completed statements by the selected column
  • Show/Hide Group By Box - drag a column header to the Group By Box field to group data by that column
  • Hide This Column - hide the selected column
  • Column Chooser - drag columns to remove them from the grid or add them back at your convenience
  • Best Fit - adjust the width of a column to fit the value with maximum characters
  • Best Fit (all columns) - ditto for all columns
  • Filter Editor - editor with rich filtering options
  • Show/Hide Auto Filter Row - show or hide a row to filter data directly from the grid

Show auto filter row

Note

SQL Query History is stored in the ExecutedQueries.db file. To specify a path to the folder where this file will be stored, go to SQL Complete menu > Options > SQL Query History. The default path is %appdata%\Devart\dbForge SQL Complete\ExecutedQueries.

Note that this file does not store your connection passwords.

Filter Editor

1. To create a custom filter, right-click the SQL Query History grid and select Filter Editor.

2. In the window that opens, choose the operators that fit your needs:

SQL Query History context menu

Here, you can also add a condition, a group of conditions, or clear all the previously set ones. An alternative way to add a condition is to click a plus button next to the operator.

SQL Query History context menu

3. By clicking the conditions in blue and green, configure the filter according to your needs.

4. Click Apply.

SQL Query History customization

If you want to remove one or more columns from the SQL Query History grid, use the Customization window.

1. Right-click the SQL Query History grid and select Column Chooser.

SQL Query History context menu

2. To customize the layout, drag the columns from SQL Query History to the window that opens.

SQL Query History customization window

3. To return the column, drag it back to the SQL Query History grid.

SQL Query History enables the following actions:

1. Success verification check

This option allows the user to check whether a SQL statement has been successfully executed. Each query in the table has a field with an icon that displays the result of the execution. It is also possible to sort the table entries by this parameter.

2. Log output range

SQL Query History provides the ability to set the date interval in which the queries were output. It is possible to view the entire list for any period, with any selected time range.

3. Group by column

This option allows grouping the completed statements by any required column in the table. To do this, just drag the selected column to the panel above the list. The SQL Query History will display the results immediately.

4. Open in a new SQL window or copy to clipboard

Each user can get access to any SQL statement from the archive to view its code to perform any changes afterward. To do it, right-click a required request and select To New SQL Window or To Clipboard.

5. Sort statements

The feature facilitates various sorting of executed statements. The archive can be sorted by any column that is present in the table. To do it, click the header of the required column.

6. Detailed item info

The SQL Query History has a convenient interface for getting all the necessary information about each statement. It is easy to sort information by each parameter field as well as perform customization of the displayed columns. Select the Column Chooser menu item after right-clicking blank space in the grid header and drag the columns.

7. Search filter

This feature allows filtering executed statements by the text key entry. This option will be very useful for DBAs who often have to work with a large number of requests. Enter the required characters in the Search field, and SQL Query History will display the results immediately.

8. Export to CSV

The SQL Query History can also be exported to the CSV file format.

9. Copy from SQL Query History

You can copy from the lower Query Text pane. To do that, select the text and press Ctrl+C.

Copy from Query Text

In case you wish to copy from the SQL Query History grid, right-click the desired cell and select either To New SQL Window or To Clipboard.

Copy from grid

SQL Query History options

To set up the SQL Query History options, go to the SQL Complete menu and select Options. These include:

  • Checkbox to enable/disable SQL Query History tracking
  • Modifiable options: Store executed queries within… (60 days by default), Max script size to store (1024 KB by default), and Show history at startup from… (the last week by default)
  • Field with the path to the folder where the history is stored
  • Clear SQL Query History.

Option

To learn more about configuring options available for SQL Query History, see Execution History.

Note

In the Express Edition of SQL Complete, SQL Query History is stored for the last 3 days only.

Execution warnings

There is always a risk of losing data and objects (whether accidentally or not) because of untested code, unwanted commands, various code leftovers, and errors that can damage data irreparably.

This is where execution warnings come into play. SQL Complete analyzes the script to be executed for the DELETE, DROP, TRUNCATE, and UPDATE statements, and produces a blocking message if the statements are used in certain types of environments. For instance, the message will be prompted if the user tries to execute a DELETE statement against a production database.

To configure the behavior of execution warnings:

  • Go to Options on the SQL Complete menu.
  • In the left pane of the Options window, click Execution Warnings.

Execution Warnings

In the Execution Warnings window, it is possible to specify the maximum script size to analyze and enable execution warnings for the required environments.

For more information on adjusting the Execution Warnings options, see Execution Warnings.

Note

SQL Complete analyzes every USE statement and prompts an execution warning if the statement refers to a database of the environment for which the execution warning is set.

To disable execution warnings for a certain piece of the code, use the following tags:

--nowarn
--endnowarn

Execution Warnings

Execution notifications

Execution notifications allow you to get informed about the query execution result.

Execution notifications

The notification contains the following information:

  • The name of the document

  • The duration of a query

  • Query execution status:

    - Query completed with errors

    - Query executed successfully

    - Query canceled

You can enable notifications and change settings on the Notifications tab of the Options menu. You can also set a query execution time limit to get notified about queries that exceed it. By default, notifications are on and the notification duration time is 7 seconds.

Execution notifications options

To explore more about configuring the Notifications options, see Notifications.

Note

If a query is running longer than 23:59:59 hours, days are added to the time in the Notification.

Transaction reminders

With this feature enabled, whenever there are open transactions during the query execution, a reminder will pop-up informing you about the number of uncommitted transactions.

The Transaction Reminder window contains the following information: a document name and a number of open transactions.

Transaction reminder

By default, notifications are on. To disable notifications, clear the Notify if execution contains open transactions check box on the Notifications tab of the Options menu.

Transaction reminder options

Note

The VIEW DATABASE STATE permission is required in the database for the feature to work properly.

To explore more about configuring the Notifications options, see Notifications.

Want to Find out More?

Overview

Overview

Take a quick tour to learn all about the key benefits delivered by dbForge SQL Complete for SQL Server.
All Features

All features

Get acquainted with the rich features and capabilities of the SQL Complete in less than 5 minutes.
Request a demo

Request a demo

If you consider employing the SQL Complete for your business, request a demo to see it in action.
Ready to start using dbForge SQL Complete for SQL Server?