SQL Server 架构和默认架构

发布于 2024-09-25 02:46:40 字数 365 浏览 0 评论 0原文

我的数据库中有一个模式定义。除了现在每次执行 sql 语句时我都必须提供模式...... SELECT * FROM [myschema].table

我使用 Management Studio 为我的用户设置了默认架构,并运行了 使用 DEFAULT_SCHEMA [myschema] 更改用户 myUser 并且在编写没有架构的查询时(SELECT * FROM table)我仍然得到无效的对象“table”

有没有一种方法可以编写 SELECT * FROM table 而不必一直指定架构名称?

它在 SQL 2005 上使用 SQL Management Studio。

I have a schema define in my database. Except now everytime I do a sql statement I have to provide the schema ...
SELECT * FROM [myschema].table

I set the default schema for my user using management studio and also ran the
ALTER USER myUser WITH DEFAULT_SCHEMA [myschema]
and I still get the invalid object 'table' when writing a query without the schema (SELECT * FROM table)

Is there a way to write SELECT * FROM table without having to specify the schema name all the time?

It's on SQL 2005 using SQL Management Studio.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不如归去 2024-10-02 02:46:40

用户是否是 SA,如果是,则根据 文档 SA 用户始终默认使用 dbo 架构。

DEFAULT_SCHEMA 的值被忽略
如果用户是该组织的成员
sysadmin 固定服务器角色。全部
sysadmin 固定服务器的成员
角色的默认架构为 dbo。

Is the user an SA, if so it will not work, according to the documentation SA users are always defaulted to the dbo schema.

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.

柠檬色的秋千 2024-10-02 02:46:40

有几个选项:

  1. 您的用户是否列在“安全”>“安全”下?用户(SSMS 中)?检查属性(右键单击名称),并查看默认架构是否在数据库上下文中设置,而不是在实例中设置(这就是 ALTER USER 的含义)环境)。
  2. 为要引用的表创建同义词:< /p>

    创建同义词表名  
       FOR [your_db].[your_schema].table_name
    

    ...这将影响在该数据库上下文中不使用至少两个名称表示法的每个人。 在此处了解更多信息。但它最终与架构相关联。

  3. 检查在“可用数据库”下拉列表(左上角,执行按钮左侧)中选择的数据库是否正确。

  4. 指定表(和视图)引用时使用三个名称表示法:

    <前><代码>选择*
    FROM [your_db].[your_schema].table_name

Couple of options:

  1. Is your user listed under Security > Users (in SSMS)? Check the Properties (right click the name), and see if the Default schema is set in the context of the database, rather than the instance (which is what ALTER USER is setting).
  2. Create a synonym for the table you want to reference:

    CREATE SYNONYM table_name  
       FOR [your_db].[your_schema].table_name
    

    ...which will affect everyone who doesn't use at least two name notation, in the context of that database. Read more about it here. But it is associated ultimately to a schema.

  3. Check that the database selected in the "Available Databases" drop down (upper left, to the left of the Execute button) is correct.

  4. Use three name notation when specifying table (and view) references:

    SELECT * 
      FROM [your_db].[your_schema].table_name
    
冬天的雪花 2024-10-02 02:46:40

如果您不想使用“完全限定”SQl 名称,则需要避免使用未使用分配的“dbo”默认架构的任何帐户或角色创建表。如果您不打算使用用户的默认架构,为什么需要更改它?

If you do not want to use "full qualified" SQl names, then you need to avoid creating your tables using any account or role that's not using the "dbo" default schema assigned. Why do you need to change the default schema on the user if you don't plan on using it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文