Select multiple columns into multiple variables

SqlOraclePlsqlSelect Into

Sql Problem Overview


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

Sql Solutions


Solution 1 - Sql

Your query should be:

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

Solution 2 - Sql

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

Solution 3 - Sql

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';

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHélder GonçalvesView Question on Stackoverflow
Solution 1 - SqlBob Jarvis - Слава УкраїніView Answer on Stackoverflow
Solution 2 - SqlGerardView Answer on Stackoverflow
Solution 3 - SqlNoorView Answer on Stackoverflow