0

我有一个存储过程,它根据“foo”的值选择 IDvar,并为它们分配包含 foo 的变量名。我可以这样做:

CALL db0.genericStoredProc(IDvar, "foo", "'foo'");

理想情况下,我只想写

CALL db0.genericStoredProc(IDvar, "foo");

如何利用 bar,这样我就不需要 bar2 参数?这是我的存储过程的相关部分。

DROP PROCEDURE IF EXISTS genericStoredProc;
DELIMITER $ $
CREATE PROCEDURE genericStoredProc(myID BIGINT(20) UNSIGNED, bar VARCHAR(5), bar2 VARCHAR(5))
BEGIN
SET @s = CONCAT('SELECT gt.name, gt.junk, gt.junkName, count(gt.idvar) as id_count,
        SUM(CASE 
            WHEN ot.offset <0 THEN 1 
            ELSE 0
        END) AS ', bar,'_id_count
    FROM genericTable gt
        LEFT JOIN otherTable ot ON gt.IDvar2 = ot.IDvar
    WHERE (gt.idvar = ', IDvar, ') AND (ot.prmtr = ', bar2, ')
GROUP BY gt.idvar');

PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END $$
DELIMITER ;
4

1 回答 1

1


使用如下局部变量

DELIMITER $ $
CREATE PROCEDURE genericStoredProc(myID BIGINT(20) UNSIGNED, bar VARCHAR(5))
BEGIN
DECLARE bar2 VARCHAR(5);
SET bar2=bar1 ;
于 2013-08-24T20:23:47.373 回答