当前位置: 代码迷 >> Sql Server >> 来看看这个SQL语句如何写:如果数据库有记录,则更新记录;如果无记录,则插入记录
  详细解决方案

来看看这个SQL语句如何写:如果数据库有记录,则更新记录;如果无记录,则插入记录

热度:186   发布时间:2016-04-27 19:28:03.0
来看看这个SQL语句怎么写:如果数据库有记录,则更新记录;如果无记录,则插入记录?
假设关键字是学生学号,如果学生数据库有该学生的记录,则更新该学生的记录,如果无,则插入记录进去。

------解决方案--------------------
SQL code
if exists(    select * from table where 学号=xxx)   update......else   insert......
------解决方案--------------------
update a set col = b.col from a, b where a.id = b.id

insert into a select * from b where id not in (select id from a)
------解决方案--------------------
if exists(select * from table where 学号=xxx )
begin
 update......
end
else
begin
insert......
end
------解决方案--------------------
insert into ....on duplicate key update...
  相关解决方案