如何检查数组索引元素是否存在

时间:2019-12-23 07:05:35

标签: postgresql

我正在尝试检查数组索引元素是否存在。

clmdata [atrindx]它给出NULL。

PostgreSQL代码。

do $$
begin 
   CREATE DOMAIN vararraytype AS numeric(9)[];
END$$;

do $$
begin
create type custschema.coldata_r as (sno numeric, cname varchar, dids integer, dloc varchar2);
END $$;

do $$
begin
create domain custschema.coldata as coldata_r[];
END $$;

CREATE OR REPLACE FUNCTION custschema.upds_func(
p_indarray vararraytype)
RETURNS void AS
$body$
    declare
        v_col_array      custschema.colrectab;
        v_columnRec        custschema.colrec;
        rec record;
        clmdata custschema.coldata; 
begin
for rec in (select sno,cname,dids,dloc from customer where custid='CUST12')
                  loop
                    clmdata[rec.sno]:=rec;
                    raise info ' clmdata[rec.sno] %',clmdata[rec.sno];
                  end loop;
raise info 'p_indarray %',array_length(p_indarray,1);
for x IN 1 .. coalesce(array_length(p_indarray,1),0) 
loop
v_columnRec.colIndex := p_indarray[x] + 2;
raise info 'v_columnRec.colIndex %',v_columnRec.colIndex;

perform custschema.arrayindex(v_columnRec.colIndex)

v_col_array[x] := v_columnRec;
end loop;
end $body$ language plpgsql;

Output of raise info  from "custschema.upds_func" function.
raise info ' clmdata[rec.sno] %',clmdata[rec.sno];

clmdata[rec.sno] (1,Abi,15,Hyd)
clmdata[rec.sno] (2,Jhon,15,Mum)
clmdata[rec.sno] (3,Thomas,20,Bang)
clmdata[rec.sno] (4,Marry,25,SYD)
clmdata[rec.sno] (5,Tina,7,KCP)
clmdata[rec.sno] (6,Jorge,10,LOL)
clmdata[rec.sno] (7,Michal,15,CHE)
clmdata[rec.sno] (8,Chanu,9,USA)

raise info 'p_indarray %',array_length(p_indarray,1); -- O/P 3
raise info 'v_columnRec.colIndex %',v_columnRec.colIndex; -- O/P 3


create or replace  function custschema.arrayindex(atrindx  IN NUMERIC,cname   out VARCHAR) AS
$body$
declare 
clmdata custschema.coldata;
begin
raise info 'atrindx %',atrindx;
raise info 'sno %',clmdata[attrIndex].sno;
if clmdata[atrindx].sno IS NULL then
raise exception e'%',  'invalid index';
end if;
cname := clmdata[atrindx].col_nme;
end $body$ language plpgsql;

Output of raise info  from "custschema.arrayindex" function.
raise info 'atrindx %',atrindx;  --O/P 3
raise info 'sno %',clmdata[attrIndex].sno; -- O/P NULL

引发异常“无效索引”。

Oracle代码。

procedure arrayindex(atrindx  IN integer,cname   OUT varchar2) 
is
v_sno integer;
begin
dbms_output.put_line('atrindx' ||atrindx);
v_sno:=clmdata(attrIndex).sno;
dbms_output.put_line('v_sno'||v_sno );
if (not clmdata.exists(atrindx)) then  
gnvGen.toStr(attrIndex-2));
raise ind_excetion;
end if;
cname := clmdata(atrindx).col_nme;
end;

dbms_output.put_line('atrindx' ||atrindx); -- O/P 3
dbms_output.put_line('v_sno'||v_sno );
v_sno 3
v_sno 4
v_sno 5

在Oracle v_sno:=clmdata(attrIndex).sno;中,数据为3、4、5。

而在PostgreSQL v_sno:=clmdata(attrIndex).sno;中则为NULL。

我的期望是在“ custschema.upds_func”函数中为clmdata [rec.sno]:= rec设置的值是什么。 应该将其转发给custschema.arrayindex函数。

for rec in (select sno,col_nme,dids,dloc from customer where custid='CUST12')
                  loop
                    clmdata[rec.sno]:=rec;
                    raise info ' clmdata[rec.sno] %',clmdata[rec.sno];
                  end loop;

raise info ' clmdata[rec.sno] %',clmdata[rec.sno];

clmdata[rec.sno] (1,Abi,15,Hyd)
clmdata[rec.sno] (2,Jhon,15,Mum)
clmdata[rec.sno] (3,Thomas,20,Bang)
clmdata[rec.sno] (4,Marry,25,SYD)
clmdata[rec.sno] (5,Tina,7,KCP)
clmdata[rec.sno] (6,Jorge,10,LOL)
clmdata[rec.sno] (7,Michal,15,CHE)
clmdata[rec.sno] (8,Chanu,9,USA)

我尝试了您的建议,就是跳过错误

.
But not assigning values to "cname" variable. 

if cardinality(clmdata) < atrindx then
raise exception e'%',  'invalid index';
end if;
cname := clmdata[atrindx].col_nme;
raise info  'cname %',col_name; -- This is printing null.

It should print  col_nme as below.
Thomas
Marry
Tina

谢谢。

0 个答案:

没有答案
相关问题