Home > Blockchain >  How to pass the variable along with some default value of column in mysql
How to pass the variable along with some default value of column in mysql

Time:08-19

I have a query file abc.sql which contains,

insert into tbl_name values ('11','name 11', null)

Here, I need to pass the value 11 as a variable name.

Tried as,

Changed the insert statement to,

insert into tbl_name values (@numb,'name @numb', null)

MYSQL > set @numb=11; MYSQL > source abc.sql;

Result showing as,

11| name @numb| null

My requirement is to display,

11| name 11| null

Any help would be appreciated.

CodePudding user response:

MySQL does not replace variables inside string literals, as you've discovered. You need to evaluate them in an expression. In the case you show, you would use the string concatenation function.

insert into tbl_name values (@numb,concat('name ', @numb), null)
  • Related