带有三条select语句的Oracle SQL存储过程
我正在将当前设置为通过JAVA运行的Oracle SQL查询转换为存储过程。基本上,同一个表中有三个select语句带有日期范围参数带有三条select语句的Oracle SQL存储过程,sql,oracle,stored-procedures,plsql,union,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_ta
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_表。但是当我硬编码日期范围时,我会得到一些数据
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,其中包含应用程序中处理的结果,而不是临时表
使用全局临时表减少撤消日志
您可以使用以下形式的单个SQL语句来提高效率:
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);
如果您需要将其写入临时表,则只需插入它
create or replace procedure my_procedure(start_date date, end_date date)
is
begin
insert into temp_table (... column names ...)
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(my_procedure.start_date),
col3(my_procedure.end_date)
where col1 in (1,2,3,4,5,6);
end;
/
不知道你说的是什么意思col3@startDate虽然也许
create or replace procedure my_procedure(start_date date, end_date date)
is
begin
insert into temp_table (... column names ...)
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_,
my_procedure.start_date,
my_procedure.end_date
where col1 in (1,2,3,4,5,6);
end;
/
我使用了你的建议,如果我必须对日期范围进行硬编码,它就会起作用。但当我将日期作为变量传递时,它返回为空。我在where子句部分中使用了date>=mystoreprocedure.startdate_和date