这个函数究竟是出了什么问题?请教!

老田低代码 2005-08-26 05:53:50
我写了如下的函数,但是在调用的时候确出现了问题!
Create function CustPolicy(@CustID char(32))
returns nvarchar(1000)
As
Begin
declare @policyNO nvarchar(1000)
declare @PolicyNOTmp Nvarchar(50)

declare cur_Cust_Pol cursor for
select PolicyNO from policy where customerID=RTRIM(@CustID) /*这个查询语句绝对是没有问题*/

open cur_Cust_Pol

fetch cur_Cust_Pol into @PolicyNOTmp
if (@@fetch_status<>0)
Begin
close cur_Cust_Pol
deallocate cur_Cust_Pol
return @CustID /*但是每次调用的时候都到了这里就返回了呢,为什么啊?*/
end

while (@@fetch_status=0)
begin
set @PolicyNO=@PolicyNO+@PolicyNOTmp
fetch cur_Cust_Pol into @policyNOTmp
end

close cur_Cust_Pol
deallocate cur_Cust_Pol
return @policyNO
end
...全文
131 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
iwl 2005-08-28
  • 打赏
  • 举报
回复
fetch cur_Cust_Pol into @PolicyNOTmp --请改为

fetch next from cur_Cust_Pol into @PolicyNOTmp

只是有点奇怪 ,为什么要在用这个判断
老田低代码 2005-08-27
  • 打赏
  • 举报
回复
不会吧,@@fetch_status=0表示成功执行了最新的FETCH语句呢。。。。
ilons1 2005-08-27
  • 打赏
  • 举报
回复
肯定是每次在return @CustID 处返回啊,
你的while 条件就是 @@fetch_status=0 循环,也就是说,当@@fetch_status<>0时,就退出循环,而下面接着的语句就是你的if语句,
不执行才奇怪了.
老田低代码 2005-08-27
  • 打赏
  • 举报
回复
对于“WangZWang(阿来)”兄的这个我试了但是结果还是一样的,对于wgsasd311(为你解忧我开心)兄的这个如果我将这句这样改:
if (@@fetch_status<>0)
Begin
close cur_Cust_Pol
deallocate cur_Cust_Pol
return "wwwwwwwwwwwwwww" /*原来是return @CustID*/ /*但是每次调用的时候都到了这里就返回了呢,为什么啊?*/
end
返回全部为wwwwwwwwwwwwwww
我实在是有点不知道了呢!!!!

xianggang101 2005-08-27
  • 打赏
  • 举报
回复
函数的结果最后来统一输出,中间用变量储存。
xzq111 2005-08-27
  • 打赏
  • 举报
回复
以下是修改后的程序:
Create function CustPolicy(@CustID char(32))
returns nvarchar(1000)
As
Begin
declare @policyNO nvarchar(1000)
declare @PolicyNOTmp Nvarchar(50)
set @policyNO = '' --要为变量@policyNO 赋初值,否则缺省为null,那么set @PolicyNO=@PolicyNO+@PolicyNOTmp永远为null
declare cur_Cust_Pol cursor for
select PolicyNO from policy where customerID=RTRIM(@CustID) /*这个查询语句绝对是没有问题*/

open cur_Cust_Pol

fetch cur_Cust_Pol into @PolicyNOTmp
if (@@fetch_status<>0)
Begin
close cur_Cust_Pol
deallocate cur_Cust_Pol
--return @CustID /*但是每次调用的时候都到了这里就返回了呢,为什么啊?*/
set @policyNO = @CustID --函数结果统一由@policyNO输出
end
else --注意逻辑,只有两种情况
begin
while (@@fetch_status=0)
begin
set @PolicyNO=@PolicyNO+@PolicyNOTmp
fetch cur_Cust_Pol into @policyNOTmp
end

close cur_Cust_Pol
deallocate cur_Cust_Pol
end
return @policyNO
end
wgsasd311 2005-08-26
  • 打赏
  • 举报
回复
Create function CustPolicy(@CustID char(32))
returns nvarchar(1000)
As
Begin
declare @policyNO nvarchar(1000)
declare @PolicyNOTmp Nvarchar(50)

declare cur_Cust_Pol cursor for
select PolicyNO from policy where customerID=RTRIM(@CustID) /*这个查询语句绝对是没有问题*/

open cur_Cust_Pol
fetch next from cur_Cust_Pol into @PolicyNOTmp
while (@@fetch_status=0)
begin
set @PolicyNO=@PolicyNO+@PolicyNOTmp
fetch next from cur_Cust_Pol into @PolicyNOTmp
end

if (@@fetch_status<>0)
Begin
close cur_Cust_Pol
deallocate cur_Cust_Pol
return @CustID /*但是每次调用的时候都到了这里就返回了呢,为什么啊?*/
end


close cur_Cust_Pol
deallocate cur_Cust_Pol
return @policyNO
end
WangZWang 2005-08-26
  • 打赏
  • 举报
回复
fetch cur_Cust_Pol into @PolicyNOTmp --请改为

fetch next from cur_Cust_Pol into @PolicyNOTmp

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧