测试查询:
select * from testarray(1120,'{"oserial":
["10002695","10002692"],"nserial":[{"serial":"10002693","price":"180.00"},
{"serial":"10002697","price":"180.00"}]}');
CREATE OR REPLACE FUNCTION public.testarray(salesid int,items json)
RETURNS int
LANGUAGE plpgsql
AS $$
declare
resu text;
resu2 text := 'TH';
ssrow RECORD;
oserlen int := 0;
counter int := 0;
begin
select json_array_length(items::json->'oserial') into oserlen;
while counter < oserlen loop
-- if I change 0 to counter, this function does not work
select (items::json#>>'{oserial,0}') into resu;
select * into ssrow
from salesserial
where fk_salesid=salesid
and serialnum=resu::int;
insert into stockloct(serialnum,fk_barcode,source,exflag)
values(ssrow.serialnum,ssrow.fk_barcode,ssrow.fk_salesid,true);
counter := counter + 1;
end loop;
select items::json#>'{nserial,0,serial}' into resu2;
return oserlen;
end;
$$;
我正在测试PostgreSQL函数。
下面的这一行似乎无效,有人可以帮我吗。提前致谢。我正在使用9.5版的Postgresql。如果我将json字符串中的0更改为counter变量,则select不返回值。它仅适用于数字,不适用于变量。
select (items::json#>>'{oserial,0}') into resu;
-- if i change 0 to counter variable, this select returns null i think as the insert below the line fails;
答案 0 :(得分:0)
#>>
的右侧参数是一个文本数组。替换
select (items::json#>>'{oserial,0}') into resu;
使用
select items::json#>>array['nserial',counter::text] into resu;