How to create a before or after Trigger that can catch DUP_VAL_ON_INDEX on Oracle PL/SQL - oracle

I have a table like this
KD_C KD_PLA KD_T GABUNG BERAKHIR GAJI_PL BUYOUT
---- ------ ---- ---------- ---------- ---------- ----------
C001 MA001 T006 2003 2014 50000 5200000
C002 SC001 T006 2012 2016 65000 20280000
C003 TW001 T006 2005 2018 90000 46800000
C004 TV001 T006 2008 2017 60000 24960000
C005 PC001 T001 2003 2016 80000 24960000
C006 AC001 T001 1996 2014 90000 9360000
C007 DB001 T001 2010 2016 65000 20280000
C008 EH001 T001 2011 2018 85000 44200000
C009 JC001 T002 1996 2014 60000 6240000
C010 SG001 T002 1998 2016 87000 27144000
C011 LS001 T002 2010 2018 81000 42120000
C012 PR001 T002 2004 2016 60000 18720000
C013 JH001 T003 2005 2018 72000 37440000
C014 GC001 T003 2003 2015 65000 13520000
C015 ED001 T003 2010 2018 100000 52000000
C016 GB001 T003 2010 2016 80000 24960000
C017 DG001 T004 2011 2018 73000 37960000
C018 RG001 T004 1992 2014 90000 9360000
C019 PJ001 T004 2011 2018 80000 41600000
C020 RP001 T004 2012 2017 92000 38272000
C021 GB002 T005 2006 2018 102000 53040000
C022 EA001 T005 2011 2015 70000 14560000
C023 HL001 T005 2012 2018 65000 33800000
C024 KW001 T005 2009 2017 67000 27872000
C025 MA001 T005 2017 2022 50000 26000000
C028 MA001 T001 2016 2018 15000 3120000
C029 MA001 T001 2016 2018 15000 3120000
C030 MA001 T001 2016 2018 15000 3120000
And then I tried to make a Trigger for updating instead of insert to the table when duplicate a primary key. The primary key is the first coloumn 'KD_CONTRACT'.
CREATE OR REPLACE TRIGGER INSERT_TABEL_CONTRACT
BEFORE INSERT ON CONTRACT
FOR EACH ROW
DECLARE
KODE VARCHAR2(20);
TEMPCARIKODE NUMBER;
BEGIN
SELECT LPAD(TO_NUMBER(NVL(SUBSTR(MAX(KD_CONTRACT),2),0))+1,3,'0') INTO KODE
FROM CONTRACT;
SELECT COUNT(KD_CONTRACT) INTO TEMPCARIKODE
FROM CONTRACT
WHERE KD_CONTRACT = :NEW.KD_CONTRACT;
IF(TEMPCARIKODE = 0) THEN
:NEW.KD_CONTRACT := 'C'||KODE;
:NEW.GABUNG := TO_NUMBER(TO_CHAR(SYSDATE,'YYYY'));
:NEW.BERAKHIR := :NEW.GABUNG + :NEW.BERAKHIR;
:NEW.BUYOUT := (:NEW.GAJI_PL * 52) * (:NEW.Berakhir-:NEW.Gabung) * 2;
END IF;
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
DBMS_OUTPUT.PUT_LINE('');
DBMS_OUTPUT.PUT_LINE('');
DBMS_OUTPUT.PUT_LINE('ERROR at Line 1:');
DBMS_OUTPUT.PUT_LINE('ORA-20003: Data sudah ada, data akan diupdate!');
END;
/
show err;
anyway maybe the code show that I tried not to update when the primary key is duplicated. But please the until end first.
it show no error... So the trigger successfully created.
then I tried to insert a duplicated primary key.
INSERT INTO CONTRACT VALUES('C001','MA001','T001',2013,2,15000,2500000);
the result
INSERT INTO CONTRACT VALUES('C001','MA001','T001',2013,2,15000,2500000)
*
ERROR at line 1:
ORA-00001: unique constraint (TUGAS.PK_CONTRACT) violated
It did raise ORA-00001 but it never touched the DUP_VAL_ON_INDEX and showed nothing or the custom message.
Anyone have Idea? When it never even touch DUP_VAL_ON_INDEX then how could I event paste an update query at the when case...
Thanks...
* anyway my oracle is XE or express edition.

The EXCEPTION block from your trigger will never be executed since your trigger doesn't contain any INSERT or UPDATE statements.
You have to consider INSTEAD OF Trigger instead.

Related

oracle Unable to create AWR report

oracle version: 11.2.0.1.0
When I try to generate an AWR Report in Oracle use sysdba,
exec dbms_workload_repository.create_snapshot();
I keep getting this error:
ORA-00600: [kewrose_1], [600], [ORA-00600: [13013], [5001], [6213], [8465936], [5], [8447794], [17], [], [], [], [], []
], [], [], [], [], [], [], [], [], []
I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this
conn / as sysdba
SQL> #$ORACLE_HOME/rdbms/admin/awrrpt.sql
SQL> #$ORACLE_HOME/rdbms/admin/awrrpt.sql
Current Instance
DB Id DB Name Inst Num Instance
----------- ------------ -------- ------------
3400239050 TEP 1 TEP
Specify the Report Type
AWR reports can be generated in the following formats. Please enter the
name of the format at the prompt. Default value is 'html'.
'html' HTML format (default)
'text' Text format
'active-html' Includes Performance Hub active report
Enter value for report_type: dec
Type Specified: dec
Instances in this Workload Repository schema
DB Id Inst Num DB Name Instance Host
------------ -------- ------------ ------------ ------------
* 3400239050 1 TEPCBC TEP oldman
Using 3400239050 for database Id
Using 1 for instance number
Specify the number of days of snapshots to choose from
Entering the number of days (n) will result in the most recent
(n) days of snapshots being listed. Pressing without
specifying a number lists all completed snapshots.
Entering the number of days (n) will result in the most recent
(n) days of snapshots being listed. Pressing without
specifying a number lists all completed snapshots.
Enter value for num_days: 1
Listing the last day's Completed Snapshots
Snap
Instance DB Name Snap Id Snap Started Level
TEP TEP 38542 20 Dec 2022 00:30 1
38543 20 Dec 2022 01:30 1
38544 20 Dec 2022 02:30 1
38545 20 Dec 2022 03:30 1
38546 20 Dec 2022 04:30 1
38547 20 Dec 2022 05:30 1
38548 20 Dec 2022 06:30 1
38549 20 Dec 2022 07:30 1
38550 20 Dec 2022 08:30 1
38551 20 Dec 2022 09:30 1
38552 20 Dec 2022 10:30 1
38553 20 Dec 2022 11:30 1
38554 20 Dec 2022 12:30 1
38555 20 Dec 2022 13:30 1
38556 20 Dec 2022 14:30 1
38557 20 Dec 2022 15:30 1
38558 20 Dec 2022 16:30 1
38559 20 Dec 2022 17:30 1
38560 20 Dec 2022 18:30 1
38561 20 Dec 2022 19:30 1
38562 20 Dec 2022 20:30 1
Specify the Begin and End Snapshot Ids
Enter value for begin_snap: 38542
Begin Snapshot Id specified: 38542
Enter value for end_snap: 38544
End Snapshot Id specified: 38544
Specify the Report Name
~~~~~~~~~~~~~~~~~~~~~~~
The default report file name is awrrpt_1_38542_38544.html. To use this name,
press <return> to continue, otherwise enter an alternative.
Enter value for report_name: test2
Using the report name test2
<html lang="en"><head><title>AWR Report for DB: TEP, Inst: TEP, Snaps: 38542-38544</title>
<style type="text/css">
body.awr {font:bold 10pt Arial,Helvetica,Geneva,sans-serif;color:black; background:White;}

Storing a date as a variable and using it in code (Oracle)

Oracle Version: 19.2.1.247
Good morning,
I am trying to get the most recent Friday and then store that date to use as a variable. I need to only bring forward those people who have a start date of 28 days or earlier from "my_date". Anyone over 28 days should be excluded.
here is what I have so far:
variable my_date varchar2(30)
exec :my_date := sysdate;
SELECT Distinct * from tbl1.people
where startdt < to_date(:my_date,(select next_day(trunc(sysdate-1), 'FRIDAY'-7 from dual))-35;
I am getting the following error message and I am not sure how to handle it:
ORA-00932: inconsistent datatypes: expected date got date
Here is an example of what I am trying to do:
Last Friday (Oct 23, 2020) is "my_date", so I need to go backwards for 28 days to Sept 25, 2020 and use that as the cutoff date. I do not want to see anyone with Sept 24,2020 or earlier.
The result set should be from Sept 25, 2020 to Oct 23, 2020 only.
Thank you for any help you can give!
To me, it looks as simple as
select *
from your_table
where startdt < next_day(sysdate - 7, 'FRIDAY') - 28;
because
SQL> select next_day(sysdate - 7, 'FRIDAY') - 28 from dual;
NEXT_DAY(S
----------
25.09.2020
SQL>

Working with a date column that corresponds to values in another column in SAS

I would like to add on to this code so that the value of the last measurement according to the "date" column in the "measurement" column is divided by the lowest value recorded in the measurement column and the result forms a new column. The current working code adds a column that subtracts the initial measurements from the other measurements in the "measurement" column if the "subject", "type" and "procedure" columns match. Thank you in advance.
data have;
input Subject Type Date $ 5-12 Procedure $ 15-22 Measurement;
datalines;
500 Initial 15 AUG 2017 Invasive 20
500 Initial 18 SEPT 2018 Surface 35
500 Followup 12 SEPT 2018 Invasive 54
428 Followup 2 JUL 2019 Outer 29
765 Seventh 3 JUL 2018 Other 13
500 Followup 6 NOV 2018 Surface 98
428 Initial 23 FEB 2018 Outer 10
765 Initial 20 AUG 2019 Other 19
610 Third 21 AUG 2018 Invasive 66
610 Initial 27 Mar 2018 Invasive 17
;
data want (drop=rc _Measurement);
if _N_ = 1 then do;
declare hash h (dataset : "have (rename=(Measurement=_Measurement) where=(Type='Initial'))");
h.definekey ('Subject');
h.definedata ('_Measurement');
h.definedone();
end;
set have;
_Measurement=.;
if Type ne 'Initial' then rc = h.find();
NewMeasurement = ifn(Measurement=., ., sum (Measurement, -_Measurement));
run;

oracle query to convert number format to date format or vice versa

In DB, date is stored in number format (Mon Jul 07 14:41:40 IST 2014
is stored as 1404724300383), I need a query to convert number to date to compare with the sysdate, or from sysdate to number format. How can we convert the date to number or vice versa?
Assume the number is a sequencial one based on specific rule. You need to know the rule first.
Get the rule
a) 07 July 2014 = 1404724300383; 07 July 2013 = ? (assume 1404700000000)
b) Assume value 0 mapping to one day, Day1
c) two formulas 1404724300383 = (07 July 2014 - Day1) * RULE; 1404700000000 = (07 July 2013 - Day1)*Rule;
Then, Rule = 24300383/365 and Day 1 = 1 Jan 1900 + 41285/(1404724300383/1404700000000*41460/((1404724300383/1404700000000)-1))
where 41825 is the days between 07 July 2014 and 1 Jan 1900)
41460 is the days between 07 July 2013 and 1 Jan 1900)
Use the Rule and Day1 to get what you need
select Day1 + [query column]/Rule from Your_Table;

i want Oracle Query for Formatted Output

Following is my table deign
Ecode Degree YOQ
7654321 SSC 2000
7654321 HSC 2002
7654321 Bcom 2006
7654321 MCA 2010
7654322 SSC 2002
7654322 HSC 2004
7654322 Bcom 2007
7654323 SSC 2002
7654323 HSC 2004
7654323 BE 2008
i want following formatted output using oracle query
Ecode Degree year
7654321 SSC,HSC,Bcom,MCA 2000,2002,2006,2010
7654322 SSC,HSC,Bcom 2002,2004,2007
7654323 SSC,HSC,BE 2002,2004,2008
Thanks in Advance
Vivek shah
SELECT ecode,wm_concat(DEGREE),wm_concat(yoQ) FROM TABLE_NAME GROUP BY ecode

Resources