gpt4 book ai didi

mysql - 无法在 Concat select Prepared 语句中传递参数值

转载 作者:行者123 更新时间:2023-11-29 06:08:06 25 4
gpt4 key购买 nike

我发现存储过程不只执行这部分:

          SET @sql 
= CONCAT('SELECT TraineeID, ', @sql, '
from tbl_submit_coursefee c
where c.BatchID='BID'
group by c.TraineeID');

在 where 子句 '' 中,引号不允许并且没有引号查询返回空,但是如果我将参数值直接放在 where 子句中,那么它就可以工作。我真的很喜欢这个。

这是我准备好的声明(工作正常):

SET @sql = NULL;    
SELECT
GROUP_CONCAT(DISTINCT
CONCAT ('MAX(IF(BillNo = ''', BillNo, ''', CRA, NULL)) AS `Inv.', BillNo, '`')
) INTO @sql
FROM tbl_submit_coursefee
WHERE BatchID="ID-Welding/FMMTTC-01M/R8/01";

SET @sql
= CONCAT('SELECT TraineeID, ', @sql, '
from tbl_submit_coursefee c
where c.BatchID="ID-Welding/FMMTTC-01M/R8/01"
group by c.TraineeID');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

将此语句放入存储过程时(不工作):

DELIMITER @@
DROP PROCEDURE GetRetainment @@
CREATE PROCEDURE vtproject.GetRetainment
(IN `BID` VARCHAR(100))
BEGIN
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT ('MAX(IF(BillNo = '', BillNo, '', CRA, NULL)) AS `Inv.', BillNo, '`')
) INTO @sql
FROM tbl_submit_coursefee
WHERE BatchID=BID;

SET @sql
= CONCAT('SELECT TraineeID, ', @sql, '
from tbl_submit_coursefee c
where c.BatchID='BID'
group by c.TraineeID');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END @@
DELIMITER ;

如果我在 prepare stmt FROM @sql 之前 select @sql 然后输出如下:

SELECT TraineeID, MAX(IF(BillNo = 1, CRA, NULL)) AS `Inv.1`,MAX(IF(BillNo = 2, CRA, NULL)) AS `Inv.2`,MAX(IF(BillNo = 3, CRA, NULL)) AS `Inv.3`,MAX(IF(BillNo = 4, CRA, NULL)) AS `Inv.4`,MAX(IF(BillNo = 5, CRA, NULL)) AS `Inv.5`,MAX(IF(BillNo = 6, CRA, NULL)) AS `Inv.6`,MAX(IF(BillNo = 7, CRA, NULL)) AS `Inv.7` from tbl_submit_coursefee c where c.BatchID=BID group by c.TraineeID

最佳答案

你需要转义引号,像这样:

SET @sql 
= CONCAT('SELECT TraineeID, ', @sql, '
from tbl_submit_coursefee c
where c.BatchID=''',BID,'''
group by c.TraineeID');

根据文档:http://dev.mysql.com/doc/refman/5.7/en/string-literals.html

关于mysql - 无法在 Concat select Prepared 语句中传递参数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40125013/

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