SQL Server:根据两个表将值插入到一个表中数据

发布于 2024-12-10 00:21:08 字数 867 浏览 0 评论 0原文

我在两个不同的数据库中有两个同名的表。 DatabaseA 中的 TableA 包含列 ServerName,而 DatabaseB 中的另一个 TableA 包含ServerId 列。我在 DatabaseB 中有第三个表 TableB,其中包含 ServerIdServerName,我想映射 DatabaseA 中的 >ServerName 及其 DatabaseB 中的 ServerIdDatabaseB表A

例如...

DatabaseA中:

TableA

ServerName
  MyServer
  HisServer
  HerServer

DatabaseB中:

TableB

ServerId | ServerName
1          MyServer
2          HisServer
3          HerServer

我想映射ServerNamesServerIds 并将 Id 插入DatabaseB 的 TableA

提前致谢。

I have two tables with the same name in two different databases. TableA in DatabaseA contains a column ServerName, while another TableA in DatabaseB contains the ServerId column. I have a third table TableB in DatabaseB that contains ServerId and ServerName, and I want to map the ServerName in DatabaseA, and its ServerId in DatabaseB, to DatabaseB's TableA.

For example ...

In DatabaseA:

TableA:

ServerName
  MyServer
  HisServer
  HerServer

In DatabaseB:

TableB:

ServerId | ServerName
1          MyServer
2          HisServer
3          HerServer

I want to map the ServerNames to the ServerIds and insert the Ids into DatabaseB's TableA.

Thanks in advance.

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

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

发布评论

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

评论(1

樱娆 2024-12-17 00:21:08

这是一个完整的脚本。

USE master;
IF EXISTS (SELECT * FROM sys.databases WHERE name = N'DatabaseA')
    DROP DATABASE DatabaseA;
IF EXISTS (SELECT * FROM sys.databases WHERE name = N'DatabaseB')
    DROP DATABASE DatabaseB;
CREATE DATABASE DatabaseA;
CREATE DATABASE DatabaseB;
GO
USE DatabaseA;
GO
CREATE TABLE dbo.TableA (
    ServerName  sysname     NOT NULL
);
INSERT INTO dbo.TableA VALUES (N'MyServer');
INSERT INTO dbo.TableA VALUES (N'HisServer');
INSERT INTO dbo.TableA VALUES (N'HerServer');
SELECT * FROM dbo.TableA;
GO
USE DatabaseB;
GO
CREATE TABLE dbo.TableA (
    ServerID    smallint    NOT NULL
);

CREATE TABLE dbo.TableB (
    ServerID    smallint    NOT NULL,
    ServerName  sysname     NOT NULL
);
INSERT INTO dbo.TableB VALUES (1, N'MyServer');
INSERT INTO dbo.TableB VALUES (2, N'HisServer');
INSERT INTO dbo.TableB VALUES (3, N'HerServer');
SELECT * FROM dbo.TableB;

-- "I want to map the ServerNames to the ServerIds and insert the Ids into DatabaseB's TableA."

INSERT INTO dbo.TableA (
    ServerID
)
SELECT bb.ServerID
FROM DatabaseA.dbo.TableA   AS aa
JOIN DatabaseB.dbo.TableB   AS bb
  ON aa.ServerName = bb.ServerName;

SELECT * FROM dbo.TableA;

和输出:

ServerName
-------------
MyServer
HisServer
HerServer

ServerID ServerName
-------- -------------------
       1 MyServer
       2 HisServer
       3 HerServer

ServerID
--------
       1
       2
       3

Here is a complete script.

USE master;
IF EXISTS (SELECT * FROM sys.databases WHERE name = N'DatabaseA')
    DROP DATABASE DatabaseA;
IF EXISTS (SELECT * FROM sys.databases WHERE name = N'DatabaseB')
    DROP DATABASE DatabaseB;
CREATE DATABASE DatabaseA;
CREATE DATABASE DatabaseB;
GO
USE DatabaseA;
GO
CREATE TABLE dbo.TableA (
    ServerName  sysname     NOT NULL
);
INSERT INTO dbo.TableA VALUES (N'MyServer');
INSERT INTO dbo.TableA VALUES (N'HisServer');
INSERT INTO dbo.TableA VALUES (N'HerServer');
SELECT * FROM dbo.TableA;
GO
USE DatabaseB;
GO
CREATE TABLE dbo.TableA (
    ServerID    smallint    NOT NULL
);

CREATE TABLE dbo.TableB (
    ServerID    smallint    NOT NULL,
    ServerName  sysname     NOT NULL
);
INSERT INTO dbo.TableB VALUES (1, N'MyServer');
INSERT INTO dbo.TableB VALUES (2, N'HisServer');
INSERT INTO dbo.TableB VALUES (3, N'HerServer');
SELECT * FROM dbo.TableB;

-- "I want to map the ServerNames to the ServerIds and insert the Ids into DatabaseB's TableA."

INSERT INTO dbo.TableA (
    ServerID
)
SELECT bb.ServerID
FROM DatabaseA.dbo.TableA   AS aa
JOIN DatabaseB.dbo.TableB   AS bb
  ON aa.ServerName = bb.ServerName;

SELECT * FROM dbo.TableA;

And the output:

ServerName
-------------
MyServer
HisServer
HerServer

ServerID ServerName
-------- -------------------
       1 MyServer
       2 HisServer
       3 HerServer

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