0
DECLARE @origSiteID int = 836,
        @newSiteID int = 838

INSERT INTO Rooms (Name, SiteID, ControlsSiteNum, ControlsRoomNum, IsActive)
SELECT 
        Name, SiteID, ControlsSiteNum, ControlsRoomNum, IsActive
FROM Rooms
WHERE SiteID = @origSiteID

上面的代码将执行以下操作:

ID  Name    SiteID  ControlsSiteNum ControlsRoomNum IsActive
2025    E Terminal  838 63900   63901   1
2026    ARPT Kitchen    838 63900   63902   1
2027    ARPT Mgr    838 63900   63903   1
2028    W Terminal  838 63900   63904   1

将所有房间从表中origSiteID插入Rooms。现在,我希望将插入的 4 个房间映射到@newSiteID!! 更新声明会完成这项工作吗?我如何将房间从 any 映射origsiteID到 any newSiteID

这个问题得到了回答,但我想更进一步:我想更进一步。我想为创建的每个房间生成一个包含原始房间 ID 及其新房间 ID 的地图。谢谢你。

4

1 回答 1

0

你应该@newSiteIDSELECT

DECLARE @origSiteID int = 836,
    @newSiteID int = 838

INSERT INTO Rooms (Name, SiteID, ControlsSiteNum, ControlsRoomNum, IsActive)
SELECT 
        Name, @newSiteID, ControlsSiteNum, ControlsRoomNum, IsActive
FROM Rooms
WHERE SiteID = @origSiteID

在评论后附加
更改表的结构:

ALTER TABLE Rooms ADD OriginalSiteID INT

然后 INSERT 将如下所示:

DECLARE @origSiteID int = 836,
    @newSiteID int = 838

INSERT INTO Rooms (Name, SiteID, ControlsSiteNum, ControlsRoomNum, IsActive, OriginalSiteID)
SELECT 
        Name, @newSiteID, ControlsSiteNum, ControlsRoomNum, IsActive, @origSiteID
FROM Rooms
WHERE SiteID = @origSiteID
于 2013-08-01T16:31:45.453 回答