gpt4 book ai didi

mysql - 调用存储过程时出错

转载 作者:行者123 更新时间:2023-11-30 01:21:30 24 4
gpt4 key购买 nike

当我调用我的过程时,它返回一个错误:

1064 - 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 'NULL' at line 1

如何修复它,我的程序代码是否有错误?

这是我的存储过程:

DELIMITER $$
DROP PROCEDURE IF EXISTS `gamedb`.`ALIAS#SEARCH`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ALIAS#SEARCH`(
in section_id varchar(255),
in category_id varchar(255),
in content_id varchar(255)
)

BEGIN
declare q varchar(4000);
set @q = 'SELECT * FROM tbl_alias WHERE ALIAS_ACTIVE_STATUS=1';

IF section_id IS NOT NULL THEN
set @q = concat(q,' AND ALIAS_SECTION_ID = ',section_id);
END IF;

IF category_id IS NOT NULL THEN
set @q = concat(q,' AND ALIAS_CATEGORY_ID = ',category_id);
END IF;

IF content_id IS NOT NULL THEN
set @q = concat(q,' AND ALIAS_CONTENT_ID = ',content_id);
END IF;

set @q= concat(q,' ORDER BY ALIAS_ID DESC');
prepare stmt from @q;

EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END$$
DELIMITER ;

最佳答案

有几个问题

  1. 由于您只能从用户变量中PREPARE,因此不需要局部变量q
  2. 使用 CONCAT() 时,@q 变量缺少 @

    set @q = concat(q,' AND ALIAS_SECTION_ID = ',section_id);
    ^
  3. 由于您的 IN 参数定义为 VARCHAR,因此您最好在构建查询时引用它们的值

话虽这么说,你的 SP 可能看起来像这样

DELIMITER $$
CREATE PROCEDURE `ALIAS#SEARCH`(
in section_id varchar(255),
in category_id varchar(255),
in content_id varchar(255)
)

BEGIN
SET @q = 'SELECT * FROM tbl_alias WHERE ALIAS_ACTIVE_STATUS = 1';

IF section_id IS NOT NULL THEN
SET @q = CONCAT(@q, ' AND ALIAS_SECTION_ID = ''', section_id, '''');
END IF;

IF category_id IS NOT NULL THEN
SET @q = CONCAT(@q, ' AND ALIAS_CATEGORY_ID = ''', category_id, '''');
END IF;

IF content_id IS NOT NULL THEN
SET @q = CONCAT(@q, ' AND ALIAS_CONTENT_ID = ''', content_id, '''');
END IF;

SET @q = CONCAT(@q, ' ORDER BY ALIAS_ID DESC');

PREPARE stmt FROM @q;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END$$
DELIMITER ;

关于mysql - 调用存储过程时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18480141/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com