0

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

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;
/
4

1 回答 1

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;

由于输入字段并不总是文件名,因此将其称为文件名会产生误导 - 我建议使用如上所示的通用名称。此外,如果要将数据库字段转换为大写,最好也转换搜索参数。

分享和享受。

于 2013-08-17T20:52:43.770 回答