简体   繁体   English

SQL如何复制具有唯一ID的主记录及其关系条目

[英]SQL How to copy a master record with a unique ID and its relation entry

I have two tables, a master table and a relation table. 我有两个表,一个主表和一个关系表。 The Master table as an automatic identity ID field (DOCUMENT_ID) that is generated upon insert. 主表作为插入时生成的自动标识ID字段(DOCUMENT_ID)。 The relation table ties that DOCUMENT_ID from the master table with a USER_NAME entry. 关系表将主表中的DOCUMENT_ID与USER_NAME条目联系起来。 I want to be able to create a copy of the the record in the master table and a copy of its relation in the relationship table while establishing the new relation using the new automatically generated DOCUMENT_ID generated by the Master table. 我希望能够在使用主表生成的新自动生成的DOCUMENT_ID建立新关系的同时,在主表中创建记录的副本,并在关系表中创建其关系的副本。 Here is an example of my tables and the desired output. 这是我的表格和所需输出的示例。

MASTER TABLE: 主表:

DOCUMENT_ID |  DOCUMENT_NAME
1           |  Application
2           |  Invoice
3           |  Receipt  

RELATION TABLE: 关系表:

DOCUMENT_ID|       USER_NAME
1          |       John
1          |       Amy
2          |       Jim
2          |       Jane
3          |       John
3          |       Jane

I would like to copy the records from the master table and create a copy of the relationship so that my output looks like this: 我想从主表中复制记录并创建关系的副本,以便我的输出如下所示:

MASTER TABLE WITH NEW RECORDS INSERTED: Note that the DOCUMENT_ID column generated the IDs automatically. 插入了新记录的主表:请注意,DOCUMENT_ID列会自动生成ID。

DOCUMENT_ID |  DOCUMENT_NAME
1           |  Application
2           |  Invoice
3           |  Receipt  
7           |  Application
8           |  Invoice
9           |  Receipt  

RELATION TABLE: This tables needs to tie the relation between the new IDs created in the master table and a copy of the USER_NAMES that were associated with the original DOCUMENT_IDs. 关系表:此表需要将在主表中创建的新ID与与原始DOCUMENT_ID关联的USER_NAMES副本之间的关系联系起来。

DOCUMENT_ID|       USER_NAME
1          |       John
1          |       Amy
2          |       Jim
2          |       Jane
3          |       John
3          |       Jane
7          |       John
7          |       Amy
8          |       Jim
8          |       Jane
9          |       John
9          |       Jane

I had no time to test this TSQL but it should give you an idea: 我没有时间测试此TSQL,但它应该给您一个想法:

declare @documentID int
declare @newDocumentID int
declare @documentName VARCHAR(256)

declare master_cursor cursor for
select DOCUMENT_ID, DOCUMENT_NAME
from MASTER_TABLE

OPEN master_cursor   
FETCH NEXT FROM master_cursor INTO @documentID, @documentName

WHILE @@FETCH_STATUS = 0   
BEGIN   
    INSERT INTO MASTER_TABLE(DOCUMENT_NAME) VALUES (@documentName)
    -- read the just inserted Document ID
    SELECT @newDocumentID = MAX(DOCUMENT_ID)
    FROM MASTER_TABLE
    WHERE DOCUMENT_NAME = @documentName 

    -- now insert new values in relations table
    INSERT INTO RELATIONS_TABLE
    SELECT @newDocumentID, USER_NAME
    FROM RELATIONS_TABLE
    WHERE
    DOCUMENT_ID = @documentID 

    FETCH NEXT FROM master_cursor INTO @documentID, @documentName
END   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在SQL表中复制记录但是换出新行的唯一ID? - How do you copy a record in a SQL table but swap out the unique id of the new row? 如何从主表中获取最大id,并基于此,在主表中插入一条记录,在子表中插入一个或多个条目 - how to get max id from master table and based on that insert one record in master table and one or more entry in child table 查找 SQL 中每个唯一 ID 的条目计数 - Finding entry count for each unique id in SQL 如何使用CakePhp克隆/复制sql记录,然后获取克隆的ID? - How to clone/copy a sql record with CakePhp, and then grab the ID of the clone? 在SQL中找到唯一的emai id记录? - Find unique emai id record in sql? 更新SQL Server中每个唯一记录的一个条目 - Update one entry of each unique Record in SQL Server 我需要从主人那里获取唯一记录并详细记录其重复记录 - I need to Fetch Unique Record from master and with its repeated record in detail SQL中的级联复制? 可以自动复制记录及其相关记录吗? - Cascade Copy in SQL? Possible to copy a record and its related records automatically? 如何按其顺序获取唯一记录 - How to get unique record by its sequence no SQL:如何显示每个唯一ID的所有记录,但不显示SQL中记录的第一条记录 - SQL: How do I display all records per unique id, but not the first record ever recorded in SQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM