首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

数据库操作该怎么写

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

数据库操作该如何写
数据库有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搞定

热点排行