Associate SQL Script with Database
Asked Answered
C

4

12

I have been using DBeaver as a replacement for SQL Server Management Studio, and I am loving it. The only thing I cannot figure out is how to associate a script with a database. For example, in SSMS I can right click a database (let's call it A) in the object explorer and click "New Query" (or ctrl+n) to open a script that is active within database A. If I open another script in a different database (B), that script is associated with the database B. When I switch back to the original script, I am back to working with database A without having to manually select from the database list or executing a USE statement. Obviously, switching back to the second script will make database B active again.

Unfortunately, in DBeaver, there appears to be only one active database for all scripts. Is there a way to set this up in DBeaver to act like SSMS in this manner?

Edit: DBeaver refers to individual databases as catalog/schema. That is what I am trying to associate with individual scripts.

Clouded answered 26/7, 2019 at 19:24 Comment(0)
C
1

UPDATE

Version 6.3.1 (2019-12-22) now supports this by default! Here is the first note of the description for 6.3.1:

  • SQL editor:
    • Active database/schema change now affects current editor only

Note: This does seem to have changed some behavior such as "Set active" from the database navigator as well as the "Auto-sync xx with navigator." These two used to take affect to the active database/schema. Now they are tied to the current connection.

Original Answer

For anyone who finds this in the future, I did find a work around that provides the desired behavior. The answer is to use projects. Unfortunately, this means creating a duplicate connection to the server.

There is also another catch here. If you want to set a schema for a specific script, you should avoid setting the schema via the "Database Navigator." This will, however, work if you want to change the schema for a script that is already in the active project. If the script is not in the active project, and you set an active schema through the Database Navigator, it will change the active schema for all open scripts associated with that server. To keep it simple, I try to avoid the Database Navigator all together.

All is not lost by avoiding the Database Navigator. By default, the "Projects" window is a tab right next to the Database Navigator. If you expand (click the + next to the name) [Project Name] -> Connections -> [Connection Name], you will have your list of databases/schema right there. Use this as your new Database Navigator and you are all set.

Clouded answered 6/8, 2019 at 14:18 Comment(0)
N
13

I am using DBeaver community and i have using with multiple DB's. You have to just select Data base from drop down to change DB, it is for current script that open .

i current not have my company system connected , find one sample image from internet . enter image description here

Nadler answered 26/7, 2019 at 20:5 Comment(1)
I probably should have been more clear. I am not talking about database connections but individual databases. Looks like DBeaver refers to them as catalog/schema. It is the drop down that is to the right of that red circle in the picture.Clouded
E
1

I don't know that DBeaver has this capability, but you could always Preface your scripts with the USE DBNAME statement...

Extranuclear answered 26/7, 2019 at 22:41 Comment(0)
C
1

UPDATE

Version 6.3.1 (2019-12-22) now supports this by default! Here is the first note of the description for 6.3.1:

  • SQL editor:
    • Active database/schema change now affects current editor only

Note: This does seem to have changed some behavior such as "Set active" from the database navigator as well as the "Auto-sync xx with navigator." These two used to take affect to the active database/schema. Now they are tied to the current connection.

Original Answer

For anyone who finds this in the future, I did find a work around that provides the desired behavior. The answer is to use projects. Unfortunately, this means creating a duplicate connection to the server.

There is also another catch here. If you want to set a schema for a specific script, you should avoid setting the schema via the "Database Navigator." This will, however, work if you want to change the schema for a script that is already in the active project. If the script is not in the active project, and you set an active schema through the Database Navigator, it will change the active schema for all open scripts associated with that server. To keep it simple, I try to avoid the Database Navigator all together.

All is not lost by avoiding the Database Navigator. By default, the "Projects" window is a tab right next to the Database Navigator. If you expand (click the + next to the name) [Project Name] -> Connections -> [Connection Name], you will have your list of databases/schema right there. Use this as your new Database Navigator and you are all set.

Clouded answered 6/8, 2019 at 14:18 Comment(0)
B
0

My use case is that I want to use the same SQL file across all my DB connections, because the DB schemas are the same, only difference is the host. I finally figured out how to associate an already open SQL file with different DB connections and change the active connection on the fly:

  1. Click Active datasource in window bar at top: enter image description here
  2. Now select the DB connection you want active against which the SQL statements will execute: enter image description here

I save all my SQL files in a folder that is outside of the DBeaver default, so prior to this I had to link such folder in DBeaver. Here are the instructions for that.

Bonnie answered 9/9 at 23:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.