使用编号数据库名称进行跨数据库查询

发布于 2024-08-07 09:47:59 字数 550 浏览 2 评论 0原文

对于 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 技术交流群。

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

发布评论

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

评论(3

蝶舞 2024-08-14 09:48:00

尝试将数字放入方括号并使用别名,例如:

SELECT    *
FROM    GrandDatabase.User 
INNER JOIN    [100].dbo.UserInfo u
   ON GrandDatabase.User.UserID = u.UserID

Try putting the numbers into square brackets and using aliases, e.g.:

SELECT    *
FROM    GrandDatabase.User 
INNER JOIN    [100].dbo.UserInfo u
   ON GrandDatabase.User.UserID = u.UserID
一影成城 2024-08-14 09:48:00

尝试用方括号将数据库名称括起来:

SELECT
    *
FROM
    GrandDatabase.User INNER JOIN
    [100].dbo.UserInfo ON GrandDatabase.User.UserID = [100].dbo.UserInfo.UserID

Try enclosing the database name with brackets:

SELECT
    *
FROM
    GrandDatabase.User INNER JOIN
    [100].dbo.UserInfo ON GrandDatabase.User.UserID = [100].dbo.UserInfo.UserID
大姐,你呐 2024-08-14 09:47:59

尝试使用 [100].dbo.UserInfo 而不仅仅是 100。

Try using [100].dbo.UserInfo instead of just the 100.

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