一个 function 执行到中途突然“停止”了

fxfeixue 2008-07-02 06:26:59
环境:
有一个function 名为F_A ,参数形式(sStr in varchar2,sTableName in varchar2,sColumnName in varchar2,sContidionColumnName in varchar2)
其功能是在 sTableName 表中查找 sContidionColumnName 字段的值等于 sStr ,返回列 sColumnName 的值。
例如:我要查询表中 UserID 对应的 UserName,select F_A(userid, '用户表','用户表_userName','用户表_userID') as 用户名 from 表1

问题:
表1中有200多条记录,上例中的查询语句执行后,有一半的记录没有被转换,比较能正常转换和不能正常转换的记录,没发现什么区别,请高人指点
...全文
132 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ahalf 2008-07-19
  • 打赏
  • 举报
回复
Exception
when others then
insert into table value (....err_code....)

试试
yegoo 2008-07-04
  • 打赏
  • 举报
回复
(苦练Java和Oracle中……)
新鲜鱼排 2008-07-03
  • 打赏
  • 举报
回复
是不是由于字符长度的限制。
fxfeixue 2008-07-02
  • 打赏
  • 举报
回复
我在最后一个end 的上面加了
Exception
when others then
Dbms_Output.put_line('exception');

在SQL plus 中执行
select namesliststr(A.DROWER,'AS005','USERID','USERCNAME') AS AUTHOR from AS02 A
没有报异常
这是以前的一个同事写的,我不懂oracle,还请各位多费心了 :(
ahalf 2008-07-02
  • 打赏
  • 举报
回复
说明一下
是全局的
ahalf 2008-07-02
  • 打赏
  • 举报
回复
加上Exception部分俘获一下错误
fxfeixue 2008-07-02
  • 打赏
  • 举报
回复
CREATE OR REPLACE FUNCTION "NAMESLISTSTR" (
sStr in varchar2,--需要转换的字符串,用逗号隔开
sTableName in varchar2,--转换查询的表名称
sColumnName in varchar2,--转换列名
sContidionColumnName in varchar2--需要转换的列名
)
return varchar2
as
aIndex int := 0 ; --逗号索引
sNameStr varchar2(500);--条件值
sParamValues varchar2(500);--剩余的参数字符串
aa varchar2(1000); -- 临时变量
iIsWhile int := 1;--标注是否循环
sSql varchar2(1000);--动态sql语句
tempvalue varchar2(100);--查询到的临时值
sColValue varchar2(1000);--返回值
begin
--if(sStr is null) then
-- return '';
--end if;

aIndex := instr(sStr,',',1,1);

if(aIndex = 0) then
begin
BEGIN
sSql := 'SELECT ' || sColumnName || ' FROM ' || sTableName || ' where ' || sContidionColumnName || ' = ''' || sStr || '''';
execute immediate sSql into sColValue;
--SELECT USERCNAME into sColValue FROM ASREG005 where USERID = sStr;
-- SELECT sColumnName into sColValue FROM sTableName where sColumnName = sStr;
EXCEPTION
WHEN NO_DATA_FOUND THEN
sColValue := sStr;
END;
end;
else

begin
--获取第一个名字
sNameStr := substr(sStr,1,aIndex-1);
begin
sSql := 'SELECT ' || sColumnName || ' FROM ' || sTableName || ' where ' || sContidionColumnName || ' = ''' || sNameStr || '''';

execute immediate sSql into tempvalue;
-- SELECT sColumnName into sValue FROM sTableName where sColumnName = sNameStr;
EXCEPTION
WHEN NO_DATA_FOUND THEN
tempvalue := sNameStr;
END;
sColValue := tempvalue ;--保存值

sParamValues := substr(sStr,aIndex +1,length(sStr) - aIndex); --:1:2:3:4:5:6:7:8

if length(trim(sParamValues)) > 1 then
begin
sColValue := sColValue || ',' ;
iIsWhile := 1;
end;
else
begin
iIsWhile := 0;
end;
end if;

WHILE iIsWhile = 1 LOOP --执行循环
begin
aIndex := instr(sParamValues,',',1,1);
if aIndex = 0 then
begin
sNameStr := sParamValues;
end;
else
begin
sNameStr := substr(sParamValues,1,aIndex-1);
end;
end if;

sSql := 'SELECT ' || sColumnName || ' FROM ' || sTableName || ' where ' || sContidionColumnName || ' = ''' || sNameStr || '''';
execute immediate sSql into tempvalue;
-- SELECT sColumnName into sValue FROM sTableName where sColumnName = sNameStr;
EXCEPTION
WHEN NO_DATA_FOUND THEN
tempvalue := sNameStr;
END;

sParamValues := substr(sParamValues,length(sNameStr) + 2,length(sParamValues) - length(sNameStr)- 1);
aa := sColValue || tempvalue ;
-- sColValue :=
--添加逗号
if length(trim(sParamValues)) > 0 then
begin
sColValue := aa || ',' ;
end;
else
begin
sColValue := aa;
iIsWhile := 0;
end;
end if;

END LOOP;
end;
end if;
return(sColValue);
end;
hebo2005 2008-07-02
  • 打赏
  • 举报
回复
function贴出来看看有什么错误

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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