sanchitkhanna26
sanchitkhanna26

Reputation: 2233

Variable taking another variable's name as its value - MySQL

SET @session = '1'; SET @session = '[@session]';
SELECT @session;

This should print -: [1] but it is printing [@session];

Whats the problem? Thanks for help..

Upvotes: 0

Views: 38

Answers (1)

John Woo
John Woo

Reputation: 263733

The result is as expected. [@session] was the last value that was set to variable @session.

SET @session = '1';           -- <<== first value
SET @session = '[@session]';  -- <<== second value (overrides the first value)
SELECT @session;

i think you mean,

SET @session = '1';           
SET @session = CONCAT('[', @session, ']');  
SELECT @session;

Upvotes: 1

Related Questions