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

求一存储过程 判断是否存在,如果不存在,就添加一条记录,如果存在,就返回另外两个字段ID与数量的值
我感觉这个存储过程好难,我只写以下部分,就不知道如何动手了
create proc Qcountifinsert @ga51 datetime ,@ga52 varchar(200),@ga01 varchar(200)
as
--判断是否存在,如果不存在,就添加一条记录,如果存在,就返回另外两个字段ID与数量的值

SELECT * FROM Qcount


返回的字段是ID,跟ga91字段. 请大家指点,或者给我一个示例,非常感谢.

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

if exists(select 1 from tb where .......)
begin
存在做什么
end
else 
begin
不存在做什么
end

------解决方案--------------------
1楼正确.
如果楼主需要返回一个ID和ga91字段,那么你可以这么做。那么按照1楼变化一下:

delclare @ID int

if exists(select 1 from tb where .......)
begin
select @ID = ID from tb where .......
end
else 
begin
set @ID = (select max(ID) + 1 from tb)
insert into ...
end

select ID, ga91 from tb
where ID = @ID