如果记录在表中,则插入或更新 [英] Insert or update if record is in table

查看:29
本文介绍了如果记录在表中,则插入或更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张表 Cars 和 CarDescriptions

I have a tables Cars and CarDescriptions

汽车:IDCar(int, PK, autoincrement)汽车描述(IDDescription,标题(nvarchar),内容(nvarchar),idCar(int,FK)

cars: IDCar(int, PK, autoincrement) carsDesciptions(IDDescription, Header(nvarchar),Content(nvarchar),idCar(int,FK)

在应用程序中,我正在添加汽车并编辑现有汽车.

In application I am adding cars and editing existing ones.

我的问题:

1.如何在数据库中保存更改过的带有描述的汽车??

1.How to save changed Car with descriptions in database ??

我有汽车的 ID,我有描述的 ID

I have ID of Car, and I have ID's of Descriptions

CarDescirption 类没有像 IsChanged 这样的池,所以

Class CarDescirption doesn't have any pool like IsChanged, so

我不想做这样的事情:

  1. 从 idcar=@idcar 的汽车描述中删除
  2. 插入卡片描述(、@Header、@Content、@IDCar)

表中存在则更新,表中不存在则插入

the record must be updated if is in table, and inserted if doesn't exist in table

推荐答案

性能最好:

UPDATE Table1 SET (...) WHERE Column1='SomeValue'
IF @@ROWCOUNT=0
    INSERT INTO Table1 VALUES (...)

这篇关于如果记录在表中,则插入或更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆