The procedure takes two dates and find the reports that issued during this duration, and update total salary, and print them.
CREATE OR REPLACE PROCEDURE Salary_Update(p_Date1 DATE, p_Date2 DATE) AS
CURSOR MYCR IS
SELECT ReportID, TotalSale, Rate, TotalSalary, ReportDate
FROM WEEKLY_REPORT
WHERE ReportDate BETWEEN TO_DATE(p_Date1, 'DD-MM-YYYY') AND TO_DATE(p_Date2, 'DD-MM-YYYY');
BEGIN
FOR MYPOINTER IN MYCR LOOP
UPDATE WEEKLY_REPORT SET TotalSalary = ((TotalSale/100)*Rate);
DBMS_OUTPUT.PUT_LINE('The total salary for report ' || MYPOINTER.REPORTID || ' updated to ' || MYPOINTER.TotalSalary || 'dollars, which is ' || MYPOINTER.Rate || '% of the total sale of ' || MYPOINTER.TotalSale || 'dollars.');
WHERE ReportID = MYPOINTER.ReportID;
END LOOP;
END Salary_Update;
/
the execution
BEGIN Salary_Update('02-04-2020','05-04-2020');
END;
BEGIN
Salary_Update(to_date('02-04-2020','dd-mm-yyyy'), to_date('05-04-2020','dd-mm-yyyy'));
END;
both don't work.
There are a number of syntax and logic errors. You haven't specified exactly what you want the code to do or provided a reproducible test case but my guess is that you want something like
CREATE OR REPLACE PROCEDURE Salary_Update(
p_Date1 DATE,
p_Date2 DATE)
AS
CURSOR MYCR IS
SELECT ReportID, TotalSale, Rate, TotalSalary, ReportDate
FROM WEEKLY_REPORT
WHERE ReportDate BETWEEN p_date1 AND p_date2;
BEGIN
FOR MYPOINTER IN MYCR
LOOP
UPDATE WEEKLY_REPORT
SET TotalSalary = ((TotalSale/100)*Rate)
WHERE ReportID = MYPOINTER.ReportID;
DBMS_OUTPUT.PUT_LINE('The total salary for report ' || MYPOINTER.REPORTID || ' updated to ' || MYPOINTER.TotalSalary || 'dollars, which is ' || MYPOINTER.Rate || '% of the total sale of ' || MYPOINTER.TotalSale || 'dollars.');
END LOOP;
END Salary_Update;
/
The string you're outputting doesn't seem to make sense-- MYPOINTER.TotalSalary is the value from the cursor which will be the value from before the update. If it is important that you print out the value you updated TotalSalary to, you'd probably want something like
CREATE OR REPLACE PROCEDURE Salary_Update(
p_Date1 DATE,
p_Date2 DATE)
AS
CURSOR MYCR IS
SELECT ReportID, TotalSale, Rate, TotalSalary, ReportDate
FROM WEEKLY_REPORT
WHERE ReportDate BETWEEN p_date1 AND p_date2;
l_newSalary weekly_report.totalSalary%type;
BEGIN
FOR MYPOINTER IN MYCR
LOOP
l_newSalary := (mypointer.TotalSale/100) * mypointer.rate;
UPDATE WEEKLY_REPORT
SET TotalSalary = l_newSalary
WHERE ReportID = MYPOINTER.ReportID;
DBMS_OUTPUT.PUT_LINE('The total salary for report ' || MYPOINTER.REPORTID || ' updated to ' || l_newSalary || 'dollars, which is ' || MYPOINTER.Rate || '% of the total sale of ' || MYPOINTER.TotalSale || 'dollars.');
END LOOP;
END Salary_Update;
/
If the error that you're getting is an ORA-00942 error that weekly_report doesn't exist, then there is no such table in the current schema. Assuming that the query works when run interactively, potentially the table exists in a different schema, there is a local synonym in the current schema for that table, and the owner of the procedure only has access to the table granted via a role. In that case, you'd need your DBA to grant access to the table directly to the owner of the procedure.
Trying to create a procedure that changes dates when the end date is less than sysdate, but it changes all the dates, so here is the code:
CREATE OR REPLACE PROCEDURE CHANGE_DATES
as
BEGIN
FOR rec IN (SELECT start_period_date,end_period_date FROM mytable) LOOP
DBMS_OUTPUT.PUT_LINE('Old Date:' || rec.start_period_date ||' ' || rec.end_period_date);
IF rec.end_period_date < sysdate THEN
update mytable set start_period_date = sysdate;
update mytable set end_period_date = sysdate + 6;
commit;
--DBMS_OUTPUT.PUT_LINE('New Date:' || begin_date ||' ' ||end_date);
end if;
END LOOP;
END;
You need a WHERE clause (and you can do it all in one UPDATE statement):
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE mytable ( start_period_date, end_period_date ) AS
SELECT DATE '2018-01-01', DATE '2018-01-02' FROM DUAL
/
CREATE OR REPLACE PROCEDURE CHANGE_DATES
AS
start_dates SYS.ODCIDATELIST;
end_dates SYS.ODCIDATELIST;
BEGIN
UPDATE ( SELECT m.*,
( SELECT m.start_period_date FROM DUAL ) AS old_start_date,
( SELECT m.end_period_date FROM DUAL ) AS old_end_date
FROM mytable m )
SET start_period_date = SYSDATE,
end_period_date = SYSDATE + INTERVAL '6' DAY
WHERE end_period_date < SYSDATE
RETURNING old_start_date, old_end_date
BULK COLLECT INTO start_dates, end_dates;
FOR i IN 1 .. start_dates.COUNT LOOP
DBMS_OUTPUT.PUT_LINE( 'Old Date:' || start_dates(i) ||' ' || end_dates(i) );
END LOOP;
-- Do not COMMIT in the procedure, COMMIT in the calling scope.
END;
/
Query 1:
BEGIN
CHANGE_DATES;
COMMIT;
END;
Query 2:
SELECT * FROM mytable
Results:
| START_PERIOD_DATE | END_PERIOD_DATE |
|----------------------|----------------------|
| 2018-05-21T09:06:05Z | 2018-05-27T09:06:05Z |
I have a table structure like as :
desc temp_table ;
Name Null? Type
student_name VARCHAR2(500)
Is_delete NUMBER(2)
using pl sql procedure I'm trying to store all values of above table into a variable.
If table has more then one row then I'm getting error like
ORA-01422: exact fetch returns more than requested number of rows
So without using cursor how can we store multiple row's in a variable??
Define two collection types:
CREATE OR REPLACE TYPE StringList IS TABLE OF VARCHAR2(4000);
/
CREATE OR REPLACE TYPE BooleanList IS TABLE OF NUMBER(1,0);
/
Use BULK COLLECT INTO:
CREATE OR REPLACE PROCEDURE UPDATE_TABLE
AS
Names StringList;
Del BooleanList;
BEGIN
DBMS_OUTPUT.ENABLE(1000000);
SELECT student_name,
Is_delete
BULK COLLECT INTO
Names,
Del
FROM temp_table
WHERE MODIFIED_DATE >= TRUNC( SYSDATE )
AND MODIFIED_DATE < TRUNC( SYSDATE ) + INTERVAL '1' DAY;
DBMS_OUTPUT.PUT_LINE ('Number Of Names: ' || Names.COUNT );
DBMS_OUTPUT.PUT_LINE ('Number Of Del: ' || Del.COUNT ); -- Will always be the same amount
FOR i IN 1 .. Names.COUNT LOOP
DBMS_OUTPUT.PUT_LINE ( Names(i) || ', ' || Del(i) );
END LOOP;
END;
/
I'm new to PL/SQL. Can anyone please help fix my compilation error? Your help is much appreciated. Also, after I would like to call this procedure to check and add a new user.
create or replace procedure CheckAddUser ( userid in varchar2(20))
as
declare vartmp number;
begin
SELECT nvl((SELECT distinct 1 FROM crm_admin.LTY_USER_STORE WHERE usr_nm = userid ), 0) INTO :varTmp FROM dual;
IF (:varTmp = 0) THEN
dbms_output.put_line('the user ' || ':userid' || ' does not exist');
elsif (:varTmp = 1) THEN
dbms_output.put_line('the user ' || ':userid' || ' already exist');
End if;
end;
Try this:
create or replace procedure checkadduser(userid in varchar2)
as
vartmp number;
begin
select coalesce(max(1), 0) into vartmp
from dual
where exists (
select 1
from crm_admin.lty_user_store
where usr_nm = userid
);
if vartmp = 0 then
dbms_output.put_line('the user ' || userid || ' does not exist');
elsif vartmp = 1 then
dbms_output.put_line('the user ' || userid || ' already exist');
end if;
end;
/
Changes made:
Removed the size from parameter
Removed the declare keyword - not part of the procedure syntax
Modified the query to stop searching as soon as a row is found and return 1 otherwise 0.
select coalesce(max(1), 0) into varTmp
from dual
where exists (
select 1
from crm_admin.lty_user_store
where usr_nm = userid
);
If the usr_nm is unique in your table, this will work well too (this can be used even if it's not unique but can be bit less performant if number of rows per usr_nm can be arbitrarily large):
select coalesce(max(1), 0)
into varTmp
from crm_admin.lty_user_store
where usr_nm = userid
Do not use : with the variables and parameters.
Don't use ":" with var.
I did some changes, which I would migth use:
--I recommended to change procedure to function, then you can use it in SQL
create or replace
procedure CheckAddUser ( userid in varchar2)
as
--Best practics, use self-describing variables
isuserexist number(1,0); -- vartmp
message_suff varchar2(30):=' does not exist';
begin
--Best practics, first check the parameters
if trim(userid) is null then
raise_application_error(-20000, 'Param userid is empty');
end if;
select count(*) into isuserexist
from crm_admin.lty_user_store
where usr_nm = userid;
--only one if, reads easier
if isUserExist > 0 then
message_suff:= ' already exist';
end if;
dbms_output.put_line('the user ' || ':userid' || message_suff);
end;
I'm having problems retrieving the time from a date field in a SP. If I run the query:
select TO_CHAR(HOR_HST_ATN,'YYYY/MM/DD HH24:MI:SS') AS HOR_HST_ATN FROM AAM0_DT_RSTCN ;
It returns the date and time ok.
But in the SP, the code:
SELECT FEC_DSD_ATN, FEC_HST_ATN, NOM_SEM_DSD_ATN, NOM_SEM_HST_ATN,
TO_DATE(HOR_DSD_ATN ,'DD/MM/YYYY HH24:MI:SS') as HOR_DSD_ATN,
TO_DATE(HOR_HST_ATN,'DD/MM/YYYY HH24:MI:SS') as HOR_HST_ATN
returns just the date ok but the time is 00:00
If I try the select in the SP with TO_CHAR as in:
SELECT FEC_DSD_ATN, FEC_HST_ATN, NOM_SEM_DSD_ATN, NOM_SEM_HST_ATN,
TO_CHAR(HOR_DSD_ATN ,'YYYY/MM/DD HH24:MI:SS') as HOR_DSD_ATN,
TO_CHAR(HOR_HST_ATN,'YYYY/MM/DD HH24:MI:SS') as HOR_HST_ATN
I get the error: SQLCODE: -6502 SQLERRM: ORA-06502: PL/SQL: error ...
Any clues ? BTW I must use a SP to retrieve this value.
Update: the column is type date. Here's the full SP:
FUNCTION FN_AAM_EV_RSTCN (
I_NUM_CONTRATO IN AAM0_DT_RSTCN.NUM_CONTRATO%TYPE,
I_COD_PFL IN AAM0_DT_RSTCN.COD_PFL%TYPE,
I_COD_APL_PFM IN AAM0_DT_RSTCN.COD_APL_PFM%TYPE,
I_COD_PTO IN AAM0_DT_RSTCN.COD_PTO%TYPE,
I_COD_FNC IN AAM0_DT_RSTCN.COD_FNC%TYPE
) RETURN BOOLEAN
AS
T_FEC_DSD_ATN AAM0_DT_RSTCN.FEC_DSD_ATN%TYPE;
T_HOR_DSD_ATN AAM0_DT_RSTCN.HOR_DSD_ATN%TYPE;
T_FEC_HST_ATN AAM0_DT_RSTCN.FEC_HST_ATN%TYPE;
T_HOR_HST_ATN AAM0_DT_RSTCN.HOR_HST_ATN%TYPE;
T_NOM_SEM_DSD_ATN AAM0_DT_RSTCN.NOM_SEM_DSD_ATN%TYPE;
T_NOM_SEM_HST_ATN AAM0_DT_RSTCN.NOM_SEM_HST_ATN%TYPE;
O_RESULTSET2 REST_REFCUR;
BEGIN
OPEN O_RESULTSET2 FOR
SELECT FEC_DSD_ATN, FEC_HST_ATN, NOM_SEM_DSD_ATN, NOM_SEM_HST_ATN,
TO_CHAR(HOR_DSD_ATN ,'DD/MM/YYYY HH24:MI:SS') as HOR_DSD_ATN,
TO_CHAR(HOR_HST_ATN,'DD/MM/YYYY HH24:MI:SS') as HOR_HST_ATN
FROM AAM0_DT_RSTCN
WHERE ROWNUM <=1
AND NUM_CONTRATO = NVL ( I_NUM_CONTRATO, NUM_CONTRATO )
AND COD_PFL = NVL ( I_COD_PFL, COD_PFL )
AND COD_APL_PFM = NVL ( I_COD_APL_PFM, COD_APL_PFM )
AND COD_PTO = NVL ( I_COD_PTO, COD_PTO )
AND COD_FNC = NVL ( I_COD_FNC, COD_FNC )
AND FLG_RCS = 'D'
AND COD_TPO_CDC = 'PC'
;
FETCH O_RESULTSET2 INTO T_FEC_DSD_ATN, T_FEC_HST_ATN, T_NOM_SEM_DSD_ATN, T_NOM_SEM_HST_ATN, T_HOR_DSD_ATN, T_HOR_HST_ATN ;
IF (O_RESULTSET2%NOTFOUND) THEN
RETURN TRUE;
ELSE
dbms_output.put_line('EVALUO RESTRICCIONES: T_FEC_DSD_ATN ' || T_FEC_DSD_ATN || ' T_FEC_HST_ATN : ' || T_FEC_HST_ATN || ' T_NOM_SEM_DSD_ATN: ' ||
T_NOM_SEM_DSD_ATN || ' T_NOM_SEM_HST_ATN: ' || T_NOM_SEM_HST_ATN || ' T_HOR_DSD_ATN: ' || TO_CHAR(T_HOR_DSD_ATN ,'DD/MM/YYYY HH24:MI:SS') ||
' T_HOR_HST_ATN: ' || TO_CHAR(T_HOR_HST_ATN ,'DD/MM/YYYY HH24:MI:SS') );
IF (NOT((T_FEC_DSD_ATN IS NULL) and (T_FEC_HST_ATN IS NULL)) ) THEN
IF ( NOT ((T_FEC_DSD_ATN <= SYSDATE) AND (T_FEC_HST_ATN >= SYSDATE ))) THEN
RETURN FALSE;
END IF ;
END IF ;
RETURN TRUE;
END IF ;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN TRUE;
WHEN OTHERS THEN
IF O_RESULTSET2%ISOPEN THEN
CLOSE O_RESULTSET2;
END IF;
DBMS_OUTPUT.PUT_LINE('ERROR en FN_AAM_EV_RSTCN : ');
DBMS_OUTPUT.PUT_LINE(' SQLCODE: ' || SQLCODE );
DBMS_OUTPUT.PUT_LINE(' SQLERRM: ' || SQLERRM );
ROLLBACK;
END FN_AAM_EV_RSTCN;
Your problem lies with the following code:
...
T_HOR_DSD_ATN AAM0_DT_RSTCN.HOR_DSD_ATN%TYPE;
T_HOR_HST_ATN AAM0_DT_RSTCN.HOR_HST_ATN%TYPE;
...
SELECT FEC_DSD_ATN, FEC_HST_ATN, NOM_SEM_DSD_ATN, NOM_SEM_HST_ATN,
TO_CHAR(HOR_DSD_ATN ,'DD/MM/YYYY HH24:MI:SS') as HOR_DSD_ATN,
TO_CHAR(HOR_HST_ATN,'DD/MM/YYYY HH24:MI:SS') as HOR_HST_ATN
FROM AAM0_DT_RSTCN
...
FETCH O_RESULTSET2 INTO T_FEC_DSD_ATN, T_FEC_HST_ATN, T_NOM_SEM_DSD_ATN,
T_NOM_SEM_HST_ATN, T_HOR_DSD_ATN, T_HOR_HST_ATN ;
Since the HOR_DSD_ATN and HOR_HST_ATN columns have the DATE data type, your local variables (T_HOR_DSD_ATN and T_HOR_HST_ATN) are also DATEs. However, in your SELECT you are converting these dates to strings with the TO_CHAR function. Therefore, your fetch is effectively doing this (I've made up some dates here):
T_HOR_DSD_ATN := '03/02/2010 09:33:30';
T_HOR_HST_ATN := '01/01/2010 12:30:00';
Since you are assigning a string to a date variable, Oracle must do an implicit TO_DATE - therefore it is using the session's NLS_DATE_FORMAT to convert them, which probably just gets the date portion (e.g. DD/MM/YYYY) and loses the time values.
To fix it, just remove the TO_CHAR()s from your SELECT statement - that way you will get dates + time values unmodified into your local variables.
Since the TO_DATE function takes a VARCHAR as argument, you wouldn't use this function on a DATE field. If you want to retrieve both the date and the time from a DATE field you would just SELECT the fields without functions:
SELECT FEC_DSD_ATN, FEC_HST_ATN, NOM_SEM_DSD_ATN, NOM_SEM_HST_ATN,
HOR_DSD_ATN, HOR_HST_ATN
...
update
The problem is that you are fetching TO_CHAR(HOR_DSD_ATN, 'DD/MM/YYYY HH24:MI:SS') into a DATE field. There is an implicit conversion that truncates your date. Use to_char on a DATE variable and use to_date on a CHAR variable.
My suggestion:
FUNCTION FN_AAM_EV_RSTCN(I_NUM_CONTRATO IN AAM0_DT_RSTCN.NUM_CONTRATO%TYPE,
I_COD_PFL IN AAM0_DT_RSTCN.COD_PFL%TYPE,
I_COD_APL_PFM IN AAM0_DT_RSTCN.COD_APL_PFM%TYPE,
I_COD_PTO IN AAM0_DT_RSTCN.COD_PTO%TYPE,
I_COD_FNC IN AAM0_DT_RSTCN.COD_FNC%TYPE)
RETURN BOOLEAN AS
BEGIN
FOR cc IN (SELECT FEC_DSD_ATN, FEC_HST_ATN, NOM_SEM_DSD_ATN,
NOM_SEM_HST_ATN, HOR_DSD_ATN, HOR_HST_ATN
FROM AAM0_DT_RSTCN
WHERE ROWNUM <= 1
AND NUM_CONTRATO = NVL(I_NUM_CONTRATO, NUM_CONTRATO)
AND COD_PFL = NVL(I_COD_PFL, COD_PFL)
AND COD_APL_PFM = NVL(I_COD_APL_PFM, COD_APL_PFM)
AND COD_PTO = NVL(I_COD_PTO, COD_PTO)
AND COD_FNC = NVL(I_COD_FNC, COD_FNC)
AND FLG_RCS = 'D'
AND COD_TPO_CDC = 'PC') LOOP
dbms_output.put_line(' T_HOR_DSD_ATN: ' ||
TO_CHAR(cc.HOR_DSD_ATN, 'DD/MM/YYYY HH24:MI:SS') ||
' T_HOR_HST_ATN: ' ||
TO_CHAR(cc.HOR_HST_ATN, 'DD/MM/YYYY HH24:MI:SS'));
-- your logic with return boolean
END LOOP;
-- no rows found
RETURN TRUE;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('ERROR en FN_AAM_EV_RSTCN : ');
DBMS_OUTPUT.PUT_LINE(' SQLCODE: ' || SQLCODE);
DBMS_OUTPUT.PUT_LINE(' SQLERRM: ' || SQLERRM);
RAISE;
END FN_AAM_EV_RSTCN;
If HOR_HST_ATN is a date column then TO_DATE(HOR_HST_ATN,'DD/MM/YYYY HH24:MI:SS') must be a typo. You probably mean TO_CHAR().
If the columns were populated by dates without a time element Oracle defaults the time to midnight...
SQL> create table t23 (d date)
2 /
Table created.
SQL> insert into t23 values (sysdate)
2 /
1 row created.
SQL> insert into t23 values (to_date('03-FEB-2010', 'DD-MON-YYYY'))
2 /
1 row created.
SQL> select to_char(d, 'DD/MM/YYYY HH24:MI:SS') as datetime from t23
2 /
DATETIME
-------------------
02/02/2010 17:59:51
03/02/2010 00:00:00
SQL>