2017-04-20 4 views
0

plpgsql 및 RECORD 유형을 처리하는 데 문제가 있습니다.Postgresql - 레코드 유형 테스트 (필드 또는 null)

반환 된 레코드 유형이 설정되었는지 여부를 간단히 테스트하는 방법을 알고있는 사람이 있습니까?

기본 패턴 :

함수는 RECORD 어디가 해제 될 수있다 반환 (반환없이 레코드에 해당) 또는 단일 레코드로 설정된다.

create or replace function blah() 
returns RECORD as $$ 
declare 
    rec RECORD; 
begin 
    rec := row(null); --Do not want to hear about it not being assigned 
    select *, status_field into rec ... ; 
    if found then 
     raise notice '%', rec.status_field is not null --prints 't' 
    end if 

    if not found then 
     raise notice '%', rec.status_field is not null --Throws Error (I just want it to say 'f' 
    end if; 
end; $$ 

create or replace function callblah() 
returns text as $$ 
declare 
    rtText text; 
    blahRec RECORD; 
begin 
blahRed := blah(...); 

-- what test can I do to determine if record is set or not. 
-- Try: if blah is not null then -- nope always returns false for is null and is not null 
-- Try: if blah.status is not null then -- nope throws error on field not existing 
-- What test condition do I need to add here to simply test if record is either (NOT FOUND) or (FOUND where field will exist). 
... 
end if 
end; $$ 
+0

어떤 Postgres 버전을 사용하고 있습니까? – McNets

답변

0

아 .. RECORD의 null 연산자는 (모든 필드가 null)이며 null이 아닙니다 (모든 필드가 null이 아님). 일부 필드가 null 일 필요가 있으므로

if blah is null then 
    -- No record found 
else 
    if blah.status is not null then 
    -- Record found with field I want 
    end if; 
end if;