SQL - Copy Data Within Same Table

前端 未结 4 470
谎友^
谎友^ 2021-01-16 19:58

I\'m not that great with SQL Server, but I\'m trying to do some behind the scenes work to create some functionality that our EMR system lacks - copying forms (and all their

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-16 20:36

    I always like creating sample tables in [tempdb] so that the syntax is correct. I created tables [t1], [t2], and [t3]. There are primary and foreign keys.

    If you have a well developed schema, ERD (entity relationship diagram) http://en.wikipedia.org/wiki/Entity-relationship_diagram , these relationships should be in place.

    -- Playing around
    use tempdb
    go
    
    
    --
    -- Table 1
    --
    
    -- Remove if it exists
    if object_id('t1') > 0
    drop table t1
    go
    
    -- Create the first table
    create table t1
    (
    encounter_id int,
    patient_id int, 
    the_date date,
    the_time time,
    constraint pk_t1 primary key (encounter_id)
    );
    go
    
    -- Add one row
    insert into t1 values (1234, 112233, '2014-01-02', '14:25:01:00');
    go
    
    -- Show the data
    select * from t1
    go
    
    
    --
    -- Table 2
    --
    
    -- Remove if it exists
    if object_id('t2') > 0
    drop table t2
    go
    
    -- Create the second table
    create table t2
    (
    encounter_id int,
    the_page int,
    recorded_on date,
    recorded_by int,
    constraint pk_t2 primary key (encounter_id, the_page)
    );
    go
    
    -- Add two rows
    insert into t2 values 
    (1234, 1, '2014-01-02', 134),
    (1234, 2, '2014-01-02', 134);    
    go  
    
    -- Show the data
    select * from t2
    go
    
    --
    -- Table 3
    --
    
    -- Remove if it exists
    if object_id('t3') > 0
    drop table t3
    go
    
    -- Create the third table
    create table t3
    (
    encounter_id int,
    the_page int,
    key_name1 varchar(16),
    key_value1 varchar(16),
    constraint pk_t3 primary key (encounter_id, the_page, key_name1)
    );
    go
    
    -- Add seven rows
    insert into t3 values 
    (1234, 1, 'key1', 'aaa'),
    (1234, 1, 'key2', 'bbb'),
    (1234, 1, 'key3', 'ccc'),
    (1234, 1, 'key4', 'ffffd'),
    (1234, 2, 'key5', 'eee'),
    (1234, 2, 'key6', 'fff'),
    (1234, 2, 'key7', 'ggg');
    go
    
    -- Show the data
    select * from t3
    go
    
    --
    -- Foreign Keys
    --
    
    alter table t2 with check
    add constraint fk_t2 foreign key (encounter_id)
    references t1 (encounter_id);
    
    alter table t3 with check
    add constraint fk_t3 foreign key (encounter_id, the_page)
    references t2 (encounter_id, the_page);
    

    Here comes the fun part, a stored procedure to duplicate the data.

    --
    -- Procedure to duplicate one record
    --
    
    -- Remove if it exists
    if object_id('usp_Duplicate_Data') > 0
    drop procedure t1
    go
    
    -- Create the procedure
    create procedure usp_Duplicate_Data @OldId int, @NewId int
    as
    begin
    
      -- Duplicate table 1's data
      insert into t1 
      select 
        @NewId,
        patient_id, 
        the_date,
        the_time
      from t1
      where encounter_id = @OldId;
    
      -- Duplicate table 2's data
      insert into t2 
      select 
        @NewId,
        the_page, 
        recorded_on,
        recorded_by
      from t2
      where encounter_id = @OldId;
    
      -- Duplicate table 3's data
      insert into t3 
      select 
        @NewId,
        the_page, 
        key_name1,
        key_value1
      from t3
      where encounter_id = @OldId;
    
    end
    

    Last but not least, we have to call the stored procedure to make sure it works.

    -- Sample call
    exec usp_Duplicate_Data 1234, 7777
    

    In summary, I did not add any error checking or accounted for a range of Id's. I leave these tasks for you to learn.

    enter image description here

提交回复
热议问题