Strategy for storing supplemental data alongside imported data

前端 未结 2 625
忘掉有多难
忘掉有多难 2021-01-17 03:01

I have ran into this problem a few times and I am wondering what other people are doing.

When I am creating a database, sometimes I have to import data into a table

相关标签:
2条回答
  • 2021-01-17 03:28

    Here is a SQL Server 2008 merge statement that I came up with to help me with my current situation:

    MERGE INTO dbo.Sections as S        -- Target
    USING dbo.SectionsStaging as SS     -- Source
    ON S.Id = SS.Id                     -- Join
    WHEN MATCHED THEN                   -- Record exists in both tables
        UPDATE SET
            TermCode = SS.TermCode,
            CourseTitle = SS.CourseTitle,
            CoursePrefix = SS.CoursePrefix,
            CourseNumber = SS.CourseNumber,
            SectionNumber = SS.SectionNumber,
            Capacity = SS.Capacity,
            Campus = SS.Campus,
            FacultyFirstName = SS.FacultyFirstName,
            FacultyLastName = SS.FacultyLastName,
            [Status] = SS.[Status],
            Enrollment = SS.Enrollment
    WHEN NOT MATCHED THEN               -- Record exists only in source table
        INSERT ([Id],[TermCode],[CourseTitle],[CoursePrefix],[CourseNumber],[SectionNumber],[Capacity],[Campus],[FacultyFirstName],[FacultyLastName],[Status],[Enrollment])
        VALUES (SS.[Id],SS.[TermCode],SS.[CourseTitle],SS.[CoursePrefix],SS.[CourseNumber],SS.[SectionNumber],SS.[Capacity],SS.[Campus],SS.[FacultyFirstName],SS.[FacultyLastName],SS.[Status],SS.[Enrollment])
    WHEN NOT MATCHED BY SOURCE THEN     -- Record exists only in target table
        DELETE;
    

    Good stuff!

    0 讨论(0)
  • 2021-01-17 03:36

    Well, when you do the import, import into a temp table, and then update the records in the production table (update in the generic sense of the word: delete what's deleted, add what's new, modify what's changed).

    You might also want to check out the new MERGE SQL command in 2008, it just might turn out to be very helpful for this case.

    0 讨论(0)
提交回复
热议问题