Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a SQL file as a Console in DataGrip

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"

like image 578
Feety Avatar asked Dec 12 '16 19:12

Feety


2 Answers

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

like image 120
moscas Avatar answered Sep 28 '22 18:09

moscas


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.

like image 24
mgrollins Avatar answered Sep 28 '22 18:09

mgrollins