Swapping ms-sql tables

前端 未结 6 2067
执笔经年
执笔经年 2021-02-05 08:17

I want to swap to tables in the best possible manner.
I have an IpToCountry table, and I create a new one on a weekly basis according to an external CSV file which I import.

6条回答
  •  心在旅途
    2021-02-05 09:06

    Assuming that you're unable to update/insert into the existing table, why don't you wrap all access to the table using a view?

    For example, you might initially store your data in a table called IpToCountry20090303, and your view would be something like this:

    CREATE VIEW IpToCountry
    AS
    SELECT * FROM IpToCountry20090303
    

    When the new data comes in, you can create and populate the IpToCountry20090310 table. Once the table is populated just update your view:

    ALTER VIEW IpToCountry
    AS
    SELECT * FROM IpToCountry20090310
    

    The switch will be completely atomic, without requiring any explicit locking or transactions. Once the view has been updated, you can simply drop the old table (or keep it if you prefer).

提交回复
热议问题