1

我有以下程序。但我没有得到预期的结果。

CREATE PROCEDURE `getHierarchy` (IN selectedValue LONGTEXT, IN selectedLevel VARCHAR(30))
CASE selectedLevel
 WHEN 'department' THEN 
    SELECT department_key,department,class_key,modified_class,subclass_key,modified_subclass,product_key,product_name FROM dim_product where department_key in (selectedValue) ORDER BY department_key,class_key,subclass_key,product_key;

 WHEN 'class' THEN  
    SELECT department_key,department,class_key,modified_class,subclass_key,modified_subclass,product_key,product_name FROM dim_product where class_key in (selectedValue) ORDER BY department_key,class_key,subclass_key,product_key;

 WHEN 'subclass' THEN   
    SELECT department_key,department,class_key,modified_class,subclass_key,modified_subclass,product_key,product_name FROM dim_product where subclass_key in (selectedValue) ORDER BY department_key,class_key,subclass_key,product_key;
 END CASE;
END

以下电话仅提供 1 个详细信息:

call getHierarchy("1,2",'subclass');
4

2 回答 2

2

如果不使用动态 SQL,就无法将字符串变量传递给IN谓词。MySQL 中的另一种选择(在您的情况下可能是最简单的选择)是使用该FIND_IN_SET()功能。例如,您的“子类”查询应替换为:

SELECT 
    department_key, 
    department,
    class_key,
    modified_class,
    subclass_key,
    modified_subclass,
    product_key,
    product_name 
FROM dim_product 
WHERE FIND_IN_SET(subclass_key, selectedValue)
ORDER BY department_key, class_key, subclass_key, product_key;
于 2013-10-01T07:56:56.650 回答
0

尝试改变

call getHierarchy("1,2",'subclass');

call getHierarchy("'1','2'",'subclass');
于 2013-10-01T07:54:38.873 回答