SQLite - UPSERT *not* INSERT or REPLACE

后端 未结 18 2615
猫巷女王i
猫巷女王i 2020-11-21 23:53

http://en.wikipedia.org/wiki/Upsert

Insert Update stored proc on SQL Server

Is there some clever way to do this in SQLite that I have not thought of?

18条回答
  •  失恋的感觉
    2020-11-22 00:34

    The best approach I know is to do an update, followed by an insert. The "overhead of a select" is necessary, but it is not a terrible burden since you are searching on the primary key, which is fast.

    You should be able to modify the below statements with your table & field names to do what you want.

    --first, update any matches
    UPDATE DESTINATION_TABLE DT
    SET
      MY_FIELD1 = (
                  SELECT MY_FIELD1
                  FROM SOURCE_TABLE ST
                  WHERE ST.PRIMARY_KEY = DT.PRIMARY_KEY
                  )
     ,MY_FIELD2 = (
                  SELECT MY_FIELD2
                  FROM SOURCE_TABLE ST
                  WHERE ST.PRIMARY_KEY = DT.PRIMARY_KEY
                  )
    WHERE EXISTS(
                SELECT ST2.PRIMARY_KEY
                FROM
                  SOURCE_TABLE ST2
                 ,DESTINATION_TABLE DT2
                WHERE ST2.PRIMARY_KEY = DT2.PRIMARY_KEY
                );
    
    --second, insert any non-matches
    INSERT INTO DESTINATION_TABLE(
      MY_FIELD1
     ,MY_FIELD2
    )
    SELECT
      ST.MY_FIELD1
     ,NULL AS MY_FIELD2  --insert NULL into this field
    FROM
      SOURCE_TABLE ST
    WHERE NOT EXISTS(
                    SELECT DT2.PRIMARY_KEY
                    FROM DESTINATION_TABLE DT2
                    WHERE DT2.PRIMARY_KEY = ST.PRIMARY_KEY
                    );
    

提交回复
热议问题