49

I have a number of .sql files that I wish to execute through DBeaver. Traditional database development programmes allow the user to edit and run SQL scripts (totally or partially) in the same window, but this is not obvious with DBeaver.

When I open a .sql script some drop down boxes in the button bar appear, that seem to serve as connection selectors. But none of the connections I have defined appear in these drop down boxes. It is possible to open a SQL console on database objects in the Database Navigation view, but not on SQL scripts.

How can I execute a SQL script, totally or partially, against a particular database connection with DBeaver?

Luís de Sousa
  • 5,765
  • 11
  • 49
  • 86
  • hello, Luis, I hope all is well. We have a comment below answering this as running part of code within SQL, maybe we should improve the question and move that answer to another question. See https://stackoverflow.com/a/42005260/5548699 Shall we consider changing question to reflect that multiple files are the case here? Maybe I read incorrectly – Curious Watcher Apr 27 '21 at 12:50
  • @Aleksandras The question is pretty clear in my view: "How can I execute **a** SQL script". Similar in the title. – Luís de Sousa Apr 28 '21 at 12:46
  • Cheers, then `ALT+X` is my choice when running a single script on dbeaver. Updated the answer. – Curious Watcher Apr 28 '21 at 20:35

5 Answers5

58

For larger files, the more optimally it is edit the .sql file in an external editor and then in DBeaver:

  • right-click on your DB
  • chose Tools / Execute script
  • load your .sql file
  • click Start.

This approach is generally more convenient and faster for large files.


To do this without an external editor, you must:

  • set your DB in Active datasource select
  • load your file File / Open file ...
  • run the whole script e.g. using a shortcut Alt+X.
simhumileco
  • 31,877
  • 16
  • 137
  • 115
  • 9
    I checked, dbeaver on ubuntu 18 does not have the option to execute script under tools. – Boniface Pereira May 06 '19 at 05:55
  • 2
    Hi @BonifacePereira, I check it now on 5.2.0 version and the option it is still there. It is important to expand the database branch in the left panel and click the right button of the mouse on the specific database and in this context menu, there is the "Tools / Execute scropts" option. – simhumileco May 06 '19 at 08:08
  • 1
    i found the option, thanks. i was trying to run script that created a database. dbeaver requires the database to be already created to run scripts in it. – Boniface Pereira May 10 '19 at 18:38
  • DBEAVER cannot do executing large sql file. – Saige Zhang Jun 15 '21 at 21:54
  • 1
    Note this seems to have changed in later versions: it is now only accessible from the tasks system. You can create an _execute SQL script_ task (specific to your rdbms) and execute an external file from there. This allows execution of large sql files without opening them and bringing the editor to its knees. – Andy Clapham Jun 29 '23 at 12:26
16

I believe I figured how to do this. First of all, the desired script must be open with the SQL editor. Then one must select the Auto-synch connection with navigator option that is available from the down arrow menu for the Set active connection from database navigator connection button:

enter image description here

In certain cases, this immediately activates the SQL console within the SQL editor panel. If that is not the case then one must go through the Database Navigator and select the desired schema on which to work.

It is then possible to execute a segment of a SQL script (e.g. a query) by selecting it and pressing Ctrl+Enter.

Luís de Sousa
  • 5,765
  • 11
  • 49
  • 86
12

I hope all is well! Great question! I had a similar question when I started working with dbeaver. Here is what I have figured out so far:

  • run an entire single script with ALT+X;
  • run selected part of the code or anything before a semicolon, ; with CTRL+ENTER;
  • run multiple files with Tasks; in dbeaver 21.0:
    • Click menu Database -> Tasks -> Create new task
    • Specify task name;
    • Choose the connection;
    • Select files to run (I had two to exclude, underlined with red);
    • Run the task from same menu and watch dbeaver iterate the code for you.

Attached is a screenshot: running a Database Task on dbeaver 21.0

Screenshot: running a Database Task on dbeaver 21.0

Curious Watcher
  • 580
  • 6
  • 12
  • It seems that the add script dialog doens't find named scripts that are open. Any thoughts? – Marc Jun 14 '21 at 18:15
  • If scripts are open, maybe dbeaver does not consider these as final, but it is only my thinking, I am yet to find an answer, this one is the closest https://dbeaver.io/forum/viewtopic.php?f=3&t=933. Maybe raise a bug? – Curious Watcher Jun 15 '21 at 20:05
5

In DBeaver 21.1.3 Community Ed. I can change database and schema by drop down lists on top toolbar. To automatically change it according to selected database in Database Navigator make tick to Window / Preferences / Editors / Auto-sync editor connection with navigator selection.

Fofola
  • 86
  • 1
  • 3
4

Step 1: Create a New SQL Script by navigating to the appropriate option or using the shortcut (Ctrl + J).

Step 2: Import the SQL Script you've just created by following these steps:

  • Click on the context menu.
  • Navigate to the "File" option.
  • Choose "Import SQL Script" (Shortcut: Shift-Ctrl-Alt-O).

enter image description here

Step 3: Execute the SQL Script you've imported by pressing the execute shortcut (Alt + X).

enter image description here

Bruno Ribeiro
  • 1,280
  • 16
  • 21