带有/ parameters的DB2存储过程

时间:2014-05-19 19:57:45

标签: sql stored-procedures db2

我很难从我的存储过程中产生正确的结果。我使用的是db2数据库,我有3个输入参数division,department,project。我的电话声明看起来像这样。

CALL schema.stored_procedure ('IT', 'MARKETING', 'ONLINE FULFILLMENT')

我需要生成在指定第三个参数时显示数据行的结果,或者具有项目名称的值(如上面的示例' Online_fulfillment' 并且当第三个参数具有值' ALL'时显示所有结果。对于项目名称(根据以下示例' ALL' )。

CALL schema.stored_procedure ('IT', 'MARKETING', 'ALL')

我下面的查询目前只返回列标题名称而没有结果,我在调试时遇到问题。这是我当前的存储过程。

CREATE PROCEDURE schema.stored_procedure
  (IN in_DIVISION_NAME VARCHAR(200)
  ,IN in_DEPARTMENT_NAME VARCHAR(20)
  ,IN in_PROJECT_NAME VARCHAR(400)
  )
  DYNAMIC RESULT SETS 1
 BEGIN
  IF (in_PROJECT_NAME = 'ALL') THEN
   BEGIN
    DECLARE GLOBAL TEMPORARY TABLE TEMP_DW_1
      (DIM_PROJECT_ID INT 
      ,PROJECT_NAME VARCHAR (400) 
      ,DIM_DEPARTMENT_ID INT  
      ,DEPARTMENT_NAME VARCHAR(100) 
      ,DIVISION_NAME VARCHAR(100)  
      ) ON COMMIT DELETE ROWS NOT LOGGED WITH REPLACE;
   END;

    INSERT INTO SESSION.TEMP_DW_1 (DIM_PROJECT_ID, PROJECT_NAME, DIM_DEPARTMENT_ID,
      DEPARTMENT_NAME,DIVISION_NAME)
             SELECT DISTINCT DJ.DIM_PROJECT_ID
            ,PROJECT_NAME
            ,DIM_DEPARTMENT_ID
            ,DEPARTMENT_NAME
            ,DIVISION_NAME
                 FROM SCHEMA.FACT_TABLE 
                 WHERE DEPARTMENT_NAME =  in_DEPARTMENT_NAME
                 AND DIVISION_NAME =  in_DIVISION_NAME; 

          BEGIN
        DECLARE  exitCursor CURSOR WITH RETURN FOR
        SELECT * 
        FROM SESSION.TEMP_DW_1;

        OPEN exitCursor;

           END;
 END 

预期成果: CALL schema.stored_procedure(' IT','营销','在线履行') enter image description here 预期成绩: CALL schema.stored_procedure(' IT','营销','所有') enter image description here

1 个答案:

答案 0 :(得分:0)

我相信我已经通过添加额外的IF语句解决了这个问题,设置了in_PROJECT_NAME<> 'ALL'并在设置PROJECT_NAME = in_PROJECT_NAME的第二个查询中添加其他过滤器。可能是解决这个问题的一种更简单的方法,但它有效:

IF (in_PROJECT_NAME <> 'ALL') THEN
 BEGIN
  DECLARE GLOBAL TEMPORARY TABLE TEMP_DW_1
    (DIM_PROJECT_ID INT 
    ,PROJECT_NAME VARCHAR (400) 
    ,DIM_DEPARTMENT_ID INT  
    ,DEPARTMENT_NAME VARCHAR(100) 
    ,DIVISION_NAME VARCHAR(100)  
    ) ON COMMIT DELETE ROWS NOT LOGGED WITH REPLACE;
 END;

 INSERT INTO SESSION.TEMP_DW_1 (DIM_PROJECT_ID, PROJECT_NAME, DIM_DEPARTMENT_ID,
   DEPARTMENT_NAME ,DIVISION_NAME)
   SELECT DISTINCT DJ.DIM_PROJECT_ID
     ,PROJECT_NAME
     ,DIM_DEPARTMENT_ID
     ,DEPARTMENT_NAME
     ,DIVISION_NAME
     FROM SCHEMA.FACT_TABLE 
     WHERE DEPARTMENT_NAME = in_DEPARTMENT_NAME
     AND DIVISION_NAME =  in_DIVISION_NAME; 
     AND PROJECT_NAME = in_PROJECT_NAME