A lot of writing stored procedures recently, to summarize.
Writing stored procedures must conform to certain rules:
create or replace procedure 'name'
is
parms varchar2;//Declare variables
begin
//Write your business logic sql
end;
For example, the following is a cursor I wrote
CREATE OR REPLACE PROCEDURE SP_DEMO/**********************************************
Comprehensive Statistics --- Stored Procedure
********************************************/
IS
FK_TOTAL NUMBER; /*Total amount of labor outsourcing contract payment in this year*/
PK_TOTAL NUMBER; /*Amount of annual payment for project outsourcing cost*/
WK_TOTAL NUMBER; /*Manpower outsourcing workload in this year*/
CT NUMBER; /*Cost*/
EXIST_TOTAL NUMBER; / *Does data exist*/
CENTER VARCHAR2(256);
COMPANY VARCHAR2(256);
CURSOR CUR_CENTER IS
SELECT ORG.ORGNAME
FROM ORG_ORGANIZATION ORG
WHERE ORG.ORGSEQ LIKE '.1.102516.102251.100068.%' and ORG.ORGSEQ<>'.1.102516 .102251.100068.'
ORDER BY ORG.ORGNAME ASC;
CURSOR CUR_COMPANY IS
SELECT V.NAME FROM VENDOR V GROUP BY V.NAME ORDER BY V.NAME ASC;
BEGIN
/*If there is stock data in the query table, it will generate historical stock data D_BUSINESS 7 represents the cost table*/
SELECT COUNT(* )
INTO EXIST_TOTAL
FROM HF_DIARY_DETAIL D
WHERE TO_CHAR(D.D_TIME, 'YYYY') = TO_CHAR(SYSDATE, 'YYYY')
AND D.D_BUSINESS IN (7, 8);
/*If there is historical data, delete the original data and insert it again DATA*/
IF EXIST_TOTAL > 0 THEN
DELETE HF_DIARY_DETAIL D
WHERE TO_CHAR(D.D_TIME, 'YYYY') = TO_CHAR(SYSDATE, 'YYYY')
AND D.D_BUSINESS IN (7, 8);
COMMIT;
END IF;
/*ON Owning company cursor */
OPEN CUR_COMPANY;
LOOP
FETCH CUR_COMPANY
INTO COMPANY;
/*End cursor*/
EXIT WHEN CUR_COMPANY %NOTFOUND;
/*Open the owning center cursor*/
OPEN CUR_CENTER;
LOOP
FETCH CUR_CENTER
INTO CENTER;
/*End cursor*/
EXIT WHEN CUR_CENTER %NOTFOUND;
/*1. Total manpower outsourcing contract payments this year*/
SELECT SUM(PAY.PYMTTOTAMT) AS TOT
INTO FK_TOTAL
FROM INTER_PM_PAYMENTAPPLY PAY, INTER_PM_CONTRACT CON
WHERE PAY.RLTVCONID = CON.DATAID
AND PAY.RLTVOUTSRGAGRMNTID IS NOT NULL
AND PAY.BLNGCNTR = CENTER
AND CON.SUPPLIER = COMPANY
AND TO_CHAR(PAY.ACTLPYMTDT, 'YYYY') = TO_CHAR(SYSDATE, 'YYYY');
/*1. The total amount of outsourcing contract payments for this year*/
SELECT SUM(PAY.PYMTTOTAMT) AS TOT
INTO PK_TOTAL
FROM INTER_PM_PAYMENTAPPLY PAY, INTER_PM_CONTRACT CON
WHERE PAY.RLTVCONID = CON.DATAID
AND CON.DVLPPRJTID IS NOT NULL
AND PAY.BLNGCNTR = CENTER
AND CON.SUPPLIER = COMPANY
AND TO_CHAR(PAY.ACTLPYMTDT, 'YYYY') = TO_CHAR(SYSDATE, 'YYYY');
/* 2. This year's labor outsourcing workload*/
SELECT COUNT(*) AS PTOTL
INTO WK_TOTAL
FROM LABOUR LAB, VENDOR VEN, org_organization org
WHERE LAB.VENDORID = VEN.ID
AND lab.orgcode = org.orgcode
AND LAB.TYPE = 0
and LAB.STAMPOFIN is not null --1
. Number of outsourced personnel in the current year
AND (TO_CHAR(LAB.STAMPOFIN, 'yyyy') = TO_CHAR(SYSDATE, 'YYYY') --2. I joined
the group in 2017 and have not left the group yet or this year
or (LAB.STAMPOFOFF IS NULL OR LAB.STAMPOFOFF>TO_DATE(TO_CHAR(SYSDATE,'yyyy')||'01'||'01 ','yyyy-mm-dd')))
AND org.orgname = CENTER
AND VEN.NAME = COMPANY;
CT := 0;
IF WK_TOTAL > 0 THEN
CT := FK_TOTAL / WK_TOTAL;
END IF;
/*1.INSERT Outsourcing Cost Data 7: Outsourcing Cost*/
INSERT INTO HF_DIARY_DETAIL D
(D.D_TIME,
D.D_BUSINESS,
D.D_QUOTA,
D.D_SYSTEM,
D.D_PLINE,
D.D_CENTER,
D.D_COMPANY)
VALUES
(SYSDATE, 7, CT, NULL, NULL, CENTER, COMPANY);
/*Insert project outsourcing cost 8: project outsourcing cost*/
INSERT INTO HF_DIARY_DETAIL D
(D.D_TIME,
D.D_BUSINESS,
D.D_QUOTA,
D.D_SYSTEM,
D.D_PLINE,
D.D_CENTER,
D.D_COMPANY)
VALUES
(SYSDATE, 8, PK_TOTAL, NULL, NULL, CENTER, COMPANY);
END LOOP;
CLOSE CUR_CENTER;
END LOOP;
CLOSE CUR_COMPANY;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
END;
-------------Configure job
On the command line of plsql, enter:
variable job1 number;
begin
dbms_job.submit(:job1,'sp_demo;',sysdate,'sysdate+1+60/1440');
end;
/
------------- start timer
begin
dbms_job.run(:job1);
end;
/
------- View timer startup and job operation
select job,next_date,next_sec,failures,broken from user_jobs;