sp_defaultdb (Transact-SQL)sp_defaultdb (Transact-SQL)

适用对象:是SQL Server 否Azure SQL 数据库 否Azure Synapse Analytics (SQL DW) 否并行数据仓库 APPLIES TO: yesSQL Server noAzure SQL Database noAzure Synapse Analytics (SQL DW) noParallel Data Warehouse

更改MicrosoftMicrosoft SQL ServerSQL Server登录名的默认数据库。Changes the default database for a MicrosoftMicrosoft SQL ServerSQL Server login.

重要

此功能处于维护模式并且可能会在 Microsoft SQL Server 将来的版本中被删除。This feature is in maintenance mode and may be removed in a future version of Microsoft SQL Server. 请避免在新的开发工作中使用该功能,并着手修改当前还在使用该功能的应用程序。Avoid using this feature in new development work, and plan to modify applications that currently use this feature.请改用ALTER LOGINUse ALTER LOGIN instead.

主题链接图标 Transact-SQL 语法约定Topic link icon Transact-SQL Syntax Conventions

语法Syntax

  
sp_defaultdb [ @loginame = ] 'login', [ @defdb = ] 'database'   

参数Arguments

[ @loginame = ] 'login'登录名。[ @loginame = ] 'login' Is the login name. loginsysname为,无默认值。login is sysname, with no default. 登录名可以是现有SQL ServerSQL Server的登录名,也可以是 Windows 用户或组。login can be an existing SQL ServerSQL Server login or a Windows user or group. 如果 SQL ServerSQL Server 中不存在 Windows 用户或用户组的登录名,则会自动添加该登录名。If a login for the Windows user or group does not exist in SQL ServerSQL Server, it is automatically added.

[ @defdb = ] 'database'新的默认数据库的名称。[ @defdb = ] 'database' Is the name of the new default database. 数据库sysname,无默认值。database is sysname, with no default. 数据库必须已存在。database must already exist.

返回代码值Return Code Values

0(成功)或 1(失败)0 (success) or 1 (failure)

备注Remarks

sp_defaultdb调用 ALTER LOGIN。sp_defaultdb calls ALTER LOGIN. 此语句支持附加选项。This statement supports additional options. 有关更改默认数据库的信息,请参阅ALTER LOGIN (transact-sql)For information about changing default database, see ALTER LOGIN (Transact-SQL).

不能在用户定义的事务中执行sp_defaultdbsp_defaultdb cannot be executed within a user-defined transaction.

权限Permissions

需要 ALTER ANY LOGIN 权限。Requires ALTER ANY LOGIN permission.

示例Examples

以下示例将 AdventureWorks2012AdventureWorks2012 设置为 SQL ServerSQL Server 登录名 Victoria 的默认数据库。The following example sets AdventureWorks2012AdventureWorks2012 as the default database for SQL ServerSQL Server login Victoria.

EXEC sp_defaultdb 'Victoria', 'AdventureWorks2012';  

另请参阅See Also

安全存储过程 (Transact-sql) Security Stored Procedures (Transact-SQL)
ALTER LOGIN (Transact-SQL) ALTER LOGIN (Transact-SQL)
sp_addlogin (Transact-sql) sp_addlogin (Transact-SQL)
sp_droplogin (Transact-sql) sp_droplogin (Transact-SQL)
sp_grantdbaccess (Transact-sql) sp_grantdbaccess (Transact-SQL)
系统存储过程 (Transact-SQL)System Stored Procedures (Transact-SQL)