Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select multiple columns into multiple variables

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
like image 983
Hélder Gonçalves Avatar asked Aug 07 '13 16:08

Hélder Gonçalves


People also ask

How do I select multiple values in a variable in SQL?

SET @variable1 = ( SELECT col1 FROM table1 ); That only allows a single column/variable per statement.

How do I select 3 columns in SQL?

To select multiple columns from a table, simply separate the column names with commas! For example, this query selects two columns, name and birthdate , from the people table: SELECT name, birthdate FROM people; Sometimes, you may want to select all columns from a table.


Video Answer


2 Answers

Your query should be:

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

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

like image 2
Gerard Avatar answered Oct 09 '22 18:10

Gerard