DataGrip 2023.1 Help

Run queries

You can run Run your code by using shortcuts, toolbar buttons and icons, a context menu, and run/debug configurations. You can run a query from query consoles, scratch files, and user files.

For information about where and how to view query results, see Query results.

The following video shows how to run queries in DataGrip.

Run statements and procedures

Run statements in a query console

You can relate to a query console as to a terminal where you type and run your code.

  1. In the Database Explorer ( View | Tool Windows | Database Explorer) , click the data source.

  2. Press F4 to open a query console. For more information about working with consoles, see Working with consoles.

  3. Type or paste the statement that you want to execute.

  4. Press Control+Enter. Alternatively, click the Execute icon The Execute icon on the toolbar.

    If you have several statements, select whether you want to execute all statements or a single statement. The suggestion list always contains an item for running all the statements.

    Run a query

Run statements from an open file

In DataGrip, you can open and run a file. Limitation for the file size is 20 MB. When you open a file that is larger than 20 MB, you see only first 2.5 MB of the file.

  1. Open the Files tool window (View | Tool Windows | Files) and double-click an SQL file. For more information about attaching directories and files in DataGrip, see User files.

  2. Click the statement that you want to execute.

    Also, you can select (highlight) the fragment of code that you want to execute. It can be a subquery or a group of statements. DataGrip executes only the selection.

  3. Press Control+Enter or select Execute from the context menu.

    To customize query execution settings, click the Сustomize link. Alternatively, open settings by pressing Control+Alt+S and navigate to Database | Query Execution. For more information about settings description, see Query execution.

  4. (Optional) If the SQL file is not connected to a data source, select or create a connection session in the <session> list. For more information about sessions, see Manage connection sessions.

  5. In the Statements window, press Enter to run the selected statement. You can switch between other entries to run another set of statements. Statements that you are going to execute are highlighted in a query editor.

    For another example of running script files, see the following video at youtube.com.

    Run individual statements

Running parameterized statements

If you have parameters in your statement, you must specify the values of the parameters before you execute the statement.

  • To execute a parameterized statement, click the Execute button the Execute button on the toolbar and enter values in the second column. Alternatively, to open the Parameters dialog, click the View Parameters button the View Parameters button.

    Execute a parametrized statement

Run stored procedures

A stored procedure is a set of SQL statements with an assigned name. You can execute stored procedures in PostgreSQL, Microsoft SQL Server, Oracle, and MySQL.

  1. Right-click a stored function that you want to execute.

  2. In the Execute Routine window, type all the necessary parameter values, and click OK.

    Run stored procedures

Run SELECT statements and save results into files

  1. (Optional) If the file is not connected to a data source, select a data source from the list of data sources on the toolbar. Then select the connection session from the Sessions list. For more information about connection sessions, see Change a data source and a connection session.

  2. Right-click a SELECT statement.

  3. Select Execute to File and select the output format.

  4. In the Export Data dialog, specify the extractor that you want to use and other settings. For more information about the Export Data dialog, see Export data.

    Save the result of a SELECT statement into a file

Cancel running statements

Before DataGrip 2022.1, when you clicked the stop button several times, it looked like the query was terminated, but in fact the request continued its execution in the database.

On the first click, DataGrip sent a cancellation request to the database, while on the second click DataGrip canceled the JDBC Driver process to terminate all connections to the data source (deactivate the data source). As a result, DataGrip received an error for the first cancellation request because the connection was lost, not because the cancellation was actually completed.

Starting with DataGrip 2022.1, the progress indicator on the gutter shows a red square while the request is being canceled. A canceling progress bar is shown, which clarifies the data source and the session.

Cancel running statements

DataGrip displays a warning to verify that you want to deactivate the data source. The warning is displayed to ensure safety as deactivation might be potentially dangerous for some processes happening simultaneously. So, if you click the Cancel Running Statements button two times, DataGrip displays a warning where you can terminate the remote process or wait a bit longer.

Also, the IDE displays a warning after 10 seconds of idle canceling.

Canceling running statements

  • Click the Cancel Running Statements button.

    cancel_running_statements_procedure

Canceling connections

Before DataGrip 2022.1, when you attempted to abort a request at the stage of creating a connection, the data source was deactivated. Deactivation means that DataGrip canceled the JDBC Driver process to terminate all connections to the data source.

Starting with DataGrip 2022.1, you can interrupt not only the execution of a request, but also the creation of a connection. This is especially relevant for the very first query in the console, which also creates a connection.

You can now stop the process of creating a connection without deactivating the data source: if you click the Cancel Running Statements button while the connection is being created, the connection will be stopped, and you will see the Connection canceled message.

Canceling connections

Resolve modes

In order to run SQL scripts, treat the usages of the database objects as links, and provide you with correct code completion, DataGrip must resolve the database objects in your code to a certain context. By default, IDE uses one of the following as a starting point to resolve the database objects to: <schema> selector, or the default database (if none of the above is set).

Depending on whether your code is a single script or it has multiple unconnected SQL statements, select one of the following resolve modes to resolve the database objects properly:

  • Playground: default mode for query consoles. All the database objects are resolved to the same context.

    Use this mode if your code is a set of unconnected SQL statements which are not meant to be run as a single script.

  • Script: default mode for local SQL files. The USE SQL statement (SET search_path TO for PostgreSQL) overrides the default context that the database objects are resolved to.

    Use this mode if your code is meant to be run as a single script.

View history of executed statements

Using the History dialog

DataGrip stores history of all statements that you have run.

  • To open the History dialog, click the History button or press Control+Alt+E.

    In the History dialog, you can perform the following operations:

    • To filter information, start typing your search query.

    • To paste the query from the History dialog to the console, double-click the query in the left pane of the History dialog.

    • To delete a record from history, select the record and press Delete.

    View history of executed queries

View results

Usually, when you run a query, you receive results in a table format. Each statement appears in a separate tab in the Services tool window. For example, if you have two SELECT statements in the query, you will see two tabs in the tool window.

For more information about viewing query results, refer to Query results.

Result tab in the Services tool window

Tips and tricks

Browse query history

  • To view history of all the queries that you ran in a query console, click the Browse query history button (the Browse query
              history button). Alternatively, press Control+Alt+E.

    Browse the query history
Last modified: 21 June 2023