Performing Insert OR Update (upsert) on sql server compact edition

后端 未结 7 1717
陌清茗
陌清茗 2020-12-17 06:36

I have c# project that is using sqlserver compact edition and entity framework for data access. I have the need to insert or update a large amount of rows, 5000+ or more to

7条回答
  •  死守一世寂寞
    2020-12-17 07:27

    Maybe you could obtain the result you seek by using simple queries. Let's say the the table you want to insert into or update is like this

    TABLE original
         id integer,
         value char(100)
    

    first you could create a temporary table with the new values (you can use a SELECT INTO or other ways to create it)

    TABLE temp
        id integer,
        value char(100)
    

    now, you need to do two things, update the rows in original and then insert the new values

    UPDATE original 
    SET original.value = temp.value
    FROM original, temp
    WHERE original.id = temp.id
    
    INSERT INTO original 
    SELECT * from temp 
    WHERE temp.id not IN (select o.id from original o)
    

提交回复
热议问题