日期:2014-05-18  浏览次数:20683 次

数据库操作该如何写
数据库有A B两表, 要对A表插入一条记录a,当a存在时候,更新;不存在时,插入a.

对B表插入一条记录b,当b存在时候,不操作;不存在时,插入b.其中B表中一字段A_id与A表id关联。

如何写存储过程,请教各位老师!

------解决方案--------------------
SQL code


if exists (select 1 from A where id = ??)
begin
    update A set ...... where id = ??
end
else
begin
    insert into A 
    select ....
end

if not exists (select 1 from B where id = ??)
begin
    insert into b
    select .... 
end

------解决方案--------------------
探讨
SQL code



if exists (select 1 from A where id = ??)
begin
update A set ...... where id = ??
end
else
begin
insert into A
select ....
end

if not exists (select 1 from B wh……

------解决方案--------------------
如果是SQL 2008 直接用MERGE搞定