1

I have a unique requirement - I have a data list which is in excel format and I import this data into SQL 2008 R2., once every year, using SQL's import functionality. In the table "Patient_Info", i have a primary key set on the column "MemberID" and when i import the data without any duplicates, all is well.

But some times, when i get this data, some of the patient's info gets repeated with updated address / telephone , etc., with the same MemberID and since I set this as primary key, this record gets left out without importing into the database and thus, i dont have an updated record for that patient.

EDIT

I am not sure how to achieve this, to update some of the rows which might have existing memberIDs and any pointer to this is greatly appreciated.

examples below:

List 1:

List 1

List 2: enter image description here

2
  • can you show how your data looks like at each stage and explain with some examples and show final output Commented Jun 20, 2017 at 3:37
  • @TheGameiswar - I have added a snapshot of my scenario, with highlighted row as my duplicate for this year. I have quite a few in the list like this as duplicates Commented Jun 21, 2017 at 13:12

1 Answer 1

2

This is not a terribly unique requirement.

One acceptable pattern you can use to resolve this problem would be to import your data into "staging" table. The staging table would have the same structure as the target table to which you're importing, but it would be a heap - it would not have a primary key.

Once the data is imported, you would then use queries to consolidate newer data records with older data records by MemberID.

Once you've consolidated all same MemberID records, there will be no duplicate MemberID values, and you can then insert all the staging table records into the target table.

EDIT

As @Panagiotis Kanavos suggests, you can use a SQL MERGE statement to both insert new records and update existing records from your staging table to the target table.

Assume that the Staging table is named Patient_Info_Stage, the target table is named Patient_Info, and that these tables have similar schemas. Also assume that field MemberId is the primary key of table Patient_Info.

The following MERGE statement will merge the staging table data into the target table:

BEGIN TRAN;
MERGE Patient_Info WITH (SERIALIZABLE) AS Target
  USING Patient_Info_Stage AS Source
    ON Target.MemberId = Source.MemberId
WHEN MATCHED THEN UPDATE 
    SET Target.FirstName = Source.FirstName
       ,Target.LastName = Source.LastName
       ,Target.Address = Source.Address
       ,Target.PhoneNumber = Source.PhoneNumber
WHEN NOT MATCHED THEN INSERT (
    MemberID
   ,FirstName
   ,LastName
   ,Address
   ,PhoneNumber
  ) Values (
    Source.MemberId
   ,Source.FirstName
   ,Source.LastName
   ,Source.Address
   ,Source.PhoneNumber
  );
COMMIT TRAN;

*NOTE: The T-SQL MERGE operation is not atomic, and it is possible to get into a race condition with it. To insure it will work properly, do these things:

  • Ensure that your SQL Server is up-to-date with service packs and patches (current rev of SQL Server 2008 R2 is SP3, version 10.50.6000.34).
  • Wrap your MERGE in a transaction (BEGIN TRAN;, COMMIT TRAN;)
  • Use SERIALIZABLE hint to help prevent a potential race condition with the T-SQL MERGE statement.
Sign up to request clarification or add additional context in comments.

7 Comments

this seems a viable approach for this problem, but if i were to consolidate data from both staging and original table, wouldn't i still have the same duplicate memberID in both the tables ? How do i solve that problem ?
@Ron that's the point - you join the two tables and insert the new records, update the existing ones. You can do it with two separate statements, UPDATE FROM and INSERT FROM. Or you can use the MERGE statement to do the same, as long as you guard it with a transaction - it's not atomic
@PanagiotisKanavos thanks for the clarification. Much appreciated.
@PanagiotisKanavos, I've added clarifications to my answer based on your suggestions.
@STLDeveloper, just tried your query with my existing database, after creating a staging table, as you had suggested, and it works like a charm. Again, thanks a ton !!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.