使用编号数据库名称进行跨数据库查询
对于 SQL Server 2005,我还是个新手。我有一个包含大部分存储过程和表的数据库(我们将其称为“GrandDatabase”)。每个用户都有自己独立的数据库,以用户的编号 ID 命名。所以我有一个数据库列表如下,例如:
GrandDatabase
100
101
102
...
我需要连接 GrandDatabase 和用户数据库中的表。我在其他地方读到,从 GrandDatabase 执行时,以下内容应该有效:
SELECT
*
FROM
GrandDatabase.User INNER JOIN
100.dbo.UserInfo ON GrandDatabase.User.UserID = 100.dbo.UserInfo.UserID
这给了我一个语法错误,抱怨“.”就在第一次引用 100 数据库之后。我做了一些调整,发现当我使用非编号数据库时,这段代码可以正常工作(例如,将上面的“100”替换为“User100”)。有谁知道如何使用编号数据库名称进行此操作?
谢谢!
克里斯
I'm a bit of a novice when it comes to SQL Server 2005. I have a database containing most of the stored procedures and tables (we'll call it 'GrandDatabase'). Each user has its own separate database named after the user's numbered ID. So I have a database list as follows, for example:
GrandDatabase
100
101
102
...
I need to join tables across the GrandDatabase and a user's database. I've read elsewhere that the following should work, when executed from GrandDatabase:
SELECT
*
FROM
GrandDatabase.User INNER JOIN
100.dbo.UserInfo ON GrandDatabase.User.UserID = 100.dbo.UserInfo.UserID
This gives me a syntax error, complaining about the '.' right after the first reference to the 100 database. I did a little tweaking and discovered that this code works fine when I use non-numbered databases (for instance, replacing the '100' above with 'User100'). Does anybody know how to make this work with numbered database names?
Thanks!
Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试将数字放入方括号并使用别名,例如:
Try putting the numbers into square brackets and using aliases, e.g.:
尝试用方括号将数据库名称括起来:
Try enclosing the database name with brackets:
尝试使用 [100].dbo.UserInfo 而不仅仅是 100。
Try using [100].dbo.UserInfo instead of just the 100.