解决了问题,代码是正确的,问题是db中的某些项目没有数据。我为任何事情道歉。
我创建了一个函数来从postgresql中获取一些值,在我的测试中我发现了一个我无法解决的问题。 我有以下行
for job in select it.name from items it where it.hostid = '3213' and it.templateid is null and it.name like 'Bacula%Status' loop
它有效,并继续进行。但如果我换了这个
declare
hid integer = '10239';
begin
for job in select it.name from items it where it.hostid = hid and it.templateid is null and it.name like 'Bacula%Status' loop
或者
for job in execute 'select it.name from items it where it.hostid = $1 and it.templateid is null and it.name like $2' using hid,sb loop
select返回null并且不输入。
任何想法?
这里是所有代码
CREATE OR REPLACE FUNCTION public.bacula_status(varchar
)
RETURNS TABLE(name character varying, itembf numeric, itembi numeric, itembd numeric, itemdf numeric, itemdi numeric, itemdd numeric, itemff numeric, itemfi numeric, itemfd numeric, itemle integer, itemst char )
LANGUAGE 'plpgsql'
COST 100
VOLATILE
ROWS 1000
AS $BODY$
declare
job record;
item integer;
itemle integer;
itembf numeric;
itembi numeric;
itembd numeric;
itemdf numeric;
itemdi numeric;
itemdd numeric;
itemff numeric;
itemfi numeric;
itemfd numeric;
itemst char;
ht ALIAS FOR $1 ;
hid integer;
sb varchar = 'Bacula%Status';
begin
select h.hostid into hid from hosts h where h.name like ht;
raise notice 'hid %',hid;
for job in execute 'select it.name from items it where it.hostid = $1 and it.templateid is null and it.name like $2' using hid,sb loop
raise notice 'hid2 %',hid;
raise notice 'jobname %',job.name;
job.name = substring(job.name from 'Bacula Job (.*) Status');
execute 'select itemid from items where name like ''%' ||job.name|| ' Last Execution'';' into item;
execute 'select value from history_uint where itemid = ' ||item|| 'order by clock desc limit 1;' into itemle;
execute 'select itemid from items where name like ''%' ||job.name|| ' Bytes FULL'';' into item;
execute 'select value from history_uint where itemid = ' ||item|| 'order by clock desc limit 1;' into itembf;
execute 'select itemid from items where name like ''%' ||job.name|| ' Bytes INCREMENTAL'';' into item;
execute 'select value from history_uint where itemid = ' ||item|| 'order by clock desc limit 1;' into itembi;
execute 'select itemid from items where name like ''%' ||job.name|| ' Bytes DIFFERENTIAL'';' into item;
execute 'select value from history_uint where itemid = ' ||item|| 'order by clock desc limit 1;' into itembd;
execute 'select itemid from items where name like ''%' ||job.name|| ' Duration FULL'';' into item;
execute 'select value from history_uint where itemid = ' ||item|| 'order by clock desc limit 1;' into itemdf;
execute 'select itemid from items where name like ''%' ||job.name|| ' Duration INCREMENTAL'';' into item;
execute 'select value from history_uint where itemid = ' ||item|| 'order by clock desc limit 1;' into itemdi;
execute 'select itemid from items where name like ''%' ||job.name|| ' Duration DIFFERENTIAL'';' into item;
execute 'select value from history_uint where itemid = ' ||item|| 'order by clock desc limit 1;' into itemdd;
execute 'select itemid from items where name like ''%' ||job.name|| ' Files FULL'';' into item;
execute 'select value from history_uint where itemid = ' ||item|| 'order by clock desc limit 1;' into itemff;
execute 'select itemid from items where name like ''%' ||job.name|| ' Files INCREMENTAL'';' into item;
execute 'select value from history_uint where itemid = ' ||item|| 'order by clock desc limit 1;' into itemfi;
execute 'select itemid from items where name like ''%' ||job.name|| ' Files DIFFERENTIAL'';' into item;
execute 'select value from history_uint where itemid = ' ||item|| 'order by clock desc limit 1;' into itemfd;
execute 'select itemid from items where name like ''%' ||job.name|| ' Status'';' into item;
execute 'select value from history_str where itemid = ' ||item|| 'order by clock desc limit 1;' into itemst;
return query select job.name as "Job",
itembf as "Bytes F",
itembi as "Bytes I",
itembd as "Bytes D",
itemdf as "Duration F",
itemdi as "Duration I",
itemdd as "Duration D",
itemff as "Files F",
itemfi as "Files I",
itemfd as "Files D",
itemle as "LastExecution",
itemst as "Status";
end loop;
raise notice 'fora for';
return;
end;
$BODY$;
答案 0 :(得分:0)
解决了问题,代码是正确的,问题是db中的某些项目没有数据。我为任何事情道歉。