user674669
user674669

Reputation: 12352

How would I assign the result of an sql query to a variable using mysql procedures?

I have a table in mysql with n rows and columns

col1 col2
a     b
c     d

...

Using mysql procedures, how would I get all the rows into 1 varchar variable?

DECLARE var varchar(4096);
SET var = <select * from table>

The value of var should be "a b\nc d\n...."

Thank you.

Upvotes: 0

Views: 33

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269743

You could do:

select var := group_concat(col1, ' ', col2 separator '
')
from table;

Seems a bit strange, but it will do what you want.

EDIT:

You can also do:

select var := group_concat(col1, ' ', col2 separator '\n')
from table;

Upvotes: 3

Related Questions