在 Snowflake 函数的 where 子句中将 NULL 作为参数传递
Passing NULL as parameter in a where clause for a Snowflake function
我有一个带参数的函数。如果我在 where 子句中将 null 作为参数传递,我希望它返回所有结果,例如
CREATE OR REPLACE FUNCTION ABC(" CUSTCODE" VARCHAR)
RETURNS TABLE ("CUSTOMER_CODE" VARCHAR, CUSTOMERNAME VARCHAR)
LANGUAGE SQL
AS '
SELECT CUSTOMER_CODE, CUSTOMERNAME FROM DIM_CUSTOMER
WHERE CUSTOMER_CODE = CUSTCODE
';
因此,如果 CUSTCODE 为 NULL
然后带回所有 CUSTOMER_CODE
几乎就像忽略 where 子句
有人能给我指出正确的方向吗?
提前致谢!
里昂
根据 Dai 的评论:
CREATE OR REPLACE TABLE DIM_CUSTOMER(CUSTOMER_CODE text, CUSTOMERNAME text);
INSERT INTO DIM_CUSTOMER VALUES ('one', 'customer one'),('two', 'customer two');
CREATE OR REPLACE FUNCTION ABC(CUSTCODE VARCHAR)
RETURNS TABLE (CUSTOMER_CODE VARCHAR, CUSTOMERNAME VARCHAR)
LANGUAGE SQL
AS '
SELECT CUSTOMER_CODE, CUSTOMERNAME FROM DIM_CUSTOMER
WHERE CUSTCODE IS NULL OR CUSTOMER_CODE = CUSTCODE
';
SELECT * FROM TABLE(ABC('one'));
给出:
CUSTOMER_CODE
CUSTOMERNAME
one
customer one
SELECT * FROM TABLE(ABC(null::text));
给出:
CUSTOMER_CODE
CUSTOMERNAME
one
customer one
two
customer two
我有一个带参数的函数。如果我在 where 子句中将 null 作为参数传递,我希望它返回所有结果,例如
CREATE OR REPLACE FUNCTION ABC(" CUSTCODE" VARCHAR)
RETURNS TABLE ("CUSTOMER_CODE" VARCHAR, CUSTOMERNAME VARCHAR)
LANGUAGE SQL
AS '
SELECT CUSTOMER_CODE, CUSTOMERNAME FROM DIM_CUSTOMER
WHERE CUSTOMER_CODE = CUSTCODE
';
因此,如果 CUSTCODE 为 NULL 然后带回所有 CUSTOMER_CODE 几乎就像忽略 where 子句
有人能给我指出正确的方向吗?
提前致谢!
里昂
根据 Dai 的评论:
CREATE OR REPLACE TABLE DIM_CUSTOMER(CUSTOMER_CODE text, CUSTOMERNAME text);
INSERT INTO DIM_CUSTOMER VALUES ('one', 'customer one'),('two', 'customer two');
CREATE OR REPLACE FUNCTION ABC(CUSTCODE VARCHAR)
RETURNS TABLE (CUSTOMER_CODE VARCHAR, CUSTOMERNAME VARCHAR)
LANGUAGE SQL
AS '
SELECT CUSTOMER_CODE, CUSTOMERNAME FROM DIM_CUSTOMER
WHERE CUSTCODE IS NULL OR CUSTOMER_CODE = CUSTCODE
';
SELECT * FROM TABLE(ABC('one'));
给出:
CUSTOMER_CODE | CUSTOMERNAME |
---|---|
one | customer one |
SELECT * FROM TABLE(ABC(null::text));
给出:
CUSTOMER_CODE | CUSTOMERNAME |
---|---|
one | customer one |
two | customer two |