Chinaunix

标题: sybase存储过程问题 [打印本页]

作者: yeath    时间: 2005-07-05 23:06
标题: sybase存储过程问题
SYBASE存储过程,为什么执行到这一句就不执行了
select @nSerialNo = serialno from schedule where phonenum= @strPhoneNum and type = @nType
if @@rowcount = 0
begin
...
end
else
begin
...
end
上面的select结果是空记录集,存储过程一执行到空记录集,就会返回?
作者: 2BeSybPro    时间: 2005-07-06 00:50
标题: sybase存储过程问题
update ur sp and try to use:
if exists (select ....)
begin
..
end
else
begin
..
end
作者: yeath    时间: 2005-07-06 08:48
标题: sybase存储过程问题
if exists(select @nSerialNo = serialno from schedule where phonenum= @strPhoneNum and type = @nType)这样的语句是错误的,执行不了。我想问一下为什么上面的写法不行?
作者: yeath    时间: 2005-07-06 13:24
标题: sybase存储过程问题
没人知道这是什么原因,高手别沉默啊
作者: chuxu    时间: 2005-07-06 14:30
标题: sybase存储过程问题
满足这个phonenum= @strPhoneNum and type = @nType条件的是不是不止一条呀?
作者: yeath    时间: 2005-07-06 18:55
标题: sybase存储过程问题
满足这个phonenum= @strPhoneNum and type = @nType条件的记录为空(没有满足这个条件的记录)
作者: chuxu    时间: 2005-07-06 20:10
标题: sybase存储过程问题
总觉得if exists(select @nSerialNo = serialno from schedule where phonenum= @strPhoneNum and type = @nType)这个语句的逻辑有点问题?
if exists 一般是用来确定是否存在类似的记录?你现在又要将存在的结果赋值?其实还不如写 if exists(select 1 from schedule where phonenum= @strPhoneNum and type = @nType)
确定是否存在这样的记录,在执行相应的处理
作者: yeath    时间: 2005-07-06 23:12
标题: sybase存储过程问题
有没有类似on exception resume这样的语句?就是执行到了空记录,还是继续往下执行。
作者: chuxu    时间: 2005-07-07 07:57
标题: sybase存储过程问题
可以用isnull(serialno,0)的语法,在没记录时赋一个固定的值。
作者: yeath    时间: 2005-07-07 12:25
标题: sybase存储过程问题
现在是一执行到空记录的select,整个存储过程就返回了,根本不往下面走。
作者: camham    时间: 2005-07-07 13:18
标题: sybase存储过程问题
把你的完整pr贴出来可以吗?
作者: hobbylu    时间: 2005-07-07 14:41
标题: sybase存储过程问题
看看你完整的存储过程吧
作者: yeath    时间: 2005-07-08 19:29
标题: sybase存储过程问题

  1. --查询歌曲列表,每次返回一首
  2. if @OperateID = '00100' and @nFieldNums = 2
  3. begin
  4.         --包体解析
  5.     exec dbo.GetField @strSource=@Body,@strSplit=';',@nField=1,@strRet=@strPhoneNum output
  6.     exec dbo.GetField @strSource=@Body,@strSplit=';',@nField=2,@strRet=@strType output

  7.     select @nType = convert(int,@strType)
  8.         select @nFavSongNum = 0

  9.         select @nSerialNo = serialno from wxdg_schedule where phonenum= @strPhoneNum and type = @nType
  10.         --执行了上面的select语句,就无法继续往下执行(执行不了下面的IF语句)
  11.         if @@rowcount = 0
  12.         begin
  13.                 if @nType = 6
  14.                 begin
  15.             set rowcount 1
  16.                         select @nSerialNo=a.serialno,@strVoiceFile=isnull(a.voicefile,''),@strResourceVoice=a.musicfile,@nID=a.id from wxdg_song a,wxdg_favorite b where a.id=b.id  and b.phonenum=@strPhoneNum and a.musicfile is not null order by a.serialno
  17.                         --收藏夹无内容
  18.                         if @@RowCount = 0
  19.                         begin
  20.                                 select '1;||;0'
  21.                 set rowcount 0
  22.                                 return
  23.                         end
  24.             set rowcount 0
  25.                 end
  26.                 else
  27.                 begin
  28.             set rowcount 1
  29.                         select @nSerialNo=serialno,@strVoiceFile=isnull(voicefile,''),@strResourceVoice=musicfile,@nID=id from wxdg_song where type=@nType and musicfile is not null order by serialno
  30.             set rowcount 0
  31.                 end

  32.                 insert into wxdg_schedule(phonenum,type,serialno) values(@strPhoneNum,@nType,@nSerialNo)
  33.         end
  34.     else
  35.         begin
  36.                 if @nType=6
  37.                 begin
  38.             set rowcount 1
  39.                          select @nSerialNo=a.serialno,@strVoiceFile=isnull(a.voicefile,''),@strResourceVoice=a.musicfile,@nID=a.id from wxdg_song a,wxdg_favorite b
  40.                         where a.musicfile is not null and a.serialno>;@nSerialNo and a.id=b.id and b.phonenum=@strPhoneNum order by a.serialno
  41.                         if @@RowCount = 0
  42.                         begin
  43.                                 select @nSerialNo=a.serialno,@strVoiceFile=isnull(a.voicefile,''),@strResourceVoice=a.musicfile,@nID=a.id from wxdg_song a,wxdg_favorite b
  44.                                   where a.id=b.id and b.phonenum=@strPhoneNum and a.musicfile is not null order by a.serialno
  45.                                 --收藏夹无内容
  46.                                 if @@RowCount = 0
  47.                                 begin
  48.                                         select '1;||;0'
  49.                     set rowcount 0
  50.                                         return
  51.                                 end
  52.                         end
  53.             set rowcount 0
  54.                 end
  55.                 else
  56.                 begin
  57.             set rowcount 1
  58.                         select @nSerialNo=serialno,@strVoiceFile=isnull(voicefile,''),@strResourceVoice=musicfile,@nID=id from wxdg_song where type=@nType and musicfile is not null and serialno>;@nSerialNo order by serialno
  59.                         if @@rowcount = 0
  60.                         begin
  61.                                 select @nSerialNo=serialno,@strVoiceFile=isnull(voicefile,''),@strResourceVoice=musicfile,@nID=id from wxdg_song where type=@nType and musicfile is not null order by serialno
  62.                         end
  63.             set rowcount 0
  64.                 end

  65.                 update wxdg_schedule set serialno=@nSerialNo where phonenum=@strPhoneNum and type=@nType
  66.         end
  67.         if @nType=6
  68.         begin
  69.                 select @nFavSongNum=count(1) from wxdg_favorite where phonenum=@strPhoneNum
  70.         end

  71.         select '1;'+convert(varchar,@nID)+'|'+@strVoiceFile+'|'+@strResourceVoice+';'+convert(varchar,@nFavSongNum)
  72.         return
  73. end
复制代码

作者: 2BeSybPro    时间: 2005-07-08 22:38
标题: sybase存储过程问题
Man, Chuxu already answered your question.
select @nSerialNo = serialno from wxdg_schedule where phonenum= @strPhoneNum and type = @nType
If the result set is null, you will get error.
So you can do either way:
1. select @nSerialNo = isnull(serialno,0) from wxdg_schedule where phonenum= @strPhoneNum and type = @nType
if @SerialNo = 0 -- don't use @@rowcount
begin
...
end
else
begin
...
end

2. Put some error handling code after this sql statement
作者: jysql    时间: 2005-07-09 23:52
标题: sybase存储过程问题
你是不是不用判断@@rowcount,而直接判断@@sqlstatus呢
作者: yeath    时间: 2005-07-10 02:10
标题: sybase存储过程问题
可能是我的SYBASE没安装好吧,怎么会有这种问题。
作者: wyz9324    时间: 2005-07-19 17:26
标题: sybase存储过程问题
有详细的出错信息吗?




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2