我为报告编写了一个存储过程,它与mysql一起正常工作但是当我从PHP调用它时,我收到一个错误:
SQLSTATE [42000]:语法错误或访问冲突:1064
您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第1行的“NULL”附近使用正确的语法
PHP:
$crm_reports = "CALL CRM_Report_Total_Prize_Redemption('".$CrmreportsFromDate."','".$CrmreportsToDate."',$CampaignID);";
$total_prize = Yii::app()->db->createCommand($crm_reports)->queryAll();
这是我的存储过程
CREATE DEFINER=`esp_reward`@`%` PROCEDURE `CRM_Report_Total_Prize_Redemption`(
IN v_StartDate DATETIME,
IN v_EndDate DATETIME,
IN v_CampaignID INT
)
BEGIN
DROP TEMPORARY TABLE IF EXISTS temp_prize_redemption;
CREATE TEMPORARY TABLE temp_prize_redemption (
ItemID INT(10)
,Qty INT(11)
,BenefitName VARCHAR(500)
);
/************************************* Item Data ****************************/
SET @v_SelectPhysicalItemForPickWin = CONCAT(" SELECT cpw.ItemID AS PrizeID,cpw.Benefit AS BenefitName
,COUNT(DISTINCT PickWinWinnerID) AS Qty
,cpw.VoucherID,cpw.VoucherAmount,cpc.CampaignName
FROM crm_promotion_campaign AS cpc
INNER JOIN crm_receipt AS cr ON cr.CampaignID = cpc.CampaignID
INNER JOIN crm_pickwin_winner AS cpw ON cpw.PickWinID = cr.PickWinID
WHERE 1 AND cpc.CampaignID = ",v_CampaignID," AND cpw.BenefitType = '1'
GROUP BY PrizeID ");
SET @v_SelectPhysicalItemForPromotion = CONCAT(" SELECT crb.ItemID AS PrizeID,crb.BenefitName
,COUNT(DISTINCT ID) AS Qty,crb.VoucherID,crb.VoucherAmount,cpc.CampaignName
FROM crm_promotion_campaign AS cpc
INNER JOIN crm_receipt AS cr ON cr.CampaignID = cpc.CampaignID AND cr.TypeID = '1'
INNER JOIN crm_receipt_benefit AS crb ON crb.PromotionID = cr.PromotionID
WHERE 1 AND cpc.CampaignID = ",v_CampaignID," AND crb.BenefitTypeID = '1'
GROUP BY PrizeID ");
SET @ItemData = CONCAT(" SELECT PrizeID,SUM(Qty) AS Total,BenefitName FROM ( ",@v_SelectPhysicalItemForPickWin," UNION ",@v_SelectPhysicalItem, " ) AS ItemData GROUP BY PrizeID ");
SET @v_data = CONCAT("INSERT INTO temp_prize_redemption", @ItemData);
PREPARE s1 FROM @v_data;
EXECUTE s1;
DEALLOCATE PREPARE s1;
/************************************* e-Voucher Data ****************************/
SET @v_SelecteVoucherForPickWin = CONCAT(" SELECT cpw.VoucherID AS PrizeID,cpw.Benefit AS BenefitName
,COUNT(DISTINCT VoucherID) AS Qty
,cpw.VoucherID,cpw.VoucherAmount,cpc.CampaignName
FROM crm_promotion_campaign AS cpc
INNER JOIN crm_receipt AS cr ON cr.CampaignID = cpc.CampaignID
INNER JOIN crm_pickwin_winner AS cpw ON cpw.PickWinID = cr.PickWinID
WHERE 1 AND cpc.CampaignID = ",v_CampaignID," AND cpw.BenefitType = '3'
GROUP BY PrizeID ");
SET @v_SelecteVoucherForPromotion = CONCAT(" SELECT crb.VoucherID AS PrizeID,crb.BenefitName
,COUNT(DISTINCT VoucherID) AS Qty,crb.VoucherID,crb.VoucherAmount,cpc.CampaignName
FROM crm_promotion_campaign AS cpc
INNER JOIN crm_receipt AS cr ON cr.CampaignID = cpc.CampaignID AND cr.TypeID = '1'
INNER JOIN crm_receipt_benefit AS crb ON crb.PromotionID = cr.PromotionID
WHERE 1 AND cpc.CampaignID = ",v_CampaignID," AND crb.BenefitTypeID = '3'
GROUP BY PrizeID ");
SET @eVoucherData = CONCAT(" SELECT PrizeID,SUM(Qty) AS Total,BenefitName FROM ( ",@v_SelecteVoucherForPickWin," UNION ",@v_SelecteVoucherForPromotion, " ) AS ItemData GROUP BY PrizeID ");
SET @v_data = CONCAT("INSERT INTO temp_prize_redemption", @eVoucherData);
PREPARE s2 FROM @v_data;
EXECUTE s2;
DEALLOCATE PREPARE s2;
/************************************* Physical Voucher Data ****************************/
SET @v_SelectPhysicalVoucherForPickWin = CONCAT(" SELECT cpw.VoucherAmount AS PrizeID,cpw.Benefit AS BenefitName
,COUNT(DISTINCT VoucherAmount) AS Qty
,cpw.VoucherID,cpw.VoucherAmount,cpc.CampaignName
FROM crm_promotion_campaign AS cpc
INNER JOIN crm_receipt AS cr ON cr.CampaignID = cpc.CampaignID
INNER JOIN crm_pickwin_winner AS cpw ON cpw.PickWinID = cr.PickWinID
WHERE 1 AND cpc.CampaignID = ",v_CampaignID," AND cpw.BenefitType = '2'
GROUP BY PrizeID ");
SET @v_SelectPhysicalVoucherForPromotion = CONCAT(" SELECT crb.VoucherAmount AS PrizeID,crb.BenefitName
,COUNT(DISTINCT VoucherAmount) AS Qty,crb.VoucherID,crb.VoucherAmount,cpc.CampaignName
FROM crm_promotion_campaign AS cpc
INNER JOIN crm_receipt AS cr ON cr.CampaignID = cpc.CampaignID AND cr.TypeID = '1'
INNER JOIN crm_receipt_benefit AS crb ON crb.PromotionID = cr.PromotionID
WHERE 1 AND cpc.CampaignID = ",v_CampaignID," AND crb.BenefitTypeID = '2'
GROUP BY PrizeID ");
SET @PhysicalVoucherVoucherData = CONCAT(" SELECT PrizeID,SUM(Qty) AS Total,BenefitName FROM ( ",@v_SelectPhysicalVoucherForPickWin," UNION ",@v_SelectPhysicalVoucherForPromotion, " ) AS ItemData GROUP BY PrizeID ");
SET @v_data = CONCAT("INSERT INTO temp_prize_redemption", @PhysicalVoucherVoucherData);
PREPARE s3 FROM @v_data;
EXECUTE s3;
DEALLOCATE PREPARE s3;
SELECT * FROM temp_prize_redemption;
END$$
任何人都可以请看一下这个并且可以给我任何提示,以便我可以解决这个问题
答案 0 :(得分:1)
这应该是一个评论,但它有点长。
当我有一两个星期的时间,我会开始尝试理解你的SQL - 但是PREPARE / EXECUTE有一小组用例,这似乎不是其中之一。
缺少(仍然)PHP代码的关键部分。
您的insert语句没有明确说明输入应该匹配的列(并不总是致命)。
通过随机机会,某人可能会发布与您的问题相符的解决方案,但如果没有发生,您应该将SQL和PHP降低到重现故障所需的最小值。