SQL - Copy Data Within Same Table

前端 未结 4 548
谎友^
谎友^ 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:28

    Psuedo code, not tested:

    DECLARE @patient_id INT, @date datetime, @time ??
    SET @patient_id = 112244 --your patient id
    
    INSERT INTO [**Table 1**] (patient_id, date, time, etc, etc, etc, etc)
    VALUES (@patient_id, @date, @time, 'a', 'b', 'c', 'd')
    
    DECLARE @encounter_id int
    SET @encounter_id = SCOPE_IDENTITY  -- or select @encounter_id = encounter_id from [**Table 1**] where patientId = @patient_id
    
    INSERT INTO [**Table 2**] (encounter_id, page, recorded_on, recorded_by, etc, etc2)
    SELECT @encounter_id, page, recorded_on, recorded_by, etc, etc2 
    FROM [**Table 2**]
    WHERE encounter_id = 1234
    
    INSERT INTO [**Table 3**] (encounter_id, page, keyname, keyvalue)
    SELECT @encounter_id, page, keyname, keyvalue
    FROM [**Table 3**]
    WHERE encounter_id = 1234
    

提交回复
热议问题