[英]How to change default database in SQL Server without using MS SQL Server Management Studio?
I dropped a database from SQL Server, however it turns out that my login was set to use the dropped database as its default.我从 SQL 服务器删除了一个数据库,但事实证明我的登录设置为默认使用删除的数据库。 I can connect to SQL Server Management Studio by using the 'options' button in the connection dialog and selecting 'master' as the database to connect to.
通过使用连接对话框中的“选项”按钮并选择“主”作为要连接的数据库,我可以连接到 SQL Server Management Studio。 However, whenever I try to do anything in object explorer, it tries to connect using my default database and fails.
但是,每当我尝试在 object 资源管理器中执行任何操作时,它都会尝试使用我的默认数据库进行连接并且失败。
Does anyone know how to set my default database without using object explorer ?有谁知道如何在不使用 object explorer的情况下设置我的默认数据库?
What you can do is set your default database using the sp_defaultdb system stored procedure.您可以做的是使用 sp_defaultdb 系统存储过程设置默认数据库。 Log in as you have done and then click the New Query button.
像您所做的那样登录,然后单击“新建查询”按钮。 After that simply run the sp_defaultdb command as follows:
之后只需运行 sp_defaultdb 命令,如下所示:
Exec sp_defaultdb @loginame='login', @defdb='master'
To do it the GUI way, you need to go edit your login.要以 GUI 方式进行操作,您需要编辑您的登录信息。 One of its properties is the default database used for that login.
它的属性之一是用于该登录的默认数据库。 You can find the list of logins under the Logins node under the Security node.
您可以在安全节点下的登录节点下找到登录列表。 Then select your login and right-click and pick Properties.
然后选择您的登录名并右键单击并选择“属性”。 Change the default database and your life will be better!
更改默认数据库,您的生活会更好!
Note that someone with sysadmin privs needs to be able to login to do this or to run the query from the previous post.请注意,具有 sysadmin privs 权限的人需要能够登录才能执行此操作或运行上一篇文章中的查询。
Alternative to sp_defaultdb (which will be removed in a future version of Microsoft SQL Server ) could be ALTER LOGIN
:替代 sp_defaultdb ( 将在 Microsoft SQL Server 的未来版本中删除)可以是
ALTER LOGIN
:
ALTER LOGIN [my_user_name] WITH DEFAULT_DATABASE = [new_default_database]
Note: unlike the sp_defaultdb
solution user and database names are provided without quotes.注意:与
sp_defaultdb
解决方案不同, sp_defaultdb
用户和数据库名称不带引号。 Brackets are needed if name had special chars (most common example will be domain user which is domain\\username
and won't work without brackets).如果名称有特殊字符,则需要括号(最常见的例子是域用户,它是
domain\\username
,没有括号就不能工作)。
Thanks to this post , I found an easier answer:感谢这篇文章,我找到了一个更简单的答案:
Open Sql Server Management Studio打开 SQL Server 管理工作室
Go to object Explorer -> Security -> Logins转到对象资源管理器 -> 安全 -> 登录
Right click on the login and select properties右键单击登录并选择属性
And in the properties window change the default database and click OK.并在属性窗口中更改默认数据库并单击确定。
If you don't have permissions to change your default DB you could manually select a different DB at the top of your queries...如果您无权更改默认数据库,则可以在查询顶部手动选择不同的数据库...
USE [SomeOtherDb]
SELECT 'I am now using a different DB'
Will work as long as you have permission to the other DB只要您对另一个数据库有权限就可以工作
I'll also prefer ALTER LOGIN
Command as in accepted answer and described here我也更喜欢
ALTER LOGIN
命令,如已接受的答案并在此处描述
But for GUI lover但对于 GUI 爱好者
Tired of reading!!!读腻了!!! just look at following
看看下面
Click on options on the connect to Server dialog and on the Connection Properties, you can choose the database to connect to on startup.单击连接到服务器对话框和连接属性上的选项,您可以选择在启动时连接到的数据库。 Its better to leave it default which will make master as default.
最好将其保留为默认值,这将使 master 成为默认值。 Otherwise you might inadvertently run sql on a wrong database after connecting to a database.
否则,您可能会在连接到数据库后无意中在错误的数据库上运行 sql。
In case you can't login to SQL Server:如果您无法登录 SQL Server:
sqlcmd –E -S InstanceName –d master
Reference: https://support.microsoft.com/en-us/kb/307864参考: https : //support.microsoft.com/en-us/kb/307864
This may or may not exactly answer the question, but I ran into this issue (and question) when I had changed my account to have a new database I had created as my "default database".这可能会也可能不会完全回答这个问题,但是当我更改我的帐户以拥有我创建的新数据库作为我的“默认数据库”时,我遇到了这个问题(和问题)。 Then I deleted that database and wanted to test my creation script, from scratch.
然后我删除了该数据库并想从头开始测试我的创建脚本。 I logged off SSMS and was going to go back in, but was denied -- cannot log into default database was the error.
我注销了 SSMS 并打算重新登录,但被拒绝了——无法登录到默认数据库是错误。 D'oh!
哦!
What I did was, on the login dialog for SSMS, go to Options, Connection Properties, then type master
on the "Connect to database" combobox.我所做的是,在 SSMS 的登录对话框中,转到选项、连接属性,然后在“连接到数据库”组合框中键入
master
。 Click Connect.单击连接。 Got me in. From there you can run the command to:
让我进来。从那里你可以运行命令:
ALTER LOGIN [DOMAIN\useracct] WITH DEFAULT_DATABASE=[master]
GO
With the MSSQL queries below , you can change database on sqlcmd :使用下面的 MSSQL 查询,您可以更改sqlcmd上的数据库:
USE testdb
GO
Then, you can check the currently used database :然后,您可以检查当前使用的数据库:
SELECT DB_NAME()
GO
testdb
测试数据库
Then, you can show all the existed databases :然后,您可以显示所有现有的数据库:
SELECT name FROM master.sys.databases
GO
master
掌握
tempdb
临时数据库
model
model
msdb
数据库
testdb
测试数据库
In addition, if you don't specify a database on sqlcmd , "master" database is used by default .此外,如果您没有在sqlcmd上指定数据库,则默认使用“master”数据库。
If you use windows authentication, and you don't know a password to login as a user via username and password, you can do this: on the login-screen on SSMS click options at the bottom right, then go to the connection properties tab.如果您使用 Windows 身份验证,并且您不知道通过用户名和密码作为用户登录的密码,您可以这样做:在 SSMS 的登录屏幕上单击右下角的选项,然后转到连接属性选项卡. Then you can type in manually the name of another database you have access to, over where it says , which will let you connect.
然后您可以手动输入您有权访问的另一个数据库的名称,在它显示的位置,这将允许您连接。 Then follow the other advice for changing your default database
然后按照其他建议更改默认数据库
https://gyazo.com/c3d04c600311c08cb685bb668b569a67 https://gyazo.com/c3d04c600311c08cb685bb668b569a67
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.