在 plpgsql 中的 for 上使用变量

Using variable on a for in plpgsql

Solved the problem, the code was correct, the problem is that some items in db had no data. My apologies for anything.

我创建了一个从 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 =   and it.templateid is null and it.name like ' using hid,sb loop

select return 为 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  ;
    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 =   and it.templateid is null and it.name like ' 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$;

问题解决,代码是正确的,问题是db中的某些项目没有数据。我对任何事情表示歉意。