当前位置: 代码迷 >> Sql Server >> 数据库操作该怎么写
  详细解决方案

数据库操作该怎么写

热度:29   发布时间:2016-04-27 12:03:48.0
数据库操作该如何写
数据库有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 = ??endelsebegin    insert into A     select ....endif 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搞定
  相关解决方案