日期:2014-05-19  浏览次数:20464 次

一个初学存储过程的学生的问题:
在存储过程中,我想执行一个SQL语句,
select   num   from   member   where   id=12345
如果存在这条记录则取出这条记录的NUM字段,不存在就插入一条新的记录,请教:

类似于这个功能:

sql   =   "select   num   from   member   where   id=12345 "
set   rs   =   conn.execute(sql)
if   not   rs.eof   then
  NUM   =   rs(0)
else
  sql   =   "insert   into   member   (num)   values   (123456) "
  conn.execute(sql)
end   if
rs.close
set   rs   =   nothing

我现在是在存储过程里执行这个过程,谢谢各位

------解决方案--------------------
create proc pc
@id int,
@num output
as
if exists(select 1 from member where id=@id)
select @num=num from member where id=@id
else
insert member(num) values(@id)
------解决方案--------------------
create proc pc
@id int,
@num output
as
if (select count(*) from member where id=@id)> 0
begin
select @num=num from member where id=@id
end
else
begin
insert member(num) values(@id)
end
------解决方案--------------------
你表的结构都没有怎么写啊?那我就凭我的理解帮你看看吧~
create proc spsel
as
if exists(select num from member where id=12345)
begin
declare @a int
select @a=num from member where id=12345
end
else
insert into member(num) values(123456)