I am trying to determine what is the expected behaviour when SELECT INTO does not return any rows.
From Setting Variables to the Results of a SELECT Statement:
In a Snowflake Scripting block, you can set variables to the values in columns specified in a SELECT clause.
SELECT <column1>, <column2>, ... INTO <variable1>, <variable2>, ... FROM ... WHERE ...;The SELECT statement must return a single row.
Now the examples.
Exactly one row - works as described, variables a,b get new values
DECLARE
a INT DEFAULT 1;
b INT DEFAULT 2;
BEGIN
SELECT x, y
INTO a, b
FROM (SELECT 100 AS x, 200 AS y) s;
RETURN CONCAT(a, ' ', b);
END;
-- Output
-- 100 200
More than one row - also works as expected(error):
DECLARE
a INT DEFAULT 1;
b INT DEFAULT 2;
BEGIN
SELECT x, y
INTO a, b
FROM (SELECT 100 AS x, 200 AS y
UNION ALL
SELECT 100 AS x, 200 AS y
) s;
RETURN CONCAT(a, ' ', b);
END;
A SELECT INTO statement expects exactly 1 returned row, but got 2.
No rows returned - here the output is NULL, the DEFAULT values for a and b are gone
DECLARE
a INT DEFAULT 1;
b INT DEFAULT 2;
BEGIN
SELECT x, y
INTO a, b
FROM (SELECT 100 AS x, 200 AS y) s
WHERE 1=2;
RETURN CONCAT(a, ' ', b);
END;
-- Output
-- null
For comparison similar setup for Oracle db<>fiddle demo and PostgreSQL db<>fiddle demo
Is the assignment of NULL values an expected behaviour or should I rather get an error like("A SELECT INTO statement expects exactly 1 returned row, but got 0.)"?