SQL Server Management Studio 连接默认为“master”选择特定于数据库的对象时
在 SQL Server 2008 R2 Management Studio 中,如果我右键单击特定数据库内的对象并选择“选择前 1000 行 ..”,则查询窗口的数据库连接始终在“master”上打开,而表名称完全是限定为[数据库].[dbo].[表]。这使得不可能跳入并调整此查询并向语句插入联接等,而无需完全限定我添加的所有内容,或添加 USE 语句,或从下拉菜单中选择数据库。
是否有设置或其他设置可以使查询窗口使用所选对象浏览器数据库的数据库连接打开,而不是连接到“master”,并且不完全限定查询文本中的对象数据库?我意识到我可以将 SQL 连接注册为默认数据库,但实际上我们每周都会访问多个新数据库——在给定的一个月内我将接触数十个数据库——因此管理多个数据库注册将很困难。我宁愿 SSMS 只是连接到指定的数据库。可能且直接吗?
In SQL Server 2008 R2 Management Studio, if I right-click on an object inside a specific database and choose "Select top 1000 rows ..", the database connection for the query window always opens on 'master' while the table name is fully qualified as [database].[dbo].[table]. This makes it impossible to jump in and tweak out this query and insert joins, etc., to the statement without also fully-qualifying everything I add, or add a USE statement, or select the database from the drop-down menu.
Is there a setting or something that will make query windows open with a database connection of the selected object browser's database rather than connect to 'master', and not fully qualify the object's database in the query text? I realize that I can register my SQL connection to default to my database, but we actually go through multiple new databases every week--in a given month I will have touched tens of databases--so it would be difficult to manage multiple database registrations. I would rather it if SSMS just connected to the specified database. Possible and straightforward?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您通过 Win Auth 进入,在一个组中,正在使用 SA 或其他一些用户 ID,或者处于更改登录名并不是真正的解决方案的情况,并且如果您想要做的只是默认数据库查询编辑器:
在现有打开的查询编辑器中,右键单击,选择“连接”、“更改连接”。
单击“选项”按钮展开选项。
在“连接属性”选项卡中,选择您要连接的数据库。
SSMS 将记住您对该服务器的选择。您可能必须对其他服务器重复此操作,但它确实可以弥补除 master 之外的默认数据库的问题。
If you are going in via Win Auth, are in a group, are using SA or some other userid, or are in a situation where changing your login is not really the solution, AND if all you wish to do is default to a database in the query editor:
In an existing open query editor, right-click, select Connection, Change Connection.
Click the Options button to expand the options.
In the Connection Properties tab, select the database you wish to connect to.
SSMS will remember your selection for that server. You may have to repeat for other servers, but it does remedy having a default database other than master.
SELECT TOP
命令没有此类设置,但您可以通过更改登录的默认数据库来完成此操作。如果您经常对各种数据库执行此操作(就像更改注册,正如我刚刚注意到您已经概述的那样),这会很乏味。而不是使用
SELECT TOP 1000
(这除了不会将您置于正确的数据库上下文中之外,还会放置一个TOP
,我假设您只是将其删除为好吧),您应该右键单击该表并选择Script Table as
>选择
> >新查询窗口
。这会将上下文放入正确的数据库中,添加USE
命令,没有TOP
并且不会为表名添加数据库前缀。There is no such setting for the
SELECT TOP
command, but you may be able to do this by changing the default database for your login. This is tedious if you're doing this often for various databases (much like changing the registrations, as I just noticed you already outlined).Instead of using
SELECT TOP 1000
(which in addition to not putting you in the right database context, also puts aTOP
in that I assume you're just going to remove as well), you should right-click the table and chooseScript Table as
>SELECT to
>New Query Window
. This puts the context in the right DB, adds aUSE
command, doesn't have aTOP
and doesn't database-prefix the table name.如果您希望查询窗口默认连接到某个数据库,请在 SSMS 中转到“安全”->“登录,选择用于连接到该服务器的登录名,然后查看属性窗口。在“常规”页面中,将默认数据库从“master”更改为您要连接的数据库。
If you want query window connects to some database by default, in SSMS go to the Security -> Logins, select the login that you use to connect to this server, and loock at the properties window. In page 'general' change the default database from 'master' to database you want to connect.
您可以在执行查询之前将
USE [数据库名称]
放在查询窗口的顶部。如果执行此操作,则无需完全限定数据库名称。如果您生成任何脚本并对它们进行版本控制,无论如何将其放在顶部是一个很好的做法。它至少可以防止针对错误的数据库错误地执行脚本(例如创建存储过程)。You could just put a
USE [database name]
at the top of the query window prior to executing a query. You do not need to fully qualify the database names if you do this. If you generate any scripts and version control them, this is a good practice to put at the top anyway. It at least prevents executing the script erroneously against the wrong database (say creation of a stored procedure).