Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
148 views
in Technique[技术] by (71.8m points)

sql - Duplicating a row with all the rows below it in the hierarchy tables

I do not know if my request has a name but I think it can be called "duplicating a row with all the rows below it in the hierarchy tables".

I want to create a stored procedure that can duplicate a record and all its childs and grandchilds records.

When I say "Duplicate" I main that all the record values in the columns will be copy to a new record on the same table (the copy should be without Primary key Id column because its automated) This Stored Procedure needs to get two parameters:

  1. Parameter 1 - Table Name
  2. Parameter 2 - Value of the Primary key Id (of that Table Name) that needs to duplicate.

Instructions of the code:

  1. The value (Parameter 2) will find one record on the table (Parameter 1) and duplicate it (as I said same values for all the columns excluding the primary key of that table.
  2. Then we will need to know all the table names that has a relationship with this primary key (of the parent table)
  3. Then every child table will duplicate the records that have the same Id value (Parameter 2) with the new Id value (of the new parent record).

several things: In the dream scenario the code knows to go down levels without any limit (children, grandchildren, great-grandchildren ...) ,but I guess it is a very complex code that will contain some recursion code, so even a code containing up to 3-4 levels I will be happily accepted.

question from:https://stackoverflow.com/questions/65937288/duplicating-a-row-with-all-the-rows-below-it-in-the-hierarchy-tables

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...