How to duplicate rows of self-referenced table

Taher Rahgooy

Suppose we have a self-referenced table like this

CREATE TABLE Month
(
  Id int IDENTITY(1,1)  PRIMARY KEY,
  Title char(128)
)
CREATE TABLE Entity
(
 Id int IDENTITY(1,1)  PRIMARY KEY,
 MonthId int FOREIGN KEY REFERENCES Month(Id),
 Name char(128),
 ParentId int FOREIGN KEY REFERENCES Entity(Id),
)

I want to copy all rows of a certain MonthId to another MonthId. The duplicate parentId's should be updated as well, the entities and their parents should be in the same month.

as an example assume we have

Id        MonthId    Name     ParentId
------------------------------------
1         1          name1     null
2         1          name11    1
3         1          name3     null 
4         1          name31    3
5         1          name311   4

after copying monthId=1 rows to monthId=2 the result should be like this:

Id        MonthId    Name     ParentId
------------------------------------
1         1          name1     null
2         1          name11    1
3         1          name3     null 
4         1          name31    3
5         1          name311   4
newId1    2          name1     null
newId2    2          name11    newId1
newId3    2          name3     null 
newId4    2          name31    newId3
newId5    2          name311   newId4

The newId's are the values that generated by the DBMS.

Note: I use Sql-Server 2012 as DBMS.

Stepan Novikov

This works fine without any assumptions:

DECLARE @baseMonthId int = 1
DECLARE @newMonthId int = 2

DECLARE @newRows TABLE(id int, orig_id int)

MERGE INTO Entity
USING (
  SELECT Id, Name, ParentId FROM Entity WHERE MonthId = @baseMonthId
) AS cf
ON 1 = 0
WHEN NOT MATCHED THEN
  INSERT(MonthId, Name, ParentId) Values(@newMonthId, cf.Name, cf.ParentId)
OUTPUT inserted.Id, cf.Id INTO @newRows(id, orig_id);

UPDATE Entity
SET Parentid = 
  ( 
    SELECT 
      nr.id
    FROM @newRows nr
      WHERE nr.orig_id = Entity.ParentId
   )
WHERE MonthId = @newMonthId;

Result:

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Delete all rows in one table that aren't referenced by another table

From Dev

How to update all rows of a table with an aggregate value referenced by foreign key on an another table in Entity Framework

From Dev

MySQL delete duplicate rows in table

From Dev

How to get all child of each records of a self-referenced table

From Dev

How to return the parents of a child in a self referenced table?

From Dev

PostgreSQL: deleting rows referenced from another table

From Dev

insert duplicate rows in temp table

From Dev

LINQ - how to remove duplicate rows in table

From Dev

Mysql Count of rows not referenced in other table

From Dev

How to use self join to reshape duplicate rows in Pandas?

From Dev

How to duplicate and modify table rows using Jooq insertInto

From Dev

In mysql how do I find rows whose id are not referenced in any other rows in the table?

From Dev

how to alter a referenced table

From Dev

Find duplicate rows in a table

From Dev

How to return the parents of a child in a self referenced table?

From Dev

how to remove duplicate rows

From Dev

insert duplicate rows in temp table

From Dev

Delete duplicate rows in a table

From Dev

How can I duplicate all the rows of a table into the same table?

From Dev

Jquery: How to remove duplicate HTML TABLE rows based on columns values

From Dev

Mysql Count of rows not referenced in other table

From Dev

Query to find duplicate rows in a table

From Dev

Combine rows from self-referenced table

From Dev

How to delete from table then delete what deleted rows referenced? (postgresql)

From Dev

Prohibit Duplicate Rows Inserted into Table

From Dev

Self referenced MySql table with conditions on parent and child

From Dev

SQL Group by main category in self-referenced table

From Dev

Finding duplicate rows in a MySQL table

From Dev

merge partial duplicate rows in a table

Related Related

  1. 1

    Delete all rows in one table that aren't referenced by another table

  2. 2

    How to update all rows of a table with an aggregate value referenced by foreign key on an another table in Entity Framework

  3. 3

    MySQL delete duplicate rows in table

  4. 4

    How to get all child of each records of a self-referenced table

  5. 5

    How to return the parents of a child in a self referenced table?

  6. 6

    PostgreSQL: deleting rows referenced from another table

  7. 7

    insert duplicate rows in temp table

  8. 8

    LINQ - how to remove duplicate rows in table

  9. 9

    Mysql Count of rows not referenced in other table

  10. 10

    How to use self join to reshape duplicate rows in Pandas?

  11. 11

    How to duplicate and modify table rows using Jooq insertInto

  12. 12

    In mysql how do I find rows whose id are not referenced in any other rows in the table?

  13. 13

    how to alter a referenced table

  14. 14

    Find duplicate rows in a table

  15. 15

    How to return the parents of a child in a self referenced table?

  16. 16

    how to remove duplicate rows

  17. 17

    insert duplicate rows in temp table

  18. 18

    Delete duplicate rows in a table

  19. 19

    How can I duplicate all the rows of a table into the same table?

  20. 20

    Jquery: How to remove duplicate HTML TABLE rows based on columns values

  21. 21

    Mysql Count of rows not referenced in other table

  22. 22

    Query to find duplicate rows in a table

  23. 23

    Combine rows from self-referenced table

  24. 24

    How to delete from table then delete what deleted rows referenced? (postgresql)

  25. 25

    Prohibit Duplicate Rows Inserted into Table

  26. 26

    Self referenced MySql table with conditions on parent and child

  27. 27

    SQL Group by main category in self-referenced table

  28. 28

    Finding duplicate rows in a MySQL table

  29. 29

    merge partial duplicate rows in a table

HotTag

Archive