SQL access query- Update row if exists, insert if does not

前端 未结 2 1376
忘了有多久
忘了有多久 2020-12-31 22:10

I need to write an SQL query for MS Access 2000 so that a row is updated if it exists, but inserted if it does not.

i.e.

If row exists...

UPD         


        
2条回答
  •  一个人的身影
    2020-12-31 22:40

    This doesn't apply directly to Access [EDIT: David-W-Fenton asserts that this is not possible in access], but for completeness (in case someone reading this is interested in something beyond Access):

    I have had success in Microsoft SQL Server using an approach that should be more efficient as it only has to do one index check, rather than two. Here's an example from my current project:

    UPDATE ActivityRelationships
    SET [Count] = ([Count] + 1)
    WHERE ActivityBeforeId=@activityBeforeId AND ActivityAfterId=@activityAfterId
    IF @@ROWCOUNT=0
        INSERT INTO ActivityRelationships ([ActivityBeforeId], [ActivityAfterId], [Count])
        VALUES (@activityBeforeId, @activityAfterId, 1)
    

提交回复
热议问题