如何在 SQL Server 2005 中设置数据库的默认架构?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8208019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:21:02  来源:igfitidea点击:

How to set the default schema of a database in SQL Server 2005?

sqlsql-serversql-server-2005

提问by user960567

The following line works:

以下行有效:

SELECT * FROM [myschema].users

But this does not:

但这不会:

SELECT * FROM users

回答by Adam Wenger

A default schema is user-specific:

默认模式是特定于用户的:

USE yourDatabase;
ALTER USER [yourUser] WITH DEFAULT_SCHEMA = myschema;

More information about the ALTER TABLEfor SQL 2005 might help you as well.

有关SQL 2005的ALTER TABLE 的更多信息也可能对您有所帮助。

As this is user-specific, if you have multiple users, you will need to execute this query (on each database) for each user whose default schema you want to update.

由于这是特定于用户的,如果您有多个用户,则需要为要更新其默认架构的每个用户执行此查询(在每个数据库上)。

It is important to note:

重要的是要注意:

The value of DEFAULT_SCHEMA is ignored if the user is a member of the sysadmin
fixed server role. All members of the sysadmin fixed server role have a default
schema of dbo.

回答by Ivan

You can use:

您可以使用:

ALTER USER Mary51 WITH DEFAULT_SCHEMA = Purchasing;

To set the default schema for user.

为用户设置默认架构。

回答by t_warsop

Like user960567, I tried the answers from both Adam and Ivan and could not get it working.

像 user960567 一样,我尝试了 Adam 和 Ivan 的答案,但无法使其正常工作。

I was running:

我之前在跑步:

ALTER USER myuser WITH DEFAULT_SCHEMA=newschema

as suggested, but after executing this and executing

按照建议,但在执行此操作并执行后

SELECT SCHEMA_NAME()

it was still returning 'dbo' as the default schema.

它仍然返回“dbo”作为默认模式。

To fix this I executed:

为了解决这个问题,我执行了:

ALTER USER myuser WITH DEFAULT_SCHEMA=newschema EXECUTE AS USER='myuser'

and it worked as expected - now executing:

它按预期工作 - 现在执行:

SELECT SCHEMA_NAME()

returns 'newschema'.

返回“新闻模式”。