Pages

Wednesday, May 9, 2012

Never hit the wrong database again in the Query window

It’s easy to do. You create an object and later wonder why it’s not there. This can happen if you’re in Query Analyzer or the Query window in Microsoft SQL Server Management Studio and forget to select the correct database in the dropdown box. In some cases, you may have even selected the correct database, but if you’re working with SQL Server Express, it may connect you to the wrong database when you reconnect.

To avoid this problem, get into the habit of starting your query pages with a Use statement, such as:

Use my_db

Those two words may actually take less time to type than it takes to use the mouse and select the my_db database from the dropdown.

Best of all, if you open a SQL file that you saved previously—or if the Query window reconnects you to the wrong database—the Use statement at the top of the page will ensure that you’re executing your SQL statements against the correct database.



No comments:

Post a Comment

Note: Only a member of this blog may post a comment.