错误:光标&#34; <未命名的门户网站=“”>&#34;不存在

时间:2017-07-26 07:07:01

标签: postgresql plpgsql

根据此过程,光标将显示两行,但是当我执行此查询时,它显示ERROR:cursor&#34; unnamed portal&#34;不存在 在图像表中

CREATE TABLE image
(
  id numeric(9,0) NOT NULL,
  nm_code character varying(100) NOT NULL,
  synonym text NOT NULL,
  CONSTRAINT synonym_id PRIMARY KEY (id)
)
INSERT INTO image(
            id, nm_code, synonym)
    VALUES (01,'13160101','CAR TYRE');
INSERT INTO image(
                id, nm_code, synonym)
        VALUES (01,'10000101','CAR TYRE');

旧版主表

CREATE TABLE legacymaster
(
  legacy_code character varying(20),
  source_data text,
)

insert into legacymaster (legacy_code,source_data) values ('123','WITH CAR TYRE FROM AUDI 2000')

mm_nounmodmaster表结构

CREATE TABLE mm_nounmodmaster
(
  nm_code character varying(18) NOT NULL,
  noun character varying(35),
  modifier1 character varying(35) NOT NULL,
  modifier2 character varying(35),
  modifier3 character varying(35),
  nm_type character(1) NOT NULL,
  nm_abbr character varying(200),
  nm_description character varying(80) NOT NULL,
  is_template character(1) DEFAULT 'N'::bpchar,
  )


    INSERT INTO mm_nounmodmaster(nm_code, noun, modifier1, modifier2, modifier3, nm_type, nm_abbr, 
            nm_description,is_template)
    VALUES ("10000101","ABRASIVE","--","--","","F","--","ABRASIVE",'Y')

    INSERT INTO mm_nounmodmaster(nm_code, noun, modifier1, modifier2, modifier3, nm_type, nm_abbr, 
                nm_description,is_template)
                VALUES ("13160101","TYRE","AUTOMOBILE","--","","F","TYR,AUTO","TYRE,AUTOMOBILE",'Y')
    INSERT INTO mm_nounmodmaster(nm_code, noun, modifier1, modifier2, modifier3, nm_type, nm_abbr, 
            nm_description,is_template)
            VALUES ("10020101","ACTUATOR","ELECTRICAL","--","--","F","ACTR,ELE","ACTUATOR,ELECTRICAL",'Y')
在nounmodmaster表中

插入了三条记录,但在图像表中,同义词包含了“汽车轮胎”和“汽车轮胎”。只有两个nm_codes。因此,当我执行此函数时,游标将在nounmodmaster中执行除最后一行之外的两行,因为它与图像表不匹配

CREATE OR REPLACE FUNCTION func_source_based_nounmod(legacy_code_in character varying)
                  RETURNS refcursor AS
                $BODY$
                    declare 
                        source text;
                        nmcode character varying(50);
                        v_parent_Rec1 record;
                        v_parent_Rec2 record;
                        item_desc character varying(50);
                        noun_t character varying(50);
                        mod1 character varying(50);
                        CUR REFCURSOR;
                begin
                    --select source_data into source from legacymaster where source_data=legacy_code_in;
                    raise notice '1';
                    for v_parent_Rec1 in(SELECT id,nm_code,synonym FROM image)loop
                raise notice '2';
                        --if exists (select source_data from legacymaster where legacy_code=legacy_code_in) then
                raise notice '3';
                        IF exists (select source_data from legacymaster where legacy_code=legacy_code_in and source_data ilike '%'||v_parent_Rec1.synonym||'%') THEN
                raise notice '4';
                            FOR v_parent_Rec2 IN(SELECT DISTINCT AA.NM_CODE, NM_TYPE,
                        (CASE WHEN NM_ABBR IS NOT NULL THEN NM_ABBR ELSE '--' END) as NM_ABBR,
                        NM_DESCRIPTION, NM_CATEGORY,
                        (CASE WHEN image.NM_CODE IS NOT NULL THEN 'ACTIVE' ELSE 'INACTIVE' END) as STATUS,
                        (CASE WHEN NOUN IS NOT NULL AND NOUN <> '--' AND trim(both ' ' from NOUN) <> '' THEN NOUN ELSE '' END) || 
                        (CASE WHEN MODIFIER1 IS NOT NULL AND MODIFIER1 <> '--' AND TRIM(BOTH ' ' FROM MODIFIER1) <> '' THEN ',' || MODIFIER1 ELSE '' END) ||
                        (CASE WHEN MODIFIER2 IS NOT NULL AND MODIFIER2 <> '--' AND TRIM(BOTH ' ' FROM MODIFIER2) <> '' THEN ',' || MODIFIER2 ELSE '' END) ||
                        (CASE WHEN MODIFIER3 IS NOT NULL AND MODIFIER3 <> '--' AND TRIM(BOTH ' ' FROM MODIFIER3) <> '' THEN ',' || MODIFIER3 ELSE '' END) AS NOUNMOD,
                        is_template, Count(image.nm_code) AS CountOfnm_code from mm_nounmodmaster aa
                            inner join image on image.nm_code=aa.nm_code
                            where image.synonym =v_parent_Rec1.synonym and image.nm_code=v_parent_Rec1.nm_code group by aa.nm_code,image.nm_code)LOOP

                    --raise notice '%',v_parent_Rec2.noun;
                    --raise notice '%',v_parent_Rec2.modifier1;
                        END LOOP;
                        ITEM_DESC:=v_parent_Rec1.nm_code;
                    raise notice '%',ITEM_DESC;
                    raise notice '%',v_parent_Rec2.nm_code;
                    raise notice '%',v_parent_Rec2.NM_TYPE;
                    raise notice '%',v_parent_Rec2.NM_ABBR;
                    raise notice '%',v_parent_Rec2.NM_DESCRIPTION;
                    raise notice '%',v_parent_Rec2.NM_CATEGORY;
                    raise notice '%',v_parent_Rec2.NOUNMOD;
                    raise notice '%',v_parent_Rec2.CountOfnm_code;
                        --end if;
                        OPEN CUR FOR 
                    SELECT DISTINCT v_parent_Rec2.nm_code,v_parent_Rec2.NM_TYPE,v_parent_Rec2.NM_ABBR,v_parent_Rec2.NM_DESCRIPTION,v_parent_Rec2.NM_CATEGORY,v_parent_Rec2.NOUNMOD,v_parent_Rec2.CountOfnm_code;
                    close cur;
                        end if;
                        end loop;
                  raise notice '5';
                RETURN CUR;
                    end;
                $BODY$

通过传递Legacy_code_in参数,它在legacymaster表中,如果源数据字符串包含&#39; Car Tire&#39;。然后它会显示两行与图像表

比较

我需要

1 个答案:

答案 0 :(得分:1)

首先,你得到的错误是因为,除非你专门给游标命名,否则postgres会返回一个带有特殊名称&#34;未命名的门户网站&#34;它可以继续1,2,3等。

这一般都没有用,但幸运的是有一个简单的选择。您只需将光标作为参数传递给函数,因此您的过程将开始:

CREATE OR REPLACE FUNCTION func_source_based_nounmod(legacy_code_in character varying, cur refcursor)

假设您从查询窗口调用此函数,现在可以使用如下代码:

BEGIN;
select func_source_based_nounmod('123', 'mycursor');
FETCH ALL IN "mycursor";
COMMIT;

不幸的是,这很容易!鉴于您发布的数据,BTW格式错误以及功能中缺少列,您会看到我没有通过“汽车轮胎”#39;正如你似乎建议但是&#39; 123&#39;这是必要的,因为否则你永远不会打开光标,因为你有

IF exists (select source_data from legacymaster 
where legacy_code=legacy_code_in and

所以legacy_code_in必须匹配legacymaster中的代码。

然后你的问题变得更糟。

你的SELECT(FOR v_parent_Rec2 IN(SELECT DISTINCT AA.NM_CODE,NM_TYPE等)中有一个Count()。这意味着SELECT中的所有项目都需要在Group BY中而不仅仅是其中一些!由于上面的表结构缺少类别,因此我在运行代码时遇到了一个特殊问题。

最后这一切都在循环中。在此循环结束时,您可以打开和关闭光标。你似乎在误解这样做,你将能够用连续的行填充光标。这不是它的工作原理。

类似地,然后在关闭光标后返回光标(因此原始错误消息)。为了能够访问光标,返回时需要打开光标。

那你怎么解决这一切呢?我建议您使用临时表。在调用函数之前清除表。只需在循环结束时添加行,而不是在循环结束时打开和关闭游标。然后运行从表中读取的函数。如果需要在游标中使用结果集,只需在临时表上打开游标即可。