检查函数参数 Postgres

Checking Function Parameter Postgres

我正在尝试找到最快的方法来检查我的 postgres 函数参数是否为 null 或空。

我当前的功能如下:

CREATE OR REPLATE FUNCTON update_seller_detail(seller_id int, 
seller_name varchar, seller_address varchar, seller_gender varchar)
RETURNS character varying
LANGUAGE plpsql
SECURITY DEFINER
AS $function$

BEGIN

IF seller_id = '' THEN
RAISE NOTICE 'Check Parameter'
return -1
ELSE
IF seller_name = '' THEN
    UPDATE seller SET address=seller_address, gender=seller_gender WHERE id=seller_id;
ELSEIF seller_address = '' THEN 
    UPDATE seller SET name=seller_name, gender=seller_gender WHERE id=seller_id;
ELSE seller_gender= '' THEN 
    UPDATE seller SET name=seller_name, address=seller_address WHERE id=seller_id;
END IF;
END IF;
END;
$function$

有什么方法可以找到哪个参数为空?所以我可以将我的更新设置为不为空的列。谢谢

您可以在具有列引用的更新查询中使用 coalesce(nullif(trim(<column-reference>), '')

CREATE OR REPLACE FUNCTION update_seller_detail(
        seller_id int,
        seller_name varchar, 
        seller_address varchar, 
        seller_gender varchar
    )
    RETURNS character varying
    LANGUAGE plpgsql
    SECURITY DEFINER
AS
$function$

BEGIN

    IF seller_id ISNULL THEN
        RAISE NOTICE 'Check Parameter';
        return -1;
    ELSE

        UPDATE public.seller s
        SET name    = coalesce(nullif(trim(seller_name), ''), s.name),
            address = coalesce(nullif(trim(seller_address), ''), s.address),
            gender  = coalesce(nullif(trim(seller_gender), ''), s.gender)
        WHERE id = seller_id;

        return 1;
    END IF;
END;
$function$