gpt4 book ai didi

sql - MySQL - 存储过程中的动态 SQL

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

请参阅下面的过程 InsertHelpQuestion2。

DROP PROCEDURE InsertHelpQuestion2;
CREATE PROCEDURE InsertHelpQuestion2(user_id char(50),scenerio_id char(50),component_id char(50))
BEGIN
DECLARE uu_id char(50);
SELECT BinToUUID(NewUUID()) INTO uu_id from helpquestion LIMIT 1;
SET @fields_part = 'INSERT INTO helpquestion(HelpQuestionKey,UserKey';
SET @values_part = CONCAT(' VALUES(UUIDToBin(\"', uu_id, '\"), UUIDToBin(\"', user_id, '\")');
IF (scenerio_id) THEN
SET @fields_part = CONCAT(@fields_part, ', ScenarioKey');
SET @values_part = CONCAT(@values_part, ', UUIDToBin("', scenerio_id, '")');
END IF;
IF (component_id) THEN
SET @fields_part = CONCAT(@fields_part, ', ComponentKey');
SET @values_part = CONCAT(@values_part, ', UUIDToBin("', component_id, '")');
END IF;
SET @query_full = CONCAT(@fields_part , @values_part, ';');
PREPARE STMT FROM @query_full;
EXECUTE STMT;
END

它有3个参数user_id, scenerio_id, component_id 都是外键。

NewUUID()、UUIDToBin 是存储函数。

由于字段有外部引用,我需要在过程中编写动态 sql,以便下面的调用工作

CALL InsertHelpQuestion2('F70724DC-AC0D-102D-9C16-00163EEDFCFC', '0F69476A-ABF2-102D-9C16-00163EEDFCFC', '06FFEE04-1FD9-11DF-9B60-001F16F664A9');

CALL InsertHelpQuestion2('F70724DC-AC0D-102D-9C16-00163EEDFCFC', '0F69476A-ABF2-102D-9C16-00163EEDFCFC', '');

CALL InsertHelpQuestion2('F70724DC-AC0D-102D-9C16-00163EEDFCFC', '', '');

但是第一次调用

CALL InsertHelpQuestion2('F70724DC-AC0D-102D-9C16-00163EEDFCFC', '0F69476A-ABF2-102D-9C16-00163EEDFCFC', '06FFEE04-1FD9-11DF-9B60-001F16F664A9');

出现以下错误

[Err] 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 'VALUES(UUIDToBin("69490F3A-6FD3-11DF-964F-F4CE462E9D80"), UUIDToBin("F70724DC-AC' at line 1

字符串连接有错误吗?

最佳答案

看起来您可能需要一个右括号。

SET @query_full = CONCAT(@fields_part , @values_part, ');');

关于sql - MySQL - 存储过程中的动态 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2974120/

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