我正在尝试使用mysql数据库中的存储过程构建动态查询,但仍然看到此错误。为什么呢?
DROP PROCEDURE IF EXISTS mp_test;
CREATE PROCEDURE mp_test(
IN PrStatus VARCHAR(255),
IN PrStatusCode VARCHAR(255),
IN PrProviderId VARCHAR(255),
IN PrRow VARCHAR(255))
BEGIN
SET @Condition = '';
SET @Query = '
SELECT
bitauth_users.fullname,
tbservices.ServiceNameAR,
tbservices.ServiceNameEN,
tbservices.ServiceId,
globrx.ProviderId,
globrx.RequestDesc,
TbServicesRequestsLog.status
FROM tbservices, Tbservicesrequests globrx,
TbServicesRequestsLog, bitauth_users
WHERE globrx.ProviderId = 'Prproviderid' ';
IF PrStatus != '' THEN
SET @Condition = CONCAT(' AND globrx.RequestId = TbServicesRequestsLog.RequestId');
SET @Condition = CONCAT(' AND globrx.ServiceId = tbservices.ServiceId');
SET @Condition = CONCAT(' AND bitauth_users.user_id = globrx.ReceiverId');
SET @Condition = CONCAT(' AND globrx.RequestId = TbServicesRequestsLog.RequestId');
SET @Condition = CONCAT('AND (SELECT status FROM TbServicesRequestsLog,Tbservicesrequests AS subrx
WHERE subrx.RequestId = TbServicesRequestsLog.RequestId
AND subrx.ServiceId = globrx.ServiceId
ORDER BY RequestLogId DESC LIMIT 1)"', status_code, '"');
SET @Condition = CONCAT('GROUP BY TbServices.ServiceId ORDER BY TbServices.ServiceId ASC LIMIT 'PrRow',10')
END IF;
SET @Query = CONCAT(@Query, @Condition);
PREPARE stmt FROM @Query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
答案 0 :(得分:1)
我不知道你的存储过程是什么......但固定版本是: 它有几个错误:
必须将分隔符设置为默认值,因为正文将包含; ,第一行我将分隔符更改为$$
连接不是自动的:
WHERE globrx.ProviderId = 'Prproviderid' ';
必须更改为
concat('...
WHERE globrx.ProviderId = ',Prproviderid,' ');
@condition是一个累积变量(我认为):
SET @Condition = CONCAT(@Condition,
结果是:
DELIMITER $$
DROP PROCEDURE IF EXISTS mp_test$$
CREATE PROCEDURE mp_test(
IN PrStatus VARCHAR(255),
IN PrStatusCode VARCHAR(255),
IN PrProviderId VARCHAR(255),
IN PrRow VARCHAR(255))
BEGIN
SET @Condition = '';
SET @Query = concat('
SELECT
bitauth_users.fullname,
tbservices.ServiceNameAR,
tbservices.ServiceNameEN,
tbservices.ServiceId,
globrx.ProviderId,
globrx.RequestDesc,
TbServicesRequestsLog.status
FROM tbservices, Tbservicesrequests globrx,
TbServicesRequestsLog, bitauth_users
WHERE globrx.ProviderId = ',Prproviderid,' ');
SET @Condition = '';
IF PrStatus != '' THEN
SET @Condition = CONCAT(@Condition,' AND globrx.RequestId = TbServicesRequestsLog.RequestId');
SET @Condition = CONCAT(@Condition,' AND globrx.ServiceId = tbservices.ServiceId');
SET @Condition = CONCAT(@Condition,' AND bitauth_users.user_id = globrx.ReceiverId');
SET @Condition = CONCAT(@Condition,' AND globrx.RequestId = TbServicesRequestsLog.RequestId');
SET @Condition = CONCAT(@Condition,' AND (SELECT status FROM TbServicesRequestsLog,Tbservicesrequests AS subrx
WHERE subrx.RequestId = TbServicesRequestsLog.RequestId
AND subrx.ServiceId = globrx.ServiceId
ORDER BY RequestLogId DESC LIMIT 1) "', status_code, '"');
SET @Condition = CONCAT(@Condition,' GROUP BY TbServices.ServiceId ORDER BY TbServices.ServiceId ASC LIMIT ',PrRow,',10');
END IF;
SET @Query = CONCAT(@Query, @Condition);
PREPARE stmt FROM @Query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END $$