Solutions for INSERT OR UPDATE on SQL Server

后端 未结 22 1970
别跟我提以往
别跟我提以往 2020-11-21 22:23

Assume a table structure of MyTable(KEY, datafield1, datafield2...).

Often I want to either update an existing record, or insert a new record if it does

22条回答
  •  感情败类
    2020-11-21 22:53

    I usually do what several of the other posters have said with regard to checking for it existing first and then doing whatever the correct path is. One thing you should remember when doing this is that the execution plan cached by sql could be nonoptimal for one path or the other. I believe the best way to do this is to call two different stored procedures.

    FirstSP:
    If Exists
       Call SecondSP (UpdateProc)
    Else
       Call ThirdSP (InsertProc)
    

    Now, I don't follow my own advice very often, so take it with a grain of salt.

提交回复
热议问题