Oracle Trigger to loop and update field based on sysdate - oracle

I would like to make a trigger that can update a field (‘STATUS’) to Inactive when a vaccine is past its expiration date (‘EXPIRATION_DATE’).
Here is the current table structure:
**CREATE TABLE WAREHOUSE.VACCINE_INVENTORY (
VACCINE VARCHAR2(200 BYTE) NOT NULL,
RECEIPT_DATE DATE NOT NULL,
CONTAINER_SIZE VARCHAR2(200 BYTE),
QUANTITY NUMBER(6, 0),
REQUISITION NUMBER(6, 0),
FISCAL_YEAR NUMBER(4, 0),
RECEIVED_BY VARCHAR2(50 BYTE),
EXPIRATION_DATE DATE,
LOT_NUMBER VARCHAR2(30 BYTE) NOT NULL,
VENDOR VARCHAR2(200 BYTE),
STATUS VARCHAR2(10 BYTE),
CATALOG_NUMBER NUMBER(5, 0),
CONSTRAINT PK_VACC PRIMARY KEY (VACCINE, RECEIPT_DATE, LOT_NUMBER) USING INDEX TABLESPACE WAREHOUSE STORAGE**
So the idea is that if SYSDATE > EXPIRATION_DATE the expiration date should be changed from Active to Inactive.
I would assume I would need to create a Loop so that when the trigger is run, it will loop through all the records in the table and set each expired vaccine to Inactive.
I would greatly appreciate any help…
Thanks,
Matthew

Create an additional view to provide fields that depend on table columns and sysdate, e.g.
CREATE TABLE VACCINE_INVENTORY(VACCINE VARCHAR2(200 BYTE) NOT NULL,
-- ---
EXPIRATION_DATE DATE NOT NULL
-- ...
);
CREATE view VACCINE_INVENTORY_VW AS
select v.*,
case
when v.expiration_date < sysdate then
'I'
else
'A'
end as status
from VACCINE_INVENTORY v;

Related

Liquibase - envers inserts data in audit table

I am developing a spring-boot (2.4.5) application with envers to audit data. When I am using the app from scratch, all is fine. From app, I can save data in database tables, and envers is storing audit data in the audit tables.
To database CI/CD, I have configured Liquibase, executing a changeset. The database is created without any issue.
But my problem is when I want to add inserts sentences in a changeset to create initial data. How can I add these initial data to audit tables?
My change set:
-- changeset puser:1659003040757-1
CREATE SEQUENCE HIBERNATE_SEQUENCE START WITH 1 MAXVALUE 9999999999999999999999999999;
-- changeset puser:1659003040757-2
--preconditions onFail:WARN onError:WARN
--precondition-sql-check expectedResult:0 SELECT count(*) FROM USER_TABLES WHERE TABLE_NAME = 'AUDIT_REVISION_ENTITY';
CREATE TABLE AUDIT_REVISION_ENTITY
(
ID NUMBER(10, 0) NOT NULL,
TIMESTAMP NUMBER(19, 0) NOT NULL,
IP_ADDRESS VARCHAR2(255 CHAR),
USERNAME VARCHAR2(255 CHAR),
CONSTRAINT SYS_C0017210 PRIMARY KEY (ID)
) TABLESPACE DATAAPP;
-- changeset puser:1659003040757-3
CREATE TABLE USERS
(
ID RAW(16) NOT NULL,
CREATED_BY VARCHAR2(255 CHAR),
CREATED_DATE TIMESTAMP(6) NOT NULL,
MODIFIED_BY VARCHAR2(255 CHAR),
MODIFIED_DATE TIMESTAMP(6),
ACTIVE NUMBER(1, 0),
EMAIL VARCHAR2(200 CHAR) NOT NULL,
USER_LOGIN VARCHAR2(20 CHAR) NOT NULL,
USER_NAME VARCHAR2(200 CHAR) NOT NULL,
USER_PASSWORD VARCHAR2(256 CHAR) NOT NULL,
USER_ROLE VARCHAR2(50 CHAR) NOT NULL,
USER_SURNAME VARCHAR2(200 CHAR) NOT NULL,
CONSTRAINT SYS_C0017439 PRIMARY KEY (ID)
) TABLESPACE DATAAPP;
-- changeset puser:1659003040757-4
CREATE TABLE USERS_AUD
(
ID RAW(16) NOT NULL,
REV NUMBER(10, 0) NOT NULL,
REVTYPE NUMBER(3, 0),
CREATED_BY VARCHAR2(255 CHAR),
CREATED_DATE TIMESTAMP(6),
MODIFIED_BY VARCHAR2(255 CHAR),
MODIFIED_DATE TIMESTAMP(6),
ACTIVE NUMBER(1, 0),
EMAIL VARCHAR2(200 CHAR),
USER_LOGIN VARCHAR2(20 CHAR),
USER_NAME VARCHAR2(200 CHAR),
USER_PASSWORD VARCHAR2(256 CHAR),
USER_ROLE VARCHAR2(50 CHAR),
USER_SURNAME VARCHAR2(200 CHAR),
CONSTRAINT SYS_C0017442 PRIMARY KEY (ID, REV)
) TABLESPACE DATAAPP;
-- changeset puser:1659003040757-5
ALTER TABLE USERS_AUD
ADD CONSTRAINT FKLD7CDNHID45YC6535CECSHYOP FOREIGN KEY (REV) REFERENCES AUDIT_REVISION_ENTITY (ID);
-- changeset puser:1659003040757-6
INSERT INTO users (id, user_surname, user_name, user_login, user_password, user_role, email, created_by, created_date, active)
VALUES ('cca76b85f24d490b8df7fd8d91743835', 'APPUSER', 'APPUSER', 'APPUSER', '$2a$10$9wXu9hshOrtZ7RopythgF.XP93XbKtISBzv6QjTGBzq', 'ADMIN', 'app#appsuser.com', 'System', current_date, 1);
My question is how do the changeset to insert data in envers audit table?
INSERT INTO users_aud ???????????????????????????????```
Thanks to #Neeraj's comment, I could finally fix with three inserts.
INSERT INTO users (id, user_surname, user_name, user_login, user_password, user_role, email, created_by, created_date, active)
VALUES ('cca76b85f24d490b8df7fd8d91743835', 'user', 'user', 'user', '$TGBzq', 'ADMIN', 'user#company.com', 'system', current_date, 1);
INSERT INTO AUDIT_REVISION_ENTITY (TIMESTAMP, IP_ADDRESS, USERNAME, ID)
VALUES (1659347410125, '0.0.0.0', 'System', HIBERNATE_SEQUENCE.NEXTVAL);
INSERT INTO USERS_AUD (ID, CREATED_BY, CREATED_DATE, MODIFIED_BY, MODIFIED_DATE, ACTIVE, USER_SURNAME, USER_NAME, USER_LOGIN, USER_PASSWORD, USER_ROLE, EMAIL, REV, REVTYPE)
SELECT 'cca76b85f24d490b8df7fd8d91743835',
'user',
current_date,
'user',
current_date,
1,
'user',
'user',
'user',
'$2jTGBzq',
'ADMIN',
'user#company.com',
rev,
0
FROM (SELECT MAX(ID) AS REV FROM AUDIT_REVISION_ENTITY);
For a few number of inserts, this is a quick solution; for a large amount of data, I think that using a custom changeset is better solution.

Oracle ORA-04063: table "TABLE" has errors

I Create a table with UDT type as a column , after some time i needed one more column to add the type, i altered the type using
alter type type_cust_instatus add attribute (sub_status_to varchar2(20)) invalidate
alter type type_cust_instatus compile
After that the table becomes invalid and tried to make the column unused and dropped the the type also and recreaded once again as per before modification.
now in the table defination there is no type fro the column and showing as blank
how to delete the column or recompile
CREATE TABLE KAMSIT.CUSTOMER_TRANSACTION_DATA
(
MSISDN VARCHAR2(13 BYTE),
IMSI VARCHAR2(15 BYTE),
CONNECTION_TYPE NUMBER,
MSTYPE VARCHAR2(6 BYTE),
SUBSCR_NO VARCHAR2(10 BYTE),
ACCOUNT_NO VARCHAR2(10 BYTE),
ALUIN_ACTIVE_DATE DATE,
FIRST_CALL_MADE_TIME DATE,
ACTIVE_DATE DATE,
INACTIVE_DATE DATE,
BILLING_IMSI VARCHAR2(15 BYTE),
CURRENT_STATUS VARCHAR2(1 BYTE) DEFAULT 'C',
ALUIN_IMSI VARCHAR2(15 BYTE),
ALUIN_SUB_STATUS VARCHAR2(50 BYTE),
INSERT_DATE DATE DEFAULT sysdate,
TRANSACTION_TYPE KAMSIT.CUSTOMER_TRANSACTION_TYPE,
TRANSACTION_IMSI KAMSIT.CUST_IMSI_CHANGE,
ALUIN_STATUS VARCHAR2(1 BYTE) DEFAULT 'C',
IMEI VARCHAR2(20 BYTE),
TRANSACTION_INSTATUS
)
NESTED TABLE TRANSACTION_TYPE STORE AS TBL_CUSTOMER_TRANSACTIONS,
NESTED TABLE TRANSACTION_IMSI STORE AS TBL_TRANSACTION_IMSI
how to drop the column or set unused or rebuild the table

Not enough values while trying to insert data from table 1 to table 2

I want to add one column in table but I get error that table needs to be empty.
I create backup table using following command
CREATE TABLE attachments_backup
AS
SELECT * FROM attachments
When I create a backup table I insert data from attachments to attachments_backup
INSERT INTO attachments SELECT * FROM attachments_backup
Then I delete data from table 1 (attachments)
DELETE FROM attachments
I add column which I need to add in table attachment
ALTER TABLE attachments
ADD ChildrenDirectory VARCHAR(50) DEFAULT NULL
Right here problem occure. Since I add new column right now I can insert data from table 2 (attachment_backup) to table 1 (attachments)
I try something like this
INSERT INTO attachments VALUES (AtaId,Belongs_To,ClientId,File_path,Id,Image_Path,Lock_,Name,Project_Id,Type,Category) SELECT * FROM attachments_backup
But this solution doesn't work since I get error message "Not enought values"
SQL Error: ORA-00947: not enough values
00947. 00000 - "not enough values"
Does anyone now how to solve this kind of issue since I have no more idea ?
Attachments table
ATAID NUMBER(10,0)
BELONGS_TO NUMBER(10,0)
CLIENTID NUMBER(10,0)
FILE_PATH VARCHAR2(255 CHAR)
ID NUMBER(10,0)
IMAGE_PATH VARCHAR2(255 CHAR)
ISDELETED NUMBER(10,0)
LOCK_ CHAR(1 CHAR)
NAME VARCHAR2(255 CHAR)
PROJECT_ID NUMBER(10,0)
TYPE VARCHAR2(4000 CHAR)
CATEGORY VARCHAR2(20 BYTE)
CHILDRENDIRECTORY VARCHAR2(50 BYTE)
Attachments_backup
ATAID NUMBER(10,0)
BELONGS_TO NUMBER(10,0)
CLIENTID NUMBER(10,0)
FILE_PATH VARCHAR2(255 CHAR)
ID NUMBER(10,0)
IMAGE_PATH VARCHAR2(255 CHAR)
ISDELETED NUMBER(10,0)
LOCK_ CHAR(1 CHAR)
NAME VARCHAR2(255 CHAR)
PROJECT_ID NUMBER(10,0)
TYPE VARCHAR2(4000 CHAR)
CATEGORY VARCHAR2(20 BYTE)
Basically, you're doing it wrong.
Except for quick and dirty selects, don't use asterisk *. Something like this:
insert into a select * from b
just waits for error to happen. Always name all columns involved. Yes, it requires some more typing, but is safe. So:
insert into a (empno, ename, job, sal)
select empno, ename, job, sal
from b;
Welcome,
You can add new column to an existing table using an alter command, even if the table is not empty
To insert from table "A" to table "B" or vice-versa, you need to have exact number of columns to perform "select * " type insert

add interval monthly partition

I want to create this table, with a monthly partition on the endTime column.
I mean each month a partition added automatically by oracle.
create table T_CALLSESSION() PARTITON BY RANGE (C_ENDTIME )
INTERVAL(NUMTOYMINTERVAL(1,'month'); (
C_ID NUMBER(34, 0) not null,
C_ENDTIME timestamp not null,
C_STARTTIME timestamp not null,
C_TYPE number(10,0) not null,
F_CREATOR NUMBER(34, 0),
F_MESSAGE_THREAD NUMBER(34, 0),
primary key (C_ID)
);
is that works?
There are a few mistakes in your code.
Table name should not contain the parenthesis ()
The PARTITION clause must be after the declaration of columns and constraints.
You must use the INTERVAL partition so that new partitions are automatically created.
One partition must be created with some constant values and then after other partitions will be automatically created.
Use the following code:
create table T_CALLSESSION (
C_ID NUMBER(34, 0) not null,
C_ENDTIME timestamp not null,
C_STARTTIME timestamp not null,
C_TYPE number(10,0) not null,
F_CREATOR NUMBER(34, 0),
F_MESSAGE_THREAD NUMBER(34, 0),
primary key (C_ID)
) PARTITION BY RANGE (C_ENDTIME)
INTERVAL(NUMTOYMINTERVAL(1, 'MONTH'))
(
PARTITION T_CALLSESSION_P1 VALUES LESS THAN (TO_DATE('01-06-2020', 'DD-MM-YYYY'))
);

Adding reference partitioning to oracle table

on the 12c version of oracle I have created T_App table like this:
create table T_APP
(
C_ID NUMBER(34, 0) not null,
C_ACTIVE number(1, 0) not null,
C_APPID varchar2(255 char) not null,
C_CONTACTTYPE varchar2(255 char) not null,
primary key (C_ID)
)
partition by list(C_CONTACTTYPE)
(
partition p1 values ('default')
);
and T_APPUSER table like this:
create table T_APPUSER
(
C_ID NUMBER(34, 0) primary key,
C_ACTIVE number(1, 0) not null,
C_DEVICETOKEN varchar2(255 char),
C_SSOID varchar2(255 char),
F_APP NUMBER(34, 0) not null,
constraint FK_APP
foreign key (F_APP)
references T_APP
)
PARTITION BY REFERENCE (FK_APP);
but when I run the query of creating T_APPUSER I get ERROR!
is there anyThing wrong with my query for creating the T_APPUSER table?

Resources