7

I have a sql file saved off that I want to open and get results back from a database I am connected to in DataGrip. When I open the file, it opens it up as a file scope, and not as a console scope, and I cannot run the sql against the database. I have to open up a new database console, copy my SQL into the database console, and run it from there. Make any changes I want to the SQL, and save it back to the file. I just want to open the file up as a console.

I have to be doing this wrong. What am I missing? How can I open a SQL file from disk in DataGrip and execute the query in a console-like fashion?

Example: I have a SQL file: "customer.sql" that contains "Select top 10 * from customer". When I open up "customer.sql", it opens up as a file in DataGrip with no execution or associated database relationship. I want to run the "customer.sql" contents of "select top 10 * from customer" against one of my database connections. Make my changes to the query "select top 11 * from customer" and save it back to "customer.sql"

Feety
  • 355
  • 2
  • 5
  • 11

2 Answers2

8

There is a new option in DataGrip 2017.2 : Attach console.

You can find via Ctrl+Shift+A or by opening the context menu for a file. After performing this action, this file can be run against the console. Switching consoles will be useful if you want to run the same script in several data sources.

enter image description here

moscas
  • 9,064
  • 36
  • 42
  • The attach console window only has a subset of my available data sources. How do you get all of your data sources (instead of just a subset) to appear in the "Attach Console" window? – BenDundee Aug 29 '17 at 23:35
  • 1
    @BenDundee please try 2017.2.2 as soon as it's released – moscas Sep 01 '17 at 08:10
  • Yep, someone from DataGrip responded to my pleas on Twitter :) Thanks! – BenDundee Sep 01 '17 at 14:04
  • @BenDundee It was me :) – moscas Sep 04 '17 at 08:11
  • 1
    Something I found that was buried in the documentation. If you have multiple DBs (mssql, postgres) and youhave a default "dialect" specified you may have to change your dialect before you can "attach" the console associated with that dialect. – happymacarts Oct 11 '17 at 22:43
  • Now you can choose others as well independent from a dialect. – moscas Oct 13 '17 at 10:59
4

Here is the documentation from "Running SQL Script Files" help page for DataGrip 2016.3 to run your own file and get results, as if running SQL from a console.

It appears that any changes to your file are saved automatically, even if the script is not executed, and then previous versions are available through "Local History" which is a feature of most if not all of the JetBrains IDE's.

Executing individual statements

When running individual statements contained in an SQL file: – The statements can contain parameters. Prior to running such statements DataGrip will ask you to specify the parameter values.

On the other hand: – The statements are run for only one data source at a time.

To run a statement or statements:

  1. Open the SQL file of interest in the editor.
  2. Place the cursor within the statement you want to execute. If you want to run more than one statement, select (highlight) the necessary statements.

  3. Do one of the following: – Press Ctrl + Enter or select Execute from the context menu. – Press Alt + Enter or click the Intention Bulb, and select Run query in console.

  4. Select the database console to be used.

The statement or statements are executed using the selected console. The corresponding console is associated with the file. The name of the associated console is shown on the Status bar.

mgrollins
  • 641
  • 3
  • 9