Postgresql - 创建函数 - RAISE INFO不打印预期值

时间:2015-02-12 06:07:20

标签: sql postgresql

在函数中,在以下行中

RAISE INFO 'MY TEST - Run - 25 - Now periodID is %', periodID;  

我期望变量periodID保存由查询返回的,但它正在显示,但是当我单独执行时,以下查询返回一个整数值,请帮助解决此问题

select distinct periodid into periodID 
                  from ccc_financialperiod fp 
                  where fp.periodtype = 'Year' 
                  and fp.periodfiscalyear = vsYear 
                  and fp.organizationid = oaPermID  
                  and fp.periodduration IN ('12M','P12M', '52W', 'P52W','53W','P53W','365D', 'P365D', 'P364D', 'P371D');

功能

CREATE OR REPLACE FUNCTION populate_period_id_KB()
    RETURNS VOID  AS
$body$
DECLARE
       oaPermID bigint;
       vsID integer;
       periodID integer;
       vsYear integer;

BEGIN  

       FOR vsID, vsYear, oaPermID in 
       SELECT value_scope_id, extract(year from time_stamp), oa_perm_id 
       from value_scopes vs, companies c 
       where vs.company_id = c.company_id 
       and period_id is null 
       and value_scope_id = 98316312
       LOOP
              IF oaPermID IS NOT NULL
              THEN
                     select distinct fs.periodid into periodID 
                     from ccc_financialperiod fp, ccc_financialstatement fs, ccc_fundamental f 
                     where fp.periodid = fs.periodid and fs.fundamentalid = f.fundamentalid and fp.organizationid = f.organizationid 
                     and fs.StatementTypeCode = 'INC' and fs.statementsystemderivedtypecode != 'MP' and fs.UpdateTypeCode = 'ORG' 
                     and fs.periodid IS NOT NULL 
                     and fp.periodtype = 'Year' 
                     and periodfiscalyear = vsYear 
                     and fp.organizationid = oaPermID  
                     and fp.periodduration IN ('12M','P12M', '52W', 'P52W','53W','P53W','365D', 'P365D', 'P364D', 'P371D');               

                     IF periodID IS NULL
                     THEN
                   RAISE INFO 'oaPermID , vsYear, periodID is %,%,%', oaPermID,vsYear,periodID;

                  select distinct periodid into periodID 
                  from ccc_financialperiod fp 
                  where fp.periodtype = 'Year' 
                  and fp.periodfiscalyear = vsYear 
                  and fp.organizationid = oaPermID  
                  and fp.periodduration IN ('12M','P12M', '52W', 'P52W','53W','P53W','365D', 'P365D', 'P364D', 'P371D');

                                  RAISE INFO 'MY TEST - Run - 25 - Now periodID is %', periodID;  

                     ELSE                       
                --Update value_scopes set period_id = periodID where value_scope_id = vsID;
                --INSERT INTO ccc_sdi_info_log VALUES(vsID, oaPermID, 'Updating Value Scope with Period Id ' || periodID  , clock_timestamp(), 'populate_period_id');          

                     END IF;
              END IF;
       END LOOP;

EXCEPTION WHEN OTHERS THEN
       INSERT INTO ccc_sdi_error_log VALUES(null, null, SQLERRM , CURRENT_TIMESTAMP, 'populate_period_id');
       RAISE NOTICE 'ERROR %  %', SQLSTATE, SQLERRM;   
END;
$body$    LANGUAGE plpgsql;

0 个答案:

没有答案