首页 > 代码库 > PLSQL存储过程及定时任务_示例
PLSQL存储过程及定时任务_示例
环境: Oracle11g, PLSQL11&12, windows7
--存储过程
CREATE OR REPLACE PROCEDURE test_proc IS
STARTTIME TIMESTAMP;
OVERTIME TIMESTAMP;
COMPARETIME TIMESTAMP;
BEGIN
STARTTIME := TO_DATE(TO_CHAR(ADD_MONTHS(LAST_DAY(SYSDATE) + 1, -2),‘yyyy-MM-dd‘),‘yyyy-MM-dd‘);
OVERTIME := TO_DATE(TO_CHAR(ADD_MONTHS(LAST_DAY(SYSDATE), -1),‘yyyy-MM-dd‘),‘yyyy-MM-dd‘);
COMPARETIME := STARTTIME + INTERVAL ‘1‘ MONTH;
INSERT INTO table1(YEARS,MONTHS,MAN,MANP,WOMAN,WOMANP,EDUMIDD,EDUMIDDP,EDUHIGH,EDUHIGHP,EDUJUNI,EDUJUNIP,EDUUNDE,
EDUUNDEP,NATIONHAN,NATIONHANP,NATIONNOHAN,NATIONNOHANP,CONTRACTTYPE1,CONTRACTTYPE1P,CONTRACTTYPE2,CONTRACTTYPE2P,CONTRACTTYPE3,
CONTRACTTYPE3P,CONTRACTTYPE4,CONTRACTTYPE4P,CHANNELTYPE1,CHANNELTYPE1P,CHANNELTYPE2,CHANNELTYPE2P,CHANNELTYPE3,HANNELTYPE3P,
CHANNELTYPE4,CHANNELTYPE4P,CHANNELTYPE5,CHANNELTYPE5P,CERTTYPE1,CERTTYPE1P,CERTTYPE2,CERTTYPE2P,CERTTYPE3,CERTTYPE3P,CERTTYPE4,
CERTTYPE4P,ALLCOUNT,AREAID)
SELECT
TO_CHAR(ADD_MONTHS(LAST_DAY(SYSDATE) + 1, -2), ‘yyyy‘),
TO_CHAR(ADD_MONTHS(LAST_DAY(SYSDATE) + 1, -2), ‘MM‘),
SUM(CASEWHEN SEX = 0 THEN 1 ELSE 0 END) AS MAN,
SUM(CASE WHEN SEX = 0 THEN 1 ELSE 0 END) / COUNT(*) AS MANP,
SUM(CASE WHEN SEX = 0 THEN 0 ELSE 1 END) AS WOMAN,
SUM(CASE WHEN SEX = 0 THEN 0 ELSE 1 END) / COUNT(*) AS WOMANP,
SUM(CASE WHEN CULTURE = 4 OR CULTURE = 7 THEN 1 ELSE 0 END) AS EDUMIDD,
SUM(CASE WHEN CULTURE = 4 OR CULTURE = 7 THEN 1 ELSE 0 END) / COUNT(*) AS EDUMIDDP,
SUM(CASE WHEN CULTURE = 3 THEN 1 ELSE 0 END) AS EDUHIGH,
SUM(CASE WHEN CULTURE = 3 THEN 1 ELSE 0 END) / COUNT(*) AS EDUHIGHP,
SUM(CASE WHEN CULTURE = 1 THEN 1 ELSE 0 END) AS EDUJUNI,
SUM(CASE WHEN CULTURE = 1 THEN 1 ELSE 0 END) / COUNT(*) AS EDUJUNIP,
SUM(CASE WHEN CULTURE = 0 OR CULTURE = 5 OR CULTURE = 6 THEN 1 ELSE 0 END) AS EDUUNDE,
SUM(CASE WHEN CULTURE = 0 OR CULTURE = 5 OR CULTURE = 6 THEN 1 ELSE 0 END) / COUNT(*) AS EDUUNDEP,
SUM(CASE WHEN NATION = 1 THEN 1 ELSE 0 END) AS NATIONHAN,
SUM(CASE WHEN NATION = 1 THEN 1 ELSE 0 END) / COUNT(*) AS NATIONHANP,
SUM(CASE WHEN NATION = 1 THEN 0 ELSE 1 END) AS NATIONNOHAN,
SUM(CASE WHEN NATION = 1 THEN 0 ELSE 1 END) / COUNT(*) AS NATIONNOHANP,
SUM(CASE WHEN CONTRACTTYPE = 1 THEN 1 ELSE 0 END) AS CONTRACTTTYPE1,
SUM(CASE WHEN CONTRACTTYPE = 1 THEN 1 ELSE 0 END) / COUNT(*) AS CONTRACTTYPE1P,
SUM(CASE WHEN CONTRACTTYPE = 2 THEN 1 ELSE 0 END) AS CONTRACTTTYPE2,
SUM(CASE WHEN CONTRACTTYPE = 2 THEN 1 ELSE 0 END) / COUNT(*) AS CONTRACTTYPE2P,
SUM(CASE WHEN CONTRACTTYPE = 3 THEN 1 ELSE 0 END) AS CONTRACTTTYPE3,
SUM(CASE WHEN CONTRACTTYPE = 3 THEN 1 ELSE 0 END) / COUNT(*) AS CONTRACTTYPE3P,
SUM(CASE WHEN CONTRACTTYPE = 4 THEN 1 ELSE 0 END) AS CONTRACTTTYPE4,
SUM(CASE WHEN CONTRACTTYPE = 4 THEN 1 ELSE 0 END) / COUNT(*) AS CONTRACTTYPE4P,
SUM(CASE WHEN CHANNELTYPE = 1 THEN 1 ELSE 0 END) AS CHANNELTYPE1,
SUM(CASE WHEN CHANNELTYPE = 1 THEN 1 ELSE 0 END) / COUNT(*) AS CHANNELTYPE1P,
SUM(CASE WHEN CHANNELTYPE = 2 THEN 1 ELSE 0 END) AS CHANNELTYPE2,
SUM(CASE WHEN CHANNELTYPE = 2 THEN 1 ELSE 0 END) / COUNT(*) AS CHANNELTYPE2P,
SUM(CASE WHEN CHANNELTYPE = 3 THEN 1 ELSE 0 END) AS CHANNELTYPE3,
SUM(CASE WHEN CHANNELTYPE = 3 THEN 1 ELSE 0 END) / COUNT(*) AS CHANNELTYPE3P,
SUM(CASE WHEN CHANNELTYPE = 4 THEN 1 ELSE 0 END) AS CHANNELTYPE4,
SUM(CASE WHEN CHANNELTYPE = 4 THEN 1 ELSE 0 END) / COUNT(*) AS CHANNELTYPE4P,
SUM(CASE WHEN CHANNELTYPE = 5 THEN 1 ELSE 0 END) AS CHANNELTYPE5,
SUM(CASE WHEN CHANNELTYPE = 5 THEN 1 ELSE 0 END) / COUNT(*) AS CHANNELTYPE5P,
SUM(CASE WHEN CERTTYPE = 0 THEN 1 ELSE 0 END) AS CERTTYPE1,
SUM(CASE WHEN CERTTYPE = 0 THEN 1 ELSE 0 END) / COUNT(*) AS CERTTYPE1P,
SUM(CASE WHEN CERTTYPE = 1 THEN 1 ELSE 0 END) AS CERTTYPE2,
SUM(CASE WHEN CERTTYPE = 1 THEN 1 ELSE 0 END) / COUNT(*) AS CERTTYPE2P,
SUM(CASE WHEN CERTTYPE = 2 THEN 1 ELSE 0 END) AS CERTTYPE3,
SUM(CASE WHEN CERTTYPE = 2 THEN 1 ELSE 0 END) / COUNT(*) AS CERTTYPE3P,
SUM(CASE WHEN CERTTYPE = 5 THEN 1 ELSE 0 END) AS CERTTYPE4,
SUM(CASE WHEN CERTTYPE = 5 THEN 1 ELSE 0 END) / COUNT(*) AS CERTTYPE4P,
COUNT(*) AS ALLCOUNT,
ZONETABLESPACE
FROM (SELECT * FROM table2 WHERE CERTSTATE = ‘1‘ AND REGISTERTIME <= OVERTIME AND ORGNO IS NOT NULL AND ORGNO <> ‘ ‘) t2
GROUP BY ZONETABLESPACE ORDER BY ZONETABLESPACE;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(‘Exception happened ,data was rollback !‘);
ROLLBACK;
END DEVMONTHSTAT;
--定时器DBMS_JOBS
begin
sys.dbms_job.submit(job => :job,
what => ‘devmonthstat;‘,
next_date => to_date(‘01-05-2017‘, ‘dd-mm-yyyy‘),
interval => ‘trunc(last_day(SYSDATE)) +1‘);
commit;
end;
/
PLSQL存储过程及定时任务_示例