I have one select statement that returns a query like this:
SELECT value FROM dummy WHERE condition = 1; ROW | value -----|----- 1 | val1 2 | val2 3 | val3 4 | val4
I have 4 variables
r1, r2, r3, r4
What is the cleanest way to assign those variables the values of my query? Can I use a SELECT INTO statement?
Advertisement
Answer
One method is:
SELECT p_v1 = MAX(CASE WHEN row = 1 THEN value end), p_v2 = MAX(CASE WHEN row = 2 THEN value end), p_v3 = MAX(CASE WHEN row = 3 THEN value end), p_v4 = MAX(CASE WHEN row = 4 THEN value end) FROM dummy WHERE condition = 1;
This is an aggregation query that returns one row. If a particular row is missing, then the value will be NULL
.