2

我有三个相关的表 -用户,组有项目

Id   UserName
1    Joe
2    Fred

Id   GroupName    UserId
1    TestGr1      1
2    TestGr2      1

Id   ItemName     GroupId   UserId
1    Item1        1         1
2    Item2        1         1
3    Item3        2         1
4    Item4        2         1

我想给 Fred Joe 所拥有的一切,即复制 Groups 和 Items 中的所有相关数据。

我已经复制了组:

insert into Groups
select [GroupName], 2
from Groups
where UserId = 1

我该如何处理物品?

更新
这就是我最终想要的:

Id   UserName
1    Joe
2    Fred

Id   GroupName    UserId
1    TestGr1      1
2    TestGr2      1
3    TestGr1      2
4    TestGr2      2

Id   ItemName     GroupId   UserId
1    Item1        1         1
2    Item2        1         1
3    Item3        2         1
4    Item4        2         1
5    Item1        3         2
6    Item2        3         2
7    Item3        4         2
8    Item4        4         2
4

3 回答 3

2

我想这可能对你有用....

--create variables to store the new users
declare @user_from int,
    @user_to int

set @user_from = 1
set @user_to = 2

--insert the new groups
insert into Groups (GroupName, UserId)
select GroupName, @user_to
from Groups
where UserId = @user_from

--insert the item values matching on the group names but using the new groups
insert into Items (ItemName,GroupId,UserId)
select i.ItemName, g2.Id, @user_to
from Items i
inner join Groups g on g.Id = i.GroupId
inner join Groups g2 on g2.GroupName = g.GroupName
                 and g2.UserId = @user_to
where i.UserId = @user_from
于 2013-02-11T17:44:30.503 回答
1

这是另一个版本

--insert items
insert into Groups      
select [GroupName], 2
from Groups
where UserId = 1

--insert groups
DECLARE @tblGroups Table(
NewGroupId int not null, 
OldGroupId int not null)
insert into @tblGroups
select ng.id, og.id
from Groups ng, groups og
where ng.groupname = og.groupname 
AND UserId = 1

insert into items
select i.itemname, t.newgroupid, 2
from items i, @tblgroups t
where t.oldgroupid = i.groupid 
and  i.userid = 1
于 2013-02-11T17:56:16.250 回答
1

我一直打算一起练习合并语句和输出语句。用您的表名替换所有临时表信息。我将@OldUserID 和@NewUserID 抽象出来,以便可以轻松地将其编写为存储过程。

干杯!

声明:

DECLARE @TempUsers TABLE (ID INT PRIMARY KEY NOT NULL IDENTITY(1,1),UserName VARCHAR(25));
DECLARE @TempGroups TABLE (ID INT PRIMARY KEY NOT NULL IDENTITY(1,1), GroupName VARCHAR(25),UserID INT);
DECLARE @TempItems TABLE (ID INT PRIMARY KEY NOT NULL IDENTITY(1,1),ItemName VARCHAR(25),GroupID INT, UserID INT);

INSERT INTO @TempUsers SELECT 'Joe' UNION SELECT 'Fred';
INSERT INTO @TempGroups SELECT 'TestGr1',1 UNION SELECT 'TestGr2',1;
INSERT INTO @TempItems SELECT 'Item1',1,1 UNION SELECT 'Item2',1,1;
INSERT INTO @TempItems SELECT 'Item3',2,1 UNION SELECT 'Item4',2,1;

SELECT 'OldUsers' AS TableName,* FROM @TempUsers
SELECT 'OldGroups' AS TableName,* FROM @TempGroups
SELECT 'OldItems' AS TableName,* FROM @TempItems

DECLARE @TempCorrelationTable TABLE (OldID INT, [NewID] INT);
DECLARE @OldUserID INT = 1;
DECLARE @NewUserID INT = 2;

解决方案:

MERGE INTO @TempGroups AS T
USING (SELECT ID,GroupName FROM @TempGroups) AS S
ON 1=2
WHEN NOT MATCHED BY TARGET THEN INSERT VALUES (GroupName,@NewUserID)
OUTPUT S.ID, INSERTED.ID INTO @TempCorrelationTable;

MERGE INTO @TempItems AS T
USING (SELECT ItemName,[NewID] FROM @TempItems AS TI INNER JOIN @TempCorrelationTable AS TC ON (TI.GroupID = TC.OldID)) AS S
ON 1=2
WHEN NOT MATCHED BY TARGET THEN INSERT VALUES (ItemName,[NewID],@NewUserID);

结果:

SELECT 'NewUsers' AS TableName,* FROM @TempUsers
SELECT 'NewGroups' AS TableName,* FROM @TempGroups
SELECT 'NewItems' AS TableName,* FROM @TempItems
于 2013-02-11T19:09:31.300 回答