case语句不能与ref cursor一起使用

时间:2013-08-17 19:32:51

标签: stored-procedures plsql case

我正在编写一个存储过程来从表add_files_tables中检索匹配的文件位置。我使用case语句与ref cursor获取结果集,然后将打印文件名。我已经创建了包,但它总是给我空集。问题出在哪里。

create or replace package search_cur as
    type my_cursor is ref cursor;
    function search_File(FILE_NAME varchar2,opt number) return my_cursor;
end search_cur;
/

create or replace package body search_cur as
    function search_File(FILE_NAME varchar2,opt number) return my_cursor is
        ret my_cursor;
    begin
        Case opt
            when 1 THEN
                OPEN ret FOR
                    select file_location
                      from add_files_details
                     where upper(username) like '%'||file_Name||'%'
                ;
                return ret;
            when 2 THEN
                OPEN ret FOR
                    select file_location
                      from add_files_details
                     where upper(EXTENSION) like '%'||file_name||'%'
                ;
                return ret;
            WHEN 3 THEN
                OPEN ret FOR
                    select file_location
                      from add_files_details
                     where upper(UPLOAD_DATE) like '%'||file_name||'%'
                ;
                return ret;
            WHEN 4 THEN
                OPEN ret FOR
                    select file_location
                      from add_files_details
                     where upper(FOLDER_ID) like '%'||file_name||'%';
                return ret;
            when 5 then
                open ret for
                    select file_location
                      from add_files_details
                     where upper(file_name) like '%'||file_name||'%';
                return ret;
        end case;
    end search_file;
end search_cur;
/

1 个答案:

答案 0 :(得分:1)

更改参数的名称,使其不与字段名称冲突。表格中有一个名为file_name的字段,例程的参数为FILE_NAME。这些在PL / SQL编译器的眼中是等价的,并且可能猜测应该使用哪一个错误。尝试类似:

create or replace package body search_cur as
    function search_File(inp_Search_value varchar2,
                         inp_Opt          number) return my_cursor is
        ret my_cursor;
    begin
        Case inp_Opt 
            when 1 THEN
                OPEN ret FOR
                    select file_location
                      from add_files_details
                     where upper(username) like '%'||UPPER(inp_Search_value ) ||'%'
                ;
                return ret;
            when 2 THEN
                OPEN ret FOR
                    select file_location
                      from add_files_details
                     where upper(EXTENSION) like '%'||UPPER(inp_Search_value )||'%'
                ;
                return ret;
            WHEN 3 THEN
                OPEN ret FOR
                    select file_location
                      from add_files_details
                     where upper(UPLOAD_DATE) like '%'||UPPER(inp_Search_value )||'%'
                ;
                return ret;
            WHEN 4 THEN
                OPEN ret FOR
                    select file_location
                      from add_files_details
                     where upper(FOLDER_ID) like '%'||UPPER(inp_Search_value )||'%';
                return ret;
            when 5 then
                open ret for
                    select file_location
                      from add_files_details
                     where upper(file_name) like '%'||UPPER(inp_Search_value )||'%';
                return ret;
        end case;
    end search_file;
end search_cur;

由于输入字段并不总是文件名,因此将其称之为误导 - 我建议使用如上所示的通用类型名称。此外,如果您将数据库字段转换为大写字母,那么转换搜索参数也是个好主意。

分享并享受。