Page 1 of 1

Change 'Owner' (source database) for tables/views in a SQL file

Posted: Thu 19 Mar 2015 13:49
by mysqluser007
I have multiple databases setup with the same tables and views. Is it possible to tell a .sql file which database to use, without having to preface each statement referencing a table/view in the SQL with the DB name? If I do explicitly state the DB name, I would have to find/replace every occurrence when I wish to reference a different DB.

For example, I have MYDB1 and MYDB2. Both have table USERS with fields ID, FIRST, LAST.
My SQL would be something like

SELECT USERS.ID, USERS.FIRST, USERS.LAST FROM USERS

Is there a way to instruct the SQL which DB to look at, without coding the DB into each table/view reference such as:

SELECT USERS.ID, USERS.FIRST FROM MYDB1.USERS

The optimal solution (at least for me) would be something that could be a property that could be set/changed within 'Query Builder' without having to edit the source SQL text.

Re: Change 'Owner' (source database) for tables/views in a SQL file

Posted: Thu 19 Mar 2015 15:17
by alexa
You could specify:

USE database_name

at the beginning of the SQL document or select the database in the 'Database' drop-down menu on the Connection toolbar for the SQL document.

Also, you could create multiple connections in Database Explorer and specify different databases for each connection in the 'Database' field on the 'General' tab of the 'Database Connection Properties' dialog box that can be invoked by right-clicking the connection in Database Explorer and selecting 'Modify Connection' from the popup menu.

Re: Change 'Owner' (source database) for tables/views in a SQL file

Posted: Thu 19 Mar 2015 18:20
by mysqluser007
Thank you, both of these options will provide the solution I need.