简体   繁体   中英

Can not find the Syntax 1064 Error in my store procedure using MySQL

I am creating a SP so that I can choose the field of a Table im interested in and a second parameter which is "ASC" or "DESC" and de SP will order the table by that field and also will order it ASC or DESC depending on what the second parameter is. But I can not find the syntax mistake which is a regular 1064 mistake! I am running this in MySQL workbench

DELIMITER $$
CREATE PROCEDURE `PROCEDUREUNO` (IN field VARCHAR(100),IN ordenamiento CHAR(5))
BEGIN
IF field <> ‘’ THEN
SET @orden = concat(‘ORDER BY ’, field,ordenamiento);
ELSE
SET @orden = ‘’;
END IF;
SET @clausula = concat(‘SELECT * FROM Contrataciones ’, @orden);
PREPARE runSQL FROM @clausula;
EXECUTE runSQL;
DEALLOCATE PREPARE runSQL;
END

END $$

You have two errors in your Stored Procedure declaration:

  • don't use rounded quotes '' , use instead simple quotes ''
  • you have two END keywords at the end of your declaration
DELIMITER $$
CREATE PROCEDURE `PROCEDUREUNO` (IN field VARCHAR(100),IN ordenamiento CHAR(5))
BEGIN
    IF field <> '' THEN
        SET @orden = CONCAT('ORDER BY ', field,ordenamiento);
    ELSE
        SET @orden = '';
    END IF;

    SET @clausula = CONCAT('SELECT * FROM Contrataciones', @orden);

    PREPARE runSQL FROM @clausula;
    EXECUTE runSQL;
    DEALLOCATE PREPARE runSQL;
END $$

Check the demo here .

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