Execute SQL statements

Last modified: March 28, 2025

Schema Compare for MySQL enables you to execute SQL scripts in various ways, including running entire scripts, selected blocks of text, or individual statements.

Entire script

To execute an entire script, click Execute on the toolbar or press F5. Alternatively, navigate to the SQL menu and select Execute.

Selected block of text

To execute an arbitrary text block:

1. Select the text block using a mouse or a keyboard.

2. On the toolbar, click Execute button Execute to execute the selected block of code.

Alternatively, select Execute Selection from the shortcut menu of the SQL Editor.

Current statement

To execute the current statement, click Execute Current Statement Execute Current Statement on the toolbar or press F8. Alternatively, right-click the required statement and select Execute Current Statement.

Execute to Cursor

To execute a script to the current position of the cursor:

1. Move the cursor to the required position.

2. Right-click the code and select Execute To Cursor or press Ctrl+Shift+F10.

Execute to cursor

If the statement is complex and contains multiple queries or T-SQL commands, the Execute To Cursor option will execute all statements above the cursor, including the statement where the cursor is placed. All statements that are 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

In this worked 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

In this case, the SELECT Address, SELECT Culture, and SELECT Customer queries will be executed.

Example 3.

Execute to cursor example

In this case, the SELECT Address and SELECT Culture queries will be executed, whereas the SELECT Customer query won’t be executed.

Stop execution

To stop SQL execution, click Stop Execution on the toolbar or press Alt+Pause.

Query execution results

The query execution results can be represented in two ways - in text and grid formats.

To represent results as text, click Results as Text on the toolbar before query execution.

Results as Text

To represent results as a grid, click Results as Grid on the toolbar before query execution.

Results as Grid

SQL Query History

SQL Query History stores the main information about executed SQL statements within a specific timeframe. Additionally, it is possible to identify the user who executed the query along with the time of execution and to provide other valuable information.

You can open the SQL Query History in the following ways:

  • On the toolbar, click Execution History or press Ctrl+Alt+H.
  • On the View menu, select Other Windows > Execution History.

If you point to the Query Text column, you will see the text of the executed script.

SQL Query History

Transactions

A transaction is a single logical unit of work that accomplishes a particular database operation. Each transaction begins with a specific task and ends when all the tasks are successful. In this case, all the changes are committed to the database. If the transaction fails, is canceled, or is rolled back, the database changes are discarded.

In SQL, there are several types of transactions:

  • AutoCommit: A default transaction in SQL. Each statement is considered to be a transaction and is either committed (if it is successful) or rolled back (if it fails).
  • Explicit: A transaction starts with the BEGIN TRANSACTION command and ends with the COMMIT or ROLLBACK commands.
  • Implicit: SQL launches an implicit transaction for each statement after the previous transaction is complete. The implicit transaction is running until you explicitly add the ending statement, either COMMIT or ROLLBACK.

The dbForge tool allows managing transactions through the statements in a SQL document and with the help of menu commands.

Manage transactions through SQL statements

You can manage transactions with the help of two SQL statements BEGIN TRANSACTION and COMMIT/ROLLBACK TRANSACTION. If neither BEGIN TRANSACTION nor COMMIT/ROLLBACK TRANSACTION are specified, statements are executed in the autocommit mode.

To use explicit transactions, first, you need to open a transaction with the BEGIN TRANSACTION statement and then declare the required DMLs. If you execute the above-mentioned statements without COMMIT/ROLLBACK TRANSACTION, the transaction will be open. In this case, no matter what other statements you run, data cannot be added, modified, or deleted in the table.

To use DML statements in the open transaction, you need to run the COMMIT TRANSACTION statement. If, for some reason, you need to cancel the executed statements in the open transaction, you should run the ROLLBACK TRANSACTION statement.

Manage transactions through SQL statements

As you can see, the first two INSERT statements were applied. Though the latter INSERT statement was executed, it was not applied to the table.

Manage transactions with the help of menu commands

In the dbForge tool, you can execute the following operations on the transactions:

  • Begin Transaction: Defines the starting point of the explicit transaction.
  • Rollback: Reverts the database changes to the original state.
  • Commit: Applies the changes to the database.

To use transactions, prior to executing a DDL statement from the SQL document, click Begin Transaction on the Transaction toolbar or on the main SQL menu. Then, click Execute.

BEGIN TRANSACTION commands

After that, the transaction remains open. You can keep on executing SQL statements or roll back all the statements executed earlier by clicking Rollback on the Transaction toolbar or on the main SQL menu. Then, the transaction is closed. Alternatively, instead of the rollback, you can click Commit to apply all the previously executed statements to the database and complete the transaction.

Note

Menu commands define behavior only when executing a SQL document.