用于复制关系数据的存储过程 (SQL Server 2000)
我有以下表格(仅显示关键列):
Order OrderItem OrderItemDoc Document ======= =========== ============ ========== OrderId OrderItemId OrderItemId DocumentId --etc-- OrderId DocumentId --etc-- --etc--
我正在编写一个存储过程来“克隆”订单(将现有的 OrderId 作为参数,复制订单和所有相关项目,然后返回新的 OrderId) 。 我被困在“OrderItemDoc”连接表上,因为它将连接两组新创建的记录。 我想我需要循环一个临时表,将旧 ID 映射到新 ID。 这是正确的方向吗? 它在 MS-SQL 2000 上运行。
I've got the following tables (only key columns shown):
Order OrderItem OrderItemDoc Document ======= =========== ============ ========== OrderId OrderItemId OrderItemId DocumentId --etc-- OrderId DocumentId --etc-- --etc--
I'm writing a stored procedure to 'clone' an Order (takes an existing OrderId as a parameter, copies the Order and all related items, then returns the new OrderId). I'm stuck on the 'OrderItemDoc' joining table as it will be joining two sets of newly created records. I'm thinking I'll need to loop round a temporary table that maps the old IDs to the new ones. Is that the right direction to go in? It's running on MS-SQL 2000.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 SQL 2005 和 2008 中有许多有效的方法来执行此操作。下面是使用 SQL2000 执行此操作的方法。
您需要声明一个变量来保存克隆的 OrderId,并创建一个临时表来保存将进入 OrderItemDoc 表的克隆记录。
这里有一些关于如何做到这一点的示例代码。 它依赖于将旧 OrderItems 链接到 OrderItemDoc 表中的新 OrderItems 的顺序。
There are many efficient ways of doing this SQL 2005 and 2008. Here's a way to do it using SQL2000.
You need to declare a variable to hold the cloned OrderId and create a temp table to hold the cloned records that will go in the OrderItemDoc table.
Here's some sample code on how to that. It relies on the sequence to link the old OrderItems to the new ones in OrderItemDoc Table.
是的,内存表或临时表将是您的最佳选择。 如果您的 PK 是标识列,那么您还可以根据偏移量假设 ID 是连续的(即,您可以假设您的新 OrderItemId 等于表中现有的 Max(OrderItemId) + 中 Item 的相对偏移量)秩序,但我不喜欢做出这样的假设,这会成为一种更深层次的痛苦)。
Yes, a memory table or a temp table would be your best options. If your PK's are identity columns then you could also make assumptions about ID's being contiguous based on an offset (ie, you could assume that your new OrderItemId is equal to the existing Max(OrderItemId) in the table + the relative offset of the Item in the Order, but I don't like making assumptions like that and it becomes a pain going more than one level deep).
drats,我写了这个然后看到你在2000...(sql server 2005没有这个使用的技巧...)
在sql 2005中不需要循环..
请记录一下同样的方式,创建一个新的@temp表,等等......
drats, I wrote up this then saw you were on 2000... (sql server 2005 doesn't have the trick that this uses...)
no loop necessary in sql 2005..
do document the same way, make a new @temp table, etc...