Select multiple columns into multiple variables

152.9k Views Asked by At

How can I do in one select with multiple columns and put each column in a variable?

Something like this:

--code here
V_DATE1 T1.DATE1%TYPE;
V_DATE2 T1.DATE2%TYPE;
V_DATE3 T1.DATE3%TYPE;

SELECT T1.DATE1 INTO V_DATE1, T1.DATE2 INTO V_DATE2, T1.DATE3 INTO V_DATE3
FROM T1
WHERE ID='X';

--code here
3

There are 3 best solutions below

1
On BEST ANSWER

Your query should be:

SELECT T1.DATE1, T1.DATE2, T1.DATE3
INTO V_DATE1, V_DATE2, V_DATE3
FROM T1
WHERE ID='X';
1
On

Select Into is used in Embedded SQL like SQLRPGLE. In SQL session like iSeries Navigator INTO clause is not allowed.

Embedded SQL select col1, col2 into:col1X, :col2X from T1 where ID ='X';

Online session(iSeries Nav or DbVeaver etc)

select col1 as Col1X, col2 as col2X from T1 where ID ='X';

0
On
SELECT
    V_DATE1 = T1.DATE1,
    V_DATE2 = T1.DATE2,
    V_DATE3 = T1.DATE3
FROM T1
WHERE ID='X';

I had problems with Bob's answer but this worked fine