[英]SQL recursively copy rows from multiple tables following PK FK relationships
I was given the task of creating a stored procedure to copy every piece of data associated with a given ID in our database. 我的任务是创建一个存储过程来复制与数据库中给定ID相关的每一段数据。 This data spans dozens of tables. 这些数据涵盖了数十个表格。 each table may have dozens of matching rows. 每个表可能有几十个匹配的行。
example: 例:
table Account 表帐户
pk = AccountID pk = AccountID
Table AccountSettings 表帐户设置
FK = AccountID FK = AccountID
Table Users 表用户
PK = UserID PK = UserID
FK = AccountID FK = AccountID
Table UserContent 表UserContent
PK = UserContentID PK = UserContentID
FK = UserID FK = UserID
I want to create a copy of everything that is associated with an AccountID(which will traverse nearly every table) The copy will have a new AccountID and UserContentID but will have the same UserID. 我想创建一个与AccountID相关联的所有内容的副本(几乎遍历每个表)。副本将具有新的AccountID和UserContentID,但具有相同的UserID。 the new data needs to be in its respective table. 新数据需要在各自的表中。 :) fun right? :)好玩吧?
The above is just a sample but I will be doing this for something like 50 or 60 tables. 以上只是一个示例,但我将在50或60个表格中执行此操作。 I have researched using CTEs but am still a bit foggy on them. 我已经研究过使用CTE,但我们仍然有点模糊。 that may prove to be the best method. 这可能是最好的方法。 MY SQL skills are...... well I have worked with it for about 40 logged hours so far :) 我的SQL技能......我到目前为止已经使用它约40个小时:)
Any advice or direction on where to look would be greatly appreciated. 任何关于在哪里寻找的建议或方向将不胜感激。 In addition, I am not opposed to doing this via C# if that would be possible or better. 另外,我不反对通过C#这样做,如果可能或更好。
Thanks in advance for any help of info. 提前感谢您提供任何信息帮助。
The simplest way to solve this is the brute force way: write a very long proc that processes each table individually. 解决这个问题的最简单方法是强力方式:编写一个非常长的proc,分别处理每个表。 This will be error-prone and very hard to maintain. 这将容易出错并且很难维护。 But it will have the advantage of not relying on the database or database metadata to be in any particularly consistent state. 但它的优点是不依赖于数据库或数据库元数据处于任何特别一致的状态。
If you want something that works based on metadata, things are more interesting. 如果你想要一些基于元数据的东西,事情会更有趣。 You have three challenges there: 那里有三个挑战:
I've looked at this problem in the past, and while I can't offer you a watertight algorithm, I can give you a general heuristic. 我过去看过这个问题,虽然我不能为你提供防水算法,但我可以给你一个通用的启发式算法。 In other words: this is how I'd approach it. 换句话说:这就是我接近它的方式。
The advantage of the EF approach: as the database changes, so can your model, and if your code is metadata-based, it ought to be able to adapt. EF方法的优点:随着数据库的变化,您的模型也会发生变化,如果您的代码是基于元数据的,那么它应该能够适应。
The disadvantage: if you have such phenomena as fields that are "really" the same but are different types, or complex three-way relationships that aren't modeled properly, or embedded CSV lists that you'd need to parse out, this won't work. 缺点:如果您有“真正”相同但不同类型的字段,或者未正确建模的复杂三向关系,或者您需要解析的嵌入式CSV列表等现象,不行。 It only works if your database is in good shape and is well-modeled. 它只适用于您的数据库状态良好且模型良好的情况。 Otherwise you'll need to resort to brute force. 否则你需要诉诸蛮力。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.