何时在查询中使用 dbo 限定表或视图名称?
我在 SQL Server 中有一个视图,比如说 MY_VIEW。
当我执行命令 SELECT * FROM MY_VIEW 时,它工作正常。但是,当我执行它时 SELECT * FROM dbo.MY_VIEW
我得到 *Invalid object name 'MyDB.dbo.MY_VIEW'*
我使用 sa 连接到 SQL Server。
这有什么问题吗?我们什么时候应该使用 dbo.MY_VIEW,什么时候不应该使用?
更新:视图上的架构名称是 dbo,当我创建视图时,我也与 sa 连接。
Update2 我发现问题是区分大小写的排序规则。问题不是因为 dbo。前缀。这是因为数据库排序规则区分大小写,并且查询中的表名大小写错误。
I have a view in SQL Server, lets say MY_VIEW.
When I execute the command SELECT * FROM MY_VIEW
it works fine. However, when I execute it asSELECT * FROM dbo.MY_VIEW
I get *Invalid object name 'MyDB.dbo.MY_VIEW'*
I am connected to SQL server using sa.
What is wrong with this? And when should we use dbo.MY_VIEW and when not?
Update: The schema name on the view is dbo and when I created the view then too I had connected with sa.
Update2 I found the problem was case sensitive collation. The problem was not because of the dbo. prefix. It was because the database collation was case sensitive and table names in queries were in wrong case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否在与 dbo 不同的架构名称下创建了对象?如果您在创建视图时没有限定它,则它将取决于您的用户帐户的默认架构名称。在 SQL 2k5 和 2k8 中,我相信默认行为是为每个用户创建一个新模式,而不是将它们分配给“dbo”模式。
Did you create your objects under a different schema name than dbo? It would depend on the default schema name for your user account if you didn't qualify it when you created the view. In SQL 2k5 and 2k8 I believe the default behavior is to create a new schema for each user vs. assigning them to the 'dbo' schema.
您位于主数据库中。您在 master 数据库中创建了视图。您的实际查询是
SELECT * FROM MyDB.dbo.MY_VIEW
。尝试在 MyDB 数据库中创建视图。You are in the master database. You created the view in the master database. Your actual query was
SELECT * FROM MyDB.dbo.MY_VIEW
. Try creating the view in the MyDB database instead.我发现问题是区分大小写的排序规则。问题不是因为 dbo。前缀。这是因为数据库排序规则区分大小写,并且查询中的表名大小写错误。
I found the problem was case sensitive collation. The problem was not because of the dbo. prefix. It was because the database collation was case sensitive and table names in queries were in wrong case.