Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

Can anyone tell me how can I set an SQL database active? I'm running SQL Server 2012 Express Edition. My objective here is that when I execute a command in the query editor, it will automatically target the database I pointed as active.


1 solution

1. There is a drop-down combo on the toolbar allowing you to select the database to which commands apply.

2. In SQL...

USE db_name

-- Now using db_name as default database.
select * from table_in_db_name
Share this answer
KatsuneShinsengumi 12-Mar-14 8:29am    
The only dropdown list I can see right now is the New Project. Is there by any chance that the dropddown you're talking about only show when a certain event is triggered?

Corporal Agarn 12-Mar-14 8:38am    
In SQL2008 it is the SQL Editor tool bar.
Rob Grainger 12-Mar-14 9:35am    
In SQL Server Management studio, the drop down (combo) usually appears in the toolbar right above the Object Explorer. Its possible you have the toolbar hidden, as Katsune said it is on the SQL Editor toolbar. This can be displayed either by right-clicking on an existing toolbar or from the Toolbars submenu on the View menu. However, the toolbar is usually displayed by default whenever a query is open - I'm surprised it isn't visible. On my installation it is right next to the Execute button on the toolbar.

KatsuneShinsengumi 12-Mar-14 9:45am    
Thanks, it showed after I open a New Query. I can now just open the drop down and choose my database. You're awesome.
Rob Grainger 12-Mar-14 10:29am    
You are welcome.

It is worth remembering how to do this in SQL too (USE db_name), as that will allow you to save a query as a .sql file, open it and run it (or run it from the command line using sqlcmd.exe) without having to remember to set the database using the drop down.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900