请参见下面的步骤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
它有三个参数:用户ID、场景ID、组件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
字符串连接有错误吗?