查询中的MySQL存储过程

时间:2012-11-09 07:06:56

标签: mysql sql stored-procedures

我想制作一个MySQL存储过程

DELIMITER $$

DROP PROCEDURE IF EXISTS `SECTOR_INDUSTRY_SP` $$
CREATE PROCEDURE `SECTOR_INDUSTRY_SP`(IN RINSERIES TEXT, IN COMMUNITYIDS TEXT)
    READS SQL DATA
BEGIN
SELECT *
from PROFESSIONAL_IDENTITY_MERGED p , std_company_detail s
where (p.pim_company_id = s.cid) AND p.pim_community_id IN  ('+'+COMMUNITYIDS+'+')
AND p.pim_rin IN  ('+'+RINSERIES+'+');

END $$

DELIMITER ;

正如你所看到的,我有两个参数称这个程序为RINSERIES,文本格式如'12312,1234,1239',同样,COMMUNITYIDS就像'2,5,8',但是当我通过时,它就像解释一样

SELECT *
    from PROFESSIONAL_IDENTITY_MERGED p , std_company_detail s
    where (p.pim_company_id = s.cid) AND p.pim_community_id IN  ('12312,1234,1239')
    AND p.pim_rin IN  ('2,5,8');

但我希望此查询看起来像下面的

SELECT *
        from PROFESSIONAL_IDENTITY_MERGED p , std_company_detail s
        where (p.pim_company_id = s.cid) AND p.pim_community_id IN  (12312,1234,1239)
        AND p.pim_rin IN  (2,5,8);

我该怎么做?谢谢!

1 个答案:

答案 0 :(得分:3)

试试这个:

DELIMITER $$

DROP PROCEDURE IF EXISTS `SECTOR_INDUSTRY_SP` $$
CREATE PROCEDURE `SECTOR_INDUSTRY_SP`(IN RINSERIES TEXT, IN COMMUNITYIDS TEXT)
    READS SQL DATA
BEGIN

SET @s = CONCAT("SELECT *
from PROFESSIONAL_IDENTITY_MERGED p , std_company_detail s
where (p.pim_company_id = s.cid) AND p.pim_community_id IN  (", COMMUNITYIDS, ")
AND p.pim_rin IN  (",RINSERIES,")");

PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

END $$

DELIMITER ;