U-Dev
U-Dev

Reputation: 1387

Use CONCAT inside CONCAT in mysql

I have a stored procedure and a part of it is:

SET @TableAlias = " ls_flock AS _flk ";
SET @Select = CONCAT(
    " _flk.Id as Id, _flk.Code as Code, ",
    CONCAT(" _em.FirstName", " ", "_em.LastName "), -- here I want to CONCAT 2 columns as one field.
    " as FlockManager"
  );
SET @Join = " JOIN employee as _em ON _flk.EmployeeId = _em.Id ";

I am unable to CONCAT two columns as a field with a CONCAT function.

And if I remove following line, code works fine:

CONCAT(" _em.FirstName", " ", "_em.LastName "), -- here I want to CONCAT 2 columns as one field.
    " as FlockManager"

I'm getting this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.LastName as FlockManager FROM ls_flock AS _flk JOIN employee as _em ON _flk.' at line 1

Upvotes: 0

Views: 506

Answers (1)

GMB
GMB

Reputation: 222672

I suspect that you want to generate a query string that uses CONCAT() - because I see that the table alias _em is declared further in the @Join variable. For this, you would need to move the function inside the query string.

SET @Select = CONCAT(
    " _flk.Id as Id, ",
    "_flk.Code as Code, ",
    "CONCAT(_em.FirstName, "" "", _em.LastName) s FlockManager"
;

And then, you just don't need the outer CONCAT():

SET @Select = 
    " _flk.Id as Id, _flk.Code as Code, CONCAT(_em.FirstName, "" "", _em.LastName) as FlockManager";

Upvotes: 3

Related Questions