IntelliJ IDEA 13.1 Web Help

There is a number of ways to run SQL scripts (i.e. the statements in SQL files) in IntelliJ IDEA. So you can always choose the way that suits you best.

Running an SQL script without opening the database console

When using this way of running the script:

  • You don't need to open the file in the editor. You can select the necessary file in the Project tool window.
  • You can run the script for more than one data source (database).

On the other hand:

  • The statements with parameters, most probably, won't run properly.
  • All the statements contained in the script will be executed. You cannot select which of the statements should be executed and which shouldn't.

To use this way of running the script:

  1. Select the necessary SQL file in the Project tool window, or open the file in the editor.
  2. Do one of the following:
    • Select Run "<file_name>" from the context menu.
    • Press Ctrl+Shift+F10Ctrl+Shift+F10Ctrl+Shift+F10Ctrl+Shift+F10Ctrl+Shift+F10Ctrl+Shift+F10Ctrl+Shift+F10Ctrl+Shift+F10Ctrl+Shift+F10Shift Command F10Shift Command F10Shift Command F10.
  3. In the Choose Data Source pop-up, click the data source to which the script should be applied.

    If you want to run the script for more than one data source, select the data sources of interest in the pop-up and press EnterEnterEnterEnterEnterEnterEnterEnterEnterEnterEnterEnter.

    RunSQLScriptSelectDS

Running SQL statements by means of an intention action

When using the Run Query in Console intention action:

  • You can select which of the statements should be executed.
  • The statements can contain parameters. Prior to running such statements IntelliJ IDEA will ask you to specify the parameter values.
  • Each time you use this intention action, you can select the data source (database) for which the selected statement or statements should be run.

On the other hand:

  • The selected statements are run for only one data source at a time.

To use the intention action:

  1. Open the SQL file of interest in the editor.
  2. Place the cursor within the statement which you want to execute.

    If you want to run more than one statement, select (highlight) the necessary statements. Note that for the intention action to be available, the cursor, as a result, must be within a statement (rather than between the statements).

  3. Do one of the following to see the list of suggested intention actions:
    • Press Alt+EnterAlt+EnterAlt+EnterAlt+EnterAlt+EnterAlt+EnterAlt+EnterAlt+EnterAlt+Enter, Shift+Alt+J or Ctrl+Shift+MAlt EnterAlt EnterCommand 1.
    • Click intentionBulb.
  4. Select Run Query in Console.

    DBSQLAltEnter

  5. Select the console to be used (i.e. the data source for which you want to run the statement or statements).

    DBSQLAltEnterChooseConsole

The database console for the selected data source opens, and the statement or statements are executed.

Note the Console attached message above the status bar. This indicates that the corresponding console (data source) is linked with the SQL file.

DBConsoleAttached

So now, if you use Ctrl+EnterCtrl+EnterCtrl+EnterCtrl+EnterCtrl+EnterCtrl+EnterCtrl+EnterCtrl+EnterCtrl+EnterCommand EnterCommand EnterCommand Enter to execute the statements in the SQL file, IntelliJ IDEA will execute those statements in this console (i.e. for the corresponding data source).

However, if you invoke the intention action (e.g. Alt+EnterAlt+EnterAlt+EnterAlt+EnterAlt+EnterAlt+EnterAlt+EnterAlt+EnterAlt+Enter, Shift+Alt+J or Ctrl+Shift+MAlt EnterAlt EnterCommand 1), IntelliJ IDEA will suggest selecting the data source again.

DBSQLAltEnterSecond

Running SQL statements as in the database console

An SQL file opened in the editor, in fact, can be used as the input pane of the database console. So you can use Ctrl+EnterCtrl+EnterCtrl+EnterCtrl+EnterCtrl+EnterCtrl+EnterCtrl+EnterCtrl+EnterCtrl+EnterCommand EnterCommand EnterCommand Enter to execute the statements.

When using this way of running the statements:

  • You can select which of the statements should be executed.
  • The statements can contain parameters. Prior to running such statements IntelliJ IDEA will ask you to specify the parameter values.

On the other hand:

  • The selected statements are run for only one data source at a time.
  • The statements, generally, are executed in the same console (i.e. for the same data source). However, if more than one console is open, you can select which console should be used from the menu on the status bar.

To use this way of running the statements:

  1. Open the SQL file of interest in the editor.
  2. Place the cursor within the statement which you want to execute.

    If you want to run more than one statement, select (highlight) the necessary statements.

  3. Press Ctrl+EnterCtrl+EnterCtrl+EnterCtrl+EnterCtrl+EnterCtrl+EnterCtrl+EnterCtrl+EnterCtrl+EnterCommand EnterCommand EnterCommand Enter.
  4. Select the console to be used (i.e. the data source for which you want to run the statement or statements).

    DBSQLCtrlEnterFirst

The database console for the selected data source opens, and the statement or statements are executed.

The console is linked with the SQL file. The name of the corresponding data source is shown on the status bar.

DBConsoleAttached

Now if you use Ctrl+EnterCtrl+EnterCtrl+EnterCtrl+EnterCtrl+EnterCtrl+EnterCtrl+EnterCtrl+EnterCtrl+EnterCommand EnterCommand EnterCommand Enter, the corresponding statements will be executed in this console (i.e. for the corresponding data source).

The area on the status bar where the name of the data source is shown provides access to the "console menu". <detach> in this menu, obviously, detached the console from the SQL file.

DBConsoleDetach

If more than one console is currently open, the menu can also be used for selecting the console associated with the SQL file.

DBConsoleMenu

See Also

Procedures:

Language and Framework-Specific Guidelines:

Web Resources: