简体   繁体   中英

MySQL Syntax Issue with CONCAT AND LIKE in Stored Procedure

I have a stored procedure to fetch an id. It concat's the last name with first name as "Stone, Cold" and compares with the _fullname passed which when i var_dump gives

String(13) Stone, Cold

It should give an id

IF NOT ISNULL(_fullname) THEN
    SET _fullname   = TRIM(_fullname);
    SET clause = CONCAT( clause , ' AND CONCAT(c.lname, ', ', c.fname) LIKE   CONCAT('%',_fullname,'%')');
END IF;

When i try the same query in MySQL it works perfectly fine but doesn't work in procedure. I'm sure that the problem is syntax in stored procedure.

Try:

DELIMITER //

DROP PROCEDURE IF EXISTS `sp_test`//

CREATE PROCEDURE `sp_test`(IN `_fullname` VARCHAR(20))
BEGIN
  DECLARE `clause` VARCHAR(500) DEFAULT '';
  IF NOT ISNULL(`_fullname`) THEN
    SET `_fullname` := TRIM(`_fullname`);
    SET `clause` := CONCAT(`clause`, ' AND CONCAT(`c`.`lname`, '', '', `c`.`fname`) LIKE ''', '%', `_fullname`, '%''');
  END IF;
  SELECT `clause`;
END//

DELIMITER ;

SQL Fiddle demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM