我有一个函数,它具有使用其中一个输入参数测试空值的条件。如果输入参数不为null,则执行sql statment,如果其null然后fit则执行最后一个SQL语句。
例如
当我执行该功能时
SELECT
CAST(MD5(iml.udf_Extract_Shipment_HASHBYTESVALUES('SG160211304', '+00000', ''))
我希望执行第二个查询并获得结果集。
CREATE OR REPLACE FUNCTION iml.udf_extract_shipment_hashbytesvalues ( varchar, varchar,varchar
)
RETURNS TABLE (
concatenatedfield text
) AS
$body$
DECLARE
v_custpo ALIAS FOR $1;
v_shipqty ALIAS FOR $2;
V_mfgpart ALIAS FOR $3;
BEGIN
IF V_mfgpart IS NOT NULL OR V_mfgpart <> ' ' THEN
RETURN QUERY
SELECT
CAST(CASE WHEN order_no IS NULL THEN '1' ELSE order_no END AS VARCHAR(200)) ||
CAST(CASE WHEN customer_po IS NULL THEN '1' ELSE customer_po END AS VARCHAR(200)) ||
CAST(CASE WHEN cust_no IS NULL THEN '1' ELSE cust_no END AS VARCHAR(200)) ||
CAST(CASE WHEN customer_name IS NULL THEN '1' ELSE customer_name END AS VARCHAR(200)) ||
CAST(CASE WHEN vendor_no IS NULL THEN '1' ELSE vendor_no END AS VARCHAR(200)) ||
CAST(CASE WHEN im_part_no IS NULL THEN '1' ELSE im_part_no END AS VARCHAR(200)) ||
CAST(CASE WHEN order_branch IS NULL THEN 1 ELSE order_branch END AS VARCHAR(200)) ||
CAST(CASE WHEN ship_frm_branch IS NULL THEN 1 ELSE ship_frm_branch END AS VARCHAR(200)) ||
CAST(CASE WHEN ship_to_branch IS NULL THEN 1 ELSE ship_to_branch END AS VARCHAR(200)) ||
CAST(CASE WHEN lent_order IS NULL THEN '1' ELSE lent_order END AS VARCHAR(200)) ||
CAST(CASE WHEN order_type IS NULL THEN '1' ELSE order_type END AS VARCHAR(200)) ||
CAST(CASE WHEN ship_qty IS NULL THEN '1' ELSE ship_qty END AS VARCHAR(200)) ||
CAST(CASE WHEN inventory_type IS NULL THEN '1' ELSE inventory_type END AS VARCHAR(200)) ||
CAST(CASE WHEN credit_code IS NULL THEN '1' ELSE credit_code END AS VARCHAR(200)) ||
CAST(CASE WHEN tax_amt IS NULL THEN 1 ELSE tax_amt END AS VARCHAR(200)) ||
CAST(CASE WHEN freight_in IS NULL THEN '1' ELSE freight_in END AS VARCHAR(200)) ||
CAST(CASE WHEN freight_out IS NULL THEN '1' ELSE freight_out END AS VARCHAR(200)) ||
CAST(CASE WHEN mfg_part IS NULL THEN '1' ELSE mfg_part END AS VARCHAR(200)) ||
CAST(CASE WHEN description IS NULL THEN '1' ELSE description END AS VARCHAR(200))
FROM
iml.shipments
WHERE customer_po = v_custpo AND ship_qty = v_shipqty AND mfg_part = V_mfgpart;
ELSEIF V_mfgpart = '' OR V_mfgpart IS NULL THEN
RETURN QUERY
SELECT
CAST(CASE WHEN order_no IS NULL THEN '1' ELSE order_no END AS VARCHAR(200)) ||
CAST(CASE WHEN customer_po IS NULL THEN '1' ELSE customer_po END AS VARCHAR(200)) ||
CAST(CASE WHEN cust_no IS NULL THEN '1' ELSE cust_no END AS VARCHAR(200)) ||
CAST(CASE WHEN customer_name IS NULL THEN '1' ELSE customer_name END AS VARCHAR(200)) ||
CAST(CASE WHEN vendor_no IS NULL THEN '1' ELSE vendor_no END AS VARCHAR(200)) ||
CAST(CASE WHEN im_part_no IS NULL THEN '1' ELSE im_part_no END AS VARCHAR(200)) ||
CAST(CASE WHEN order_branch IS NULL THEN 1 ELSE order_branch END AS VARCHAR(200)) ||
CAST(CASE WHEN ship_frm_branch IS NULL THEN 1 ELSE ship_frm_branch END AS VARCHAR(200)) ||
CAST(CASE WHEN ship_to_branch IS NULL THEN 1 ELSE ship_to_branch END AS VARCHAR(200)) ||
CAST(CASE WHEN lent_order IS NULL THEN '1' ELSE lent_order END AS VARCHAR(200)) ||
CAST(CASE WHEN order_type IS NULL THEN '1' ELSE order_type END AS VARCHAR(200)) ||
CAST(CASE WHEN ship_qty IS NULL THEN '1' ELSE ship_qty END AS VARCHAR(200)) ||
CAST(CASE WHEN inventory_type IS NULL THEN '1' ELSE inventory_type END AS VARCHAR(200)) ||
CAST(CASE WHEN credit_code IS NULL THEN '1' ELSE credit_code END AS VARCHAR(200)) ||
CAST(CASE WHEN tax_amt IS NULL THEN 1 ELSE tax_amt END AS VARCHAR(200)) ||
CAST(CASE WHEN freight_in IS NULL THEN '1' ELSE freight_in END AS VARCHAR(200)) ||
CAST(CASE WHEN freight_out IS NULL THEN '1' ELSE freight_out END AS VARCHAR(200)) ||
CAST(CASE WHEN mfg_part IS NULL THEN '1' ELSE mfg_part END AS VARCHAR(200)) ||
CAST(CASE WHEN description IS NULL THEN '1' ELSE description END AS VARCHAR(200))
FROM
iml.shipments
WHERE customer_po = v_custpo AND ship_qty = v_shipqty AND mfg_part IS NULL OR mfg_part = V_mfgpart
;
END IF;
END;
$body$
LANGUAGE 'plpgsql'
VOLATILE
CALLED ON NULL INPUT
SECURITY INVOKER
COST 100 ROWS 1000;
答案 0 :(得分:1)
您传递的空字符串(非空)为mfgpart。所以第一个查询将被执行。仅当您将null作为mfgpart传递时,才会执行第二个查询。如果要在传递空字符串时执行第二个查询,请更改
IF V_mfgpart IS NOT NULL OR V_mfgpart <> ' ' THEN
到
IF V_mfgpart IS NOT NULL and V_mfgpart <> '' THEN
注意空格与空字符串
我怀疑你想要这个
create or replace function iml.udf_extract_shipment_hashbytesvalues (
varchar v_custpo,
varchar v_shipqty,
varchar v_mfgpart
)
returns table (
concatenatedfield text
) as
$body$
select
cast(case when order_no is null then '1' else order_no end as varchar(200)) ||
cast(case when customer_po is null then '1' else customer_po end as varchar(200)) ||
cast(case when cust_no is null then '1' else cust_no end as varchar(200)) ||
cast(case when customer_name is null then '1' else customer_name end as varchar(200)) ||
cast(case when vendor_no is null then '1' else vendor_no end as varchar(200)) ||
cast(case when im_part_no is null then '1' else im_part_no end as varchar(200)) ||
cast(case when order_branch is null then 1 else order_branch end as varchar(200)) ||
cast(case when ship_frm_branch is null then 1 else ship_frm_branch end as varchar(200)) ||
cast(case when ship_to_branch is null then 1 else ship_to_branch end as varchar(200)) ||
cast(case when lent_order is null then '1' else lent_order end as varchar(200)) ||
cast(case when order_type is null then '1' else order_type end as varchar(200)) ||
cast(case when ship_qty is null then '1' else ship_qty end as varchar(200)) ||
cast(case when inventory_type is null then '1' else inventory_type end as varchar(200)) ||
cast(case when credit_code is null then '1' else credit_code end as varchar(200)) ||
cast(case when tax_amt is null then 1 else tax_amt end as varchar(200)) ||
cast(case when freight_in is null then '1' else freight_in end as varchar(200)) ||
cast(case when freight_out is null then '1' else freight_out end as varchar(200)) ||
cast(case when mfg_part is null then '1' else mfg_part end as varchar(200)) ||
cast(case when description is null then '1' else description end as varchar(200))
from
iml.shipments
where
customer_po = v_custpo and
ship_qty = v_shipqty and
(v_mfg_part is null or v_mfg_part = '' or mfg_part = v_mfgpart)
;
end;
$body$
language sql
volatile
called on null input
security invoker
cost 100 rows 1000;