Oracle SQL存储过程有三个select语句

时间:2015-09-29 17:13:19

标签: sql oracle stored-procedures plsql union

我正致力于将当前设置为通过JAVA运行的Oracle SQL查询转换为存储过程。基本上,同一个表中有三个带有日期范围参数的select语句。

CREATE OR REPLACE PROCEDURE get_users(startdate_in IN DATE,
                                      enddate_in   IN DATE)
IS
 BEGIN

    DELETE FROM temp_table;

    INSERT INTO temp_table (id, role, date_used, count_s)
      SELECT
        EMPLOYEE.CUSTOMERID                    AS user_id,
        'Customer'                             AS role_,
        to_char(EMPLOYEE.STARTEDON, 'MM-YYYY') AS req_month,
        count(EMPLOYEE.SUBJECT)                AS subj_count
      FROM RCUSER.EMPLOYEE EMPLOYEE
      WHERE EMPLOYEE.STATEID NOT IN (4, 9, 16, 31, 36)
            AND (EMPLOYEE.AGENTID = 0)
            AND (EMPLOYEE.STARTEDON >= to_date(startdate_in, 'yyyy-mm-dd'))
            AND (EMPLOYEE.STARTEDON < to_date(enddate_in, 'yyyy-mm-dd'))
      GROUP BY CUSTOMERID, to_char(EMPLOYEE.STARTEDON, 'MM-YYYY')

      UNION

      SELECT
        EMPLOYEE.PERFORMERID                   AS user_id,
        'Performer'                            AS role_,
        to_char(EMPLOYEE.STARTEDON, 'MM-YYYY') AS req_month,
        count(EMPLOYEE.SUBJECT)                AS subj_count
      FROM RCUSER.EMPLOYEE EMPLOYEE
      WHERE EMPLOYEE.PEID IN (1, 4, 6)
            AND EMPLOYEE.STATEID NOT IN (4, 9, 16, 31, 36)
            AND (EMPLOYEE.AGENTID = 0)
            AND (EMPLOYEE.STARTEDON >= to_date(startdate_in, 'yyyy-mm-dd'))
            AND (EMPLOYEE.STARTEDON < to_date(enddate_in, 'yyyy-mm-dd'))
      GROUP BY PERFORMERID, to_char(EMPLOYEE.STARTEDON, 'MM-YYYY')

      UNION

      SELECT
        employee.performerid                   AS user_id,
        'Approver'                             AS role_,
        to_char(EMPLOYEE.STARTEDON, 'MM-YYYY') AS req_month,
        count(EMPLOYEE.SUBJECT)                AS subj_count
      FROM RCUSER.EMPLOYEE EMPLOYEE
      WHERE EMPLOYEE.PEID IN (2, 3)
            AND EMPLOYEE.STATEID NOT IN (4, 9, 16, 31, 36)
            AND (EMPLOYEE.AGENTID = 0)
            AND (EMPLOYEE.STARTEDON >= to_date(startdate_in, 'yyyy-mm-dd'))
            AND (EMPLOYEE.STARTEDON < to_date(enddate_in, 'yyyy-mm-dd'))
      GROUP BY PERFORMERID, to_char(EMPLOYEE.STARTEDON, 'MM-YYYY');

  END;

运行时,没有任何内容写入TEMP_TABLE。但是当我硬编码日期范围时,我得到了一些数据。

2 个答案:

答案 0 :(得分:0)

CREATE OR REPLACE PROCEDURE GET_PEOPLE(startdate_in IN DATE, enddate_in IN DATE)
IS

BEGIN
INSERT INTO temp_table
(ID, COL1, COL2, ROLE_,START_DATE,END_DATE)
VALUES
(select * from
(Select col1, col2, 'Customer' as Role_, col3, col3
where col1 in (1,2)
UNION
Select col1, col2, 'Authore' as Role_, col3, col3
where col1 in (3,4)
UNION
Select col1, col2,  'Buyer' as Role_,col3, col3
where col1 in (5,6))
where col3 between STARTDATE_IN and ENDDATE_IN;

END;

当然,你可以做更多的事情来编写好的代码:

  • 添加断言startdate_in和enddate_in是有效日期且在预期值范围内
  • 而不是临时表返回一个sys refcursor,其中包含在applicaiton中处理的结果
  • 使用全局临时表来减少撤消日志

答案 1 :(得分:0)

使用以下形式的单个SQL语句可以提高效率:

bundle exec rake assets:precompile RAILS_ENV=production

如果你需要将它写入临时表,那么只需插入它......

git push heroku master

不确定你的意思是col3(@startDate)虽然......也许......

Select col1,
       col2,
       case
         when col1 in (1,2) then 'Customer'
         when col1 in (3,4) then 'Authore'
         when col1 in (5,6) then 'Buyer'
       end as Role_,
       col3(@startDate),
       col3(@endDate)
where col1 in (1,2,3,4,5,6);