【问题标题】:Problem with returning records返回记录的问题
【发布时间】:2009-12-21 19:30:40
【问题描述】:

这是我最近几天面临的 Postgresql 问题:

使用 选择 usage_rep_sp.get_result('2009-01-01','2009-12-01')full_name from dual; 下面的包假设返回许多记录((至少 5 个不同的名称) 但是它只返回一个。

从 iReports 它给了我错误信息: 引起:org.postgresql.util.PSQLException: ERROR: cursor "()" does not exist ;

你能帮我解决这些问题吗?

CREATE OR REPLACE PACKAGE usage_rep_sp
IS

type usage_type is record (
full_name     varchar2(50));
-- 
 type srr_rec is ref cursor return usage_type;
 type mycursor is ref cursor;

function get_usage_rep(p_start_date  timestamp without time zone,
  p_end_date  timestamp without time zone)
  return srr_rec;

function get_result(p_start_date timestamp without time zone, p_end_date timestamp without time zone) return mycursor;

END usage_rep_sp;

CREATE OR REPLACE PACKAGE BODY usage_rep_sp
IS
function get_usage_rep
 (p_start_date  timestamp without time zone, p_end_date  timestamp without time zone)

  return      srr_rec
  is
  v_report            srr_rec;
  v_temp              varchar2(50):=' ';
  v_aff_level         varchar2(30);
  commapos            number ;        
  outstring           varchar2(50) := upper(v_temp) ;      
  vquery              varchar2(3200);
  whereclause         varchar2(3200);

begin
if v_temp =' ' or v_temp is null then
whereclause  := 'and  u.affiliate_id in  (select aff_id from ultra_affiliate)';      
else
for index_var in 1..50        
loop              
commapos   := instr(outstring,',',1,index_var) ;              
exit when commapos=0 ;          
outstring  := substr(outstring,1,(commapos-1))||''','''|| substr(outstring,(commapos+1));          
end loop ;                  
--outstring    := '('''||outstring||''')' ;  
v_temp      := outstring ;  


if v_aff_level= 'COUNCIL' then        
whereclause     := 'and  u.affiliate_id in  (select aff_id from ultra_affiliate where  council_id = '''|| v_temp ||''')';
elsif v_aff_level = 'DISTRICT' then
whereclause     := 'and  u.affiliate_id in  (select aff_id from ultra_affiliate where district = '''|| v_temp ||''')';
elsif v_aff_level= 'LOCAL' then
    whereclause     := 'and  u.affiliate_id in (select aff_id from ultra_affiliate where aff_id = '''|| v_temp ||''')';
end if;
end if;


open v_report for
  'select distinct initcap( u.first_name) || initcap( u.last_name )full_name '
     || chr (10)
     ||' from ubcsecurity.user_session s,cod_security_vw u, ultra_affiliate ua '
     || chr (10)
     ||' where s.user_name      = u.user_name '
     || chr (10)    
     ||' and   ua.aff_id = u.affiliate_id '
     || chr (10)    
     ||' and   s.login >= '''|| p_start_date|| ''' and   s.login <= '''|| p_end_date|| ''' '
     || chr (10)
     || whereclause
     || chr (10)
     || ' group by initcap( u.first_name) || initcap( u.last_name ) '
     || chr(10)
     ||' order by initcap( u.first_name) || initcap( u.last_name ) ';

   return v_report;
end get_usage_rep;

function get_result(p_start_date  timestamp without time zone, p_end_date  timestamp without time zone) return mycursor
is
        mycursor usage_rep_sp.srr_rec;
        myrec    usage_rep_sp.usage_type;

        begin

       select usage_rep_sp.get_usage_rep(p_start_date, p_end_date)
        into mycursor from dual;

       if mycursor%isopen then
                loop
                fetch mycursor into myrec;
                exit when mycursor%notfound;
                end loop;
                close mycursor;
        end if;
return myrec;
end  get_result;
END usage_rep_sp;

【问题讨论】:

  • 这就是垃圾。尝试重新格式化;选择并使用 Ctrl-K。

标签: enterprisedb


【解决方案1】:

我不知道你用的是什么,但这不是 PostgreSQL。 PostgreSQL 没有“双重”(这是 Oracle 的东西)。它没有包。没有 %isopen 运算符。几乎没有理由在函数中使用 PostgreSQL 中的游标。 PostgreSQL 中没有 varchar2 数据类型。

【讨论】:

  • 很抱歉给您带来了困惑。它是 8.3。 Oracle 兼容版本的 Postgresql。
  • 我完全不知道“PostgreSQL 的 Oracle 兼容版本”是什么。有一些基于 Pg 的数据库,内置扩展来模仿 Oracle - 我认为它是由 EnterpriseDB 提供的 - 也许你应该尝试支持它?
【解决方案2】:

您似乎正在尝试将光标上的声明和打开结合起来。我没有 EnterpriseDB,但我从未在 Oracle 或 PostgreSQL 上看到过这个。您可能需要声明游标,然后打开它。

【讨论】:

  • 不幸的是,即使我在 select usage_rep_sp.get_usage_rep(p_start_date, p_end_date) from dual 中为 mycursor 动态声明游标也没关系。结果是一样的。
  • 你得到一个游标不存在的错误。当您尝试定义光标,然后尝试打开它时,您会遇到什么错误?这些消息可能会揭示停止第一种格式的错误是什么。
猜你喜欢
  • 2020-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-14
相关资源
最近更新 更多