读书人

来看看这个SQL语句如何写:如果数据库

发布时间: 2012-01-13 22:43:30 作者: rapoo

来看看这个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...

读书人网 >SQL Server

热点推荐