使用IF语句创建SQL存储过程

时间:2022-09-28 22:51:02

I'm trying to create a stored procedure in PHPmyadmin but it doesn't seem to like my IF statement. Can anybody please help me identify what i've done wrong:

我正在尝试在PHPmyadmin中创建一个存储过程,但它似乎不喜欢我的IF语句。任何人都可以帮我确定我做错了什么:

SQL:

CREATE PROCEDURE get_products_in_category(IN inCategoryID INT, 
IN inShortDescriptionLength INT, IN inProductsPerPage INT, IN inStartItem INT)

BEGIN
"SELECT p.productID, p.name, IF(LENGTH(p.description) <= ?, p.description, 
 CONCAT(LEFT(p.description, ?), '...')) AS description, p.price, p.discounted_price, 
 p.thumbnail FROM tblProducts p

INNER JOIN tblProdCat pc
ON pc.productID = p.productID
WHERE tblProdCat.categoryID = ?
ORDER BY p.display DESC
LIMIT ?, ?";

SET @p1 = inShortDescriptionLength;
SET @p2 = inShortDescriptionLength;
SET @p3 = inCategoryID;
SET @p4 = inStartItem;
SET @p5 = inProductsPerPage;

END$$

I'm getting the following error my:

我收到以下错误:

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 '"SELECT p.productID, p.name, IF(LENGTH(p.description) <= ?, p.description, CONCA' at line 3

您的SQL语法有错误;查看与您的MySQL服务器版本相对应的手册,以便在第3行选择“p.productID,p.name,IF(LENGTH(p.description)<=?,p.description,CONCA''附近使用正确的语法)

1 个解决方案

#1


0  

I think you want to execute Dynamic SQL,

我想你想要执行动态SQL,

DELIMITER $$
CREATE PROCEDURE get_products_in_category
(
    IN inCategoryID INT, 
    IN inShortDescriptionLength INT, 
    IN inProductsPerPage INT, 
    IN inStartItem INT
)
BEGIN

    SET @sql = CONCAT('SELECT   p.productID, 
                                p.name, 
                                IF(CHAR_LENGTH(p.description) <= ?, p.description, CONCAT(LEFT(p.description, ?), ''...'')) AS description, 
                                p.price, 
                                p.discounted_price, 
                                p.thumbnail 
                        FROM    tblProducts p
                                INNER JOIN tblProdCat pc
                                    ON pc.productID = p.productID
                        WHERE   pc.categoryID = ?
                        ORDER   BY p.display DESC
                        LIMIT   ',inStartItem, ', ',inProductsPerPage);



    PREPARE stmt1 FROM @sql;
    SET @p1 = inShortDescriptionLength;
    SET @p2 = inShortDescriptionLength;
    SET @p3 = inCategoryID;

    EXECUTE stmt1 USING @p1, @p2, @p3;
    DEALLOCATE PREPARE stmt1;
END $$
DELIMITER ;

#1


0  

I think you want to execute Dynamic SQL,

我想你想要执行动态SQL,

DELIMITER $$
CREATE PROCEDURE get_products_in_category
(
    IN inCategoryID INT, 
    IN inShortDescriptionLength INT, 
    IN inProductsPerPage INT, 
    IN inStartItem INT
)
BEGIN

    SET @sql = CONCAT('SELECT   p.productID, 
                                p.name, 
                                IF(CHAR_LENGTH(p.description) <= ?, p.description, CONCAT(LEFT(p.description, ?), ''...'')) AS description, 
                                p.price, 
                                p.discounted_price, 
                                p.thumbnail 
                        FROM    tblProducts p
                                INNER JOIN tblProdCat pc
                                    ON pc.productID = p.productID
                        WHERE   pc.categoryID = ?
                        ORDER   BY p.display DESC
                        LIMIT   ',inStartItem, ', ',inProductsPerPage);



    PREPARE stmt1 FROM @sql;
    SET @p1 = inShortDescriptionLength;
    SET @p2 = inShortDescriptionLength;
    SET @p3 = inCategoryID;

    EXECUTE stmt1 USING @p1, @p2, @p3;
    DEALLOCATE PREPARE stmt1;
END $$
DELIMITER ;