无法将 plpgsql 中的自定义类型对象设置为空。相反,该对象的字段变为空
Cannot set custom type object in plpgsql to null. Instead fields of that object becomes null
我正在尝试将自定义类型对象的 null 附加到 null。但只有该对象内部的字段变为空,而不是对象本身。
我已经尝试使用 FOUND 标志来确定是否有任何行分配给了我的对象。如果 found 为 false,则自定义类型对象设置为 null。但是当我打印对象时,在 pgadmin 控制台中它看起来像 (,)。
Select id,provider_id,beneficiary_name,beneficiary_branch,beneficiary_account_number,swift_code,
payment_terms,currency,bank_name,payment_entity,payment_type,invoice_due_date_type,last_updated_time
INTO provider_payment_method_result
from provider_info.provider_payment_method where provider_id = provider_id_param;
IF NOT FOUND
THEN provider_payment_method_result := NULL::provider_info_api.payment_method;
END IF;
raise notice 'found : %', found;
raise notice 'payment_method % ', provider_payment_method_result;
return provider_payment_method_result;
我希望
NOTICE: found : f NOTICE: payment_method null
实际结果
NOTICE: found : f NOTICE: payment_method (,,,,,,,,,,,,)
这取决于 Postgre 的版本SQL。如果 PostgreSQL 早于 10,那么您所描述的行为是预期的。在所有字段中具有 NULL 的行(或复合值)实际上是 NULL。在这些版本上,复合值每次都是按值传递(从SQL的角度来看),不能表示为一个值(尽管这个值是NULL)。
在 PostgreSQL 10 及更高版本上,此行为已更改。
CREATE TYPE xy AS (x int, y int);
DO $$
DECLARE _xy xy;
BEGIN
_xy := (NULL, NULL);
RAISE NOTICE '% %', _xy, _xy IS NULL;
END;
$$;
如果您有较旧的 Postgres,并且您只会看到 NULL,请改用 RECORD
类型。
我正在尝试将自定义类型对象的 null 附加到 null。但只有该对象内部的字段变为空,而不是对象本身。
我已经尝试使用 FOUND 标志来确定是否有任何行分配给了我的对象。如果 found 为 false,则自定义类型对象设置为 null。但是当我打印对象时,在 pgadmin 控制台中它看起来像 (,)。
Select id,provider_id,beneficiary_name,beneficiary_branch,beneficiary_account_number,swift_code,
payment_terms,currency,bank_name,payment_entity,payment_type,invoice_due_date_type,last_updated_time
INTO provider_payment_method_result
from provider_info.provider_payment_method where provider_id = provider_id_param;
IF NOT FOUND
THEN provider_payment_method_result := NULL::provider_info_api.payment_method;
END IF;
raise notice 'found : %', found;
raise notice 'payment_method % ', provider_payment_method_result;
return provider_payment_method_result;
我希望
NOTICE: found : f NOTICE: payment_method null
实际结果
NOTICE: found : f NOTICE: payment_method (,,,,,,,,,,,,)
这取决于 Postgre 的版本SQL。如果 PostgreSQL 早于 10,那么您所描述的行为是预期的。在所有字段中具有 NULL 的行(或复合值)实际上是 NULL。在这些版本上,复合值每次都是按值传递(从SQL的角度来看),不能表示为一个值(尽管这个值是NULL)。
在 PostgreSQL 10 及更高版本上,此行为已更改。
CREATE TYPE xy AS (x int, y int);
DO $$
DECLARE _xy xy;
BEGIN
_xy := (NULL, NULL);
RAISE NOTICE '% %', _xy, _xy IS NULL;
END;
$$;
如果您有较旧的 Postgres,并且您只会看到 NULL,请改用 RECORD
类型。