用sql语句更新一条记录,如果不存在就插入,怎么写?

我要myswl的语法,怎么写
2024-12-26 00:27:34
推荐回答(2个)
回答1:

PLSQL 的写法:

begin
update tablename
set xxx=values;
exception
when no_data_found then
insert into tablename
valuse();
end ;

回答2:

create proc InsertData(@id int)
as
begin
declare @count int
set @count =(select count(*) from 表名 where id = @id)
if @count>0
begin
update 表名 set 字段名='修改值' where id = @id
end
else
begin
insert into 表名(id,字段1,字段2) values('id1','值1','值2')
end
end
go

exec InsertData 1
exec InsertData 2
exec InsertData 3