 |
SoftTree Technologies
Technical Support Forums
|
|
Author |
Message |
pfg1114
Joined: 30 Jun 2021 Posts: 32
|
|
How to switch the database automatically? |
|
How to switch the database automatically?

|
|
Thu May 04, 2023 5:09 am |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7948
|
|
|
|
I'm not sure I completely understand. If you mean the default database, you can choose it in SQL Assistant options, in connection settings, or you can use in connection the Bootstrap queries options and enter
USE db_test
or whatever database you use.
Generally speaking, the Intellisense follows the current database. It internally uses the Context query (you can see it in the Options) to find out current database, part of the query is executing DATABASE() function to get the current database name. It's not important which database is selected in MySQL Query Browser database navigator, it's important which database is current in the editor, which is what provides the context.
|
|
Thu May 04, 2023 8:03 am |
|
 |
pfg1114
Joined: 30 Jun 2021 Posts: 32
|
|
|
|
 |
 |
I'm not sure I completely understand. If you mean the default database, you can choose it in SQL Assistant options, in connection settings, or you can use in connection the Bootstrap queries options and enter
USE db_test
or whatever database you use.
Generally speaking, the Intellisense follows the current database. It internally uses the Context query (you can see it in the Options) to find out current database, part of the query is executing DATABASE() function to get the current database name. It's not important which database is selected in MySQL Query Browser database navigator, it's important which database is current in the editor, which is what provides the context. |
After switching the database, the intelligent prompt does not display the context information in the database where the current editor is located.
In other words: the default database is: mysql, when I switch to the USE DB_TEST () database, the database where the editor is located is db_test, but the intelligent prompt does not display the current context information.
Can you understand what I mean?

|
|
Thu May 04, 2023 11:17 am |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7948
|
|
|
|
The connection between MySQL Workbench and SQL Assistant isn't shared. SQL Assistant doesn't know when database is changed in MySQL Workbench. It's a limitation of the plug-in. Workbench doesn't provide an interface or API to obtain its current connection details like current database.
|
|
Thu May 04, 2023 12:13 pm |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7948
|
|
|
|
To change the database in SQl Assistant current connection you can execute the same USE test_db using Ctrl+Shift+F9, or using its menus.
Ctrl+F7 can be used to show its own database explorer. There are other options.
Talking about code execution using SQL Assistant, its datagrids provide many advanced features as compared to simple data grids in MySQL Workbench.
|
|
Thu May 04, 2023 1:58 pm |
|
 |
pfg1114
Joined: 30 Jun 2021 Posts: 32
|
|
|
|
 |
 |
To change the database in SQl Assistant current connection you can execute the same USE test_db using Ctrl+Shift+F9, or using its menus.
Ctrl+F7 can be used to show its own database explorer. There are other options.
Talking about code execution using SQL Assistant, its datagrids provide many advanced features as compared to simple data grids in MySQL Workbench. |
Ctrl+Shift+F9 Execute SQL Code.
How Hot key is change the database in SQL assistant current connection?
or Click which menu item? thanks!!!

|
|
Fri May 05, 2023 2:07 am |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7948
|
|
|
|
In my opinion, simplest and fastest is typing U in the editor, selecting USE key word in the prompt then selecting database name from the prompt, highlighting that text and pressing Ctrl+Shift+F9
If this is something you use often, I would suggest creating a code snippet to automate this process. Here is a screenshot demonstrating how to do that, follow the enumerated steps. The required snippet code is
 |
 |
$$
USE $OBJECT(schema, ins_schema)$;
SELECT 'Current database is $DB$';
$$ |
To access the snippets, right-click in the editor, select SQL Assistant -> Code snippets -> Modify Code snippets... menu.
1. Select MySQL
2. Right-click in the snippets list and select Add...
3. Name it sd for (s)elect (d)atabase, or something else, it's up to you.
4, 5, 6 Follow steps on the screenshot
If you're no sure about the steps pictured above, please let us know.
On the other hand, changing database isn't really required, it's just a schema name, you can refer to table names and other names using schema_name.object_name notation, in that case which database is current or selected in database connection settings isn't relevant.
|
|
Fri May 05, 2023 8:25 am |
|
 |
pfg1114
Joined: 30 Jun 2021 Posts: 32
|
|
|
|
 |
 |
In my opinion, simplest and fastest is typing U in the editor, selecting USE key word in the prompt then selecting database name from the prompt, highlighting that text and pressing Ctrl+Shift+F9
If this is something you use often, I would suggest creating a code snippet to automate this process. Here is a screenshot demonstrating how to do that, follow the enumerated steps. The required snippet code is
 |
 |
$$
USE $OBJECT(schema, ins_schema)$;
SELECT 'Current database is $DB$';
$$ |
To access the snippets, right-click in the editor, select SQL Assistant -> Code snippets -> Modify Code snippets... menu.
1. Select MySQL
2. Right-click in the snippets list and select Add...
3. Name it sd for (s)elect (d)atabase, or something else, it's up to you.
4, 5, 6 Follow steps on the screenshot
If you're no sure about the steps pictured above, please let us know.
On the other hand, changing database isn't really required, it's just a schema name, you can refer to table names and other names using schema_name.object_name notation, in that case which database is current or selected in database connection settings isn't relevant. |
USE $OBJECT(schema, ins_schema)$;
There is no effect after the code is executed.
|
|
Sat May 06, 2023 5:26 am |
|
 |
pfg1114
Joined: 30 Jun 2021 Posts: 32
|
|
|
|
 |
 |
In my opinion, simplest and fastest is typing U in the editor, selecting USE key word in the prompt then selecting database name from the prompt, highlighting that text and pressing Ctrl+Shift+F9
If this is something you use often, I would suggest creating a code snippet to automate this process. Here is a screenshot demonstrating how to do that, follow the enumerated steps. The required snippet code is
 |
 |
$$
USE $OBJECT(schema, ins_schema)$;
SELECT 'Current database is $DB$';
$$ |
To access the snippets, right-click in the editor, select SQL Assistant -> Code snippets -> Modify Code snippets... menu.
1. Select MySQL
2. Right-click in the snippets list and select Add...
3. Name it sd for (s)elect (d)atabase, or something else, it's up to you.
4, 5, 6 Follow steps on the screenshot
If you're no sure about the steps pictured above, please let us know.
On the other hand, changing database isn't really required, it's just a schema name, you can refer to table names and other names using schema_name.object_name notation, in that case which database is current or selected in database connection settings isn't relevant. |
USE $OBJECT(schema, ins_schema)$;
There is no effect after the code is executed.
You can try it!
|
|
Sat May 06, 2023 5:27 am |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7948
|
|
|
|
The required macro code is different. The single line you tried is not sufficient. There was a reason for why the suggested macro had 4 lines of code, not 1
Please update the snippet code to the following. It should work. Note that leading and trailing $$ are part of the snippet, it's $$..$$ macro, you can find more details about how it works in the documentation
 |
 |
$$
USE $OBJECT(schema, ins_schema)$;
SELECT concat('Current database is ', database());
$$ |
After you update the snippet code, please restart Workbench.
|
|
Sat May 06, 2023 11:52 am |
|
 |
pfg1114
Joined: 30 Jun 2021 Posts: 32
|
|
|
|
 |
 |
The required macro code is different. The single line you tried is not sufficient. There was a reason for why the suggested macro had 4 lines of code, not 1
Please update the snippet code to the following. It should work. Note that leading and trailing $$ are part of the snippet, it's $$..$$ macro, you can find more details about how it works in the documentation
 |
 |
$$
USE $OBJECT(schema, ins_schema)$;
SELECT concat('Current database is ', database());
$$ |
After you update the snippet code, please restart Workbench. |
Workbench has been restarted.
|
|
Sun May 07, 2023 10:44 pm |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7948
|
|
|
|
The snippet you quoted above is this
 |
 |
$$
USE $OBJECT(schema, ins_schema)$;
SELECT concat('Current database is ', database());
$$ |
The snippet on your screenshot is slightly different, it returns database name before the change. The above returns it after the change.
|
|
Sun May 07, 2023 11:23 pm |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|