ORA-00942 on INSERT to a newly created table - oracle

I'm doing an evaluation of FLYWAY and running a very simple script that creates a new table and insert a row to that table. The table creates successfully but I get an ORA-942 error on the INSERT. I have tried every permutation I can think of using upper/lower case and quoted and unquoted schema and table names. I have also tried separating out the INSERT statement into different script from the CREATE statement - all to no avail. Can anyone help explain the format flyway needs for the INSERT statements?
Here are the statements from my most recent attempt. I have tried the same statements with quotes around the table_name, with all lower case, with no quotes around the schema - nothing works. Logged directly into the database as SYS, I can describe the table with this command:
desc "FLYWAY_USER".department
CREATE TABLE "FLYWAY_USER".DEPARTMENT
( DEPARTMENT_id NUMBER(3) NOT NULL,
DEPARTMENT VARCHAR2(64) NOT NULL,
display_name VARCHAR2(64) NOT NULL,
description VARCHAR2(400),
create_date DATE NOT NULL,
update_date DATE NOT NULL,
created_by VARCHAR2(80) NOT NULL,
updated_by VARCHAR2(80) NOT NULL )
TABLESPACE users;
INSERT INTO "FLYWAY_USER".DEPARTMENT
( DEPARTMENT_id,
DEPARTMENT,
display_name,
description,
create_date,
update_date,
created_by,
updated_by )
VALUES
( ( SELECT Nvl( Max(DEPARTMENT_id), 0) + 1 FROM DEPARTMENT ),
'HUMAN_RESOURCES',
'Human Resources',
'The best place to eat smores or get a raise.',
Sysdate,
Sysdate,
'AT09001',
'AT09001' );
Thanks

Logged directly into the database as SYS
Use better a dedicated application user.
Anyway if you use a different user that the schema owner of your objects (FLYWAY_USER) you must qualify ALL your references.
especially
VALUES
( ( SELECT Nvl( Max(DEPARTMENT_id), 0) + 1 FROM DEPARTMENT ),
should be
VALUES
( ( SELECT Nvl( Max(DEPARTMENT_id), 0) + 1 FROM FLYWAY_USER.DEPARTMENT ),

Related

"ORA-00001: unique constraint (constraint_name) violated" Error even though I have NOT EXISTS check

I am trying to insert some values in the table through the application and get the issue "ORA-00001: unique constraint (constraint_name) violated". I have the below table:
CREATE TABLE EMPLOYEE
(
EMP_ID VARCHAR2(32) NOT NULL,
NAME VARCHAR2(30) NOT NULL,
TIME TIMESTAMP(3),
PRIMARY KEY(EMP_ID)
);
I have below statement in procedure and it is failing at this INSERT statement even though I have NOT EXISTS check before inserting:
INSERT INTO EMPLOYEE
(EMP_ID, NAME, TIME)
( SELECT v_empid ,
sys_context('USERENV','SID'),
SYSTIMESTAMP+INTERVAL '10' SECOND
FROM DUAL
WHERE NOT EXISTS ( SELECT 1
FROM EMPLOYEE
WHERE EMP_ID = v_empid) );
The above procedure is getting invoked from multiple services almost at the same time, Is there something issue with the parallel transactions like multiple sessions are trying to insert into the same table?
Any help is appreciated in this, thanks in advance.
To me it looks like 2 sessions are trying to insert same user at the same time.
Here how I reproduced it:
Step 1. Session A:
INSERT INTO test_EMPLOYEE(EMP_ID, NAME, TIME)
(SELECT 123, sys_context('USERENV','SID'), SYSTIMESTAMP+INTERVAL '10' SECOND
FROM DUAL
WHERE NOT EXISTS ( SELECT 1 FROM test_EMPLOYEE WHERE EMP_ID = 123));
Step 2. Session B:
-- same query as Session A
INSERT INTO test_EMPLOYEE(EMP_ID, NAME, TIME)
(SELECT 123, sys_context('USERENV','SID'), SYSTIMESTAMP+INTERVAL '10' SECOND
FROM DUAL
WHERE NOT EXISTS ( SELECT 1 FROM test_EMPLOYEE WHERE EMP_ID = 123));
Step 3. Session A:
commit;
Voila: session B fails with constraint violation
So I believe it's users or something outside of database causing the error

Oracle rename table to temp table, create new, then move data from temp to new table

I have a table named MESSAGE_ID:
MESSAGE_ID VARCHAR2(36 BYTE) PRIMARY KEY
DATE TIMESTAMP(6)
STATUS VARCHAR2(200 BYTE)
I want to create a new table to add a new field which should be the primary key.
alter table MESSAGE_ID rename to MESSAGE_ID_OLD;
CREATE TABLE MESSAGE_ID
(
MY_ID NUMBER NOT NULL,
MESSAGE_ID VARCHAR2(36) NOT NULL,
DATE TIMESTAMP,
STATUS VARCHAR2(200),
PRIMARY KEY(MY_ID)
);
Now I want to take everything from the MESSAGE_ID_OLD and insert it into the new table, however I need to put some thing into MY_ID, some random number or something.
This is what I have, but it gives me error:
INSERT INTO MESSAGE_ID (MY_ID, MESSAGE_ID, DATE, STATUS)
(SELECT MY_SEQUENCE.nextval from dual), (SELECT MESSAGE_ID, DATE, STATUS FROM MESSAGE_ID_OLD));
Then I want to delete the old table:
DROP TABLE MESSAGE_ID_OLD;
The Insert statement you are using is not correct.
It should be
INSERT INTO MESSAGE_ID (
MY_ID
,MESSAGE_ID
,DATE_t
,STATUS
)
SELECT MY_SEQUENCE.nextval AS my_id
,MESSAGE_ID
,DATE_t
,STATUS
FROM MESSAGE_ID_OLD;
Also, change the column "DATE" to something else meaningful, as it's a reserved keyword.

Error: ora-00917: missing comma

Create table A_15006977.vehicle. (
Vin varchar(20) primary key,
Vehicle_type char(20) not null,
Mileage number(20) not null,
Manufacturer char(20) not null
);
Insert all
Into A_15006977.vehicle(vin,vehicle_type,mileage,manufacturer)
values ('tf1bb2ve533093891','panel van',18 325,'man')
A_15006977.vehicle(vin,vehicle_type,mileage,manufacturer)
values
('tf1bb2ve533093822','standard van',79 885,'ford')
Select * from dual;
Create table A_15006977.vehicle (
Vin varchar(20) CONSTRAINT vehicle__vin__pk PRIMARY KEY,
Vehicle_type char(20) CONSTRAINT vehicle__vehicle_type__nn not null,
Mileage number(20) CONSTRAINT vehicle__mileage__nn not null,
Manufacturer char(20) CONSTRAINT vehicle__manufacturer__nn not null
);
Insert all
Into A_15006977.vehicle(vin,vehicle_type,mileage,manufacturer)
VALUES ( 'tf1bb2ve533093891', 'panel van', 18325, 'man' )
INTO A_15006977.vehicle (vin,vehicle_type,mileage,manufacturer)
values ( 'tf1bb2ve533093822', 'standard van', 79885, 'ford' )
SELECT 1 FROM DUAL;
Or:
Insert Into A_15006977.vehicle( vin,vehicle_type,mileage,manufacturer )
SELECT 'tf1bb2ve533093891','panel van', 18325, 'man' FROM DUAL UNION ALL
SELECT 'tf1bb2ve533093822','standard van', 79885, 'ford' FROM DUAL;
Note:
You had an extra . after the table name in the DDL statement and spaces in the mileage (18 325 and 79 885) which need removing and you needed an INTO keyword before the second insert.
It is also useful to name your constraints (then you can easily determine which constraint has been violated in later statements).

Check Constraint For A Defaulting Value

I'm stuck and can't figure out how to add the condition to the CHECK constraint in oracle...
Basically I have a table with the following structure:
CREATE TABLE TEST_TBL
(
COL_1 VARCHAR2(100) NOT NULL,
COL_2 VARCHAR2(100) NOT NULL,
COL_3 VARCHAR2(100) NOT NULL,
COL_4 VARCHAR2(100) NOT NULL,
DEF_COL CHAR(1) DEFAULT 'Y',
CONSTRAINT def_check_const
CHECK (???????)
);
There may be multiple values in this table, however I require that only 'Y' or 'N' can be entered in the DEF_COL column. Also i want to add a constraint to restrict the number of 'Y' in the DEF_COL column to only one. There may be multiple 'N' entries allowed for DEF_COL column. Please could someone guide me as to what must be done to add the constraint in the check section of the create table query?
Thanks in advance....
Oracle Setup:
CREATE TABLE TEST_TBL(
COL_1 VARCHAR2(100) NOT NULL,
COL_2 VARCHAR2(100) NOT NULL,
COL_3 VARCHAR2(100) NOT NULL,
COL_4 VARCHAR2(100) NOT NULL,
DEF_COL CHAR(1) DEFAULT 'Y'
CONSTRAINT def_check_const
CHECK ( DEF_COL IN ('Y', 'N' ) )
);
CREATE UNIQUE INDEX def_col_only_one_y__U
ON TEST_TBL( CASE DEF_COL WHEN 'Y' THEN 'Y' END );
Insert a row:
INSERT INTO TEST_TBL VALUES ( 'A1', 'A2', 'A3', 'A4', 'Y' );
1 rows inserted.
Insert a second column with DEF_COL = 'Y':
INSERT INTO TEST_TBL VALUES ( 'B1', 'B2', 'B3', 'B4', 'Y' )
Error report -
SQL Error: ORA-00001: unique constraint (TEST.DEF_COL_ONLY_ONE_Y__U) violated
00001. 00000 - "unique constraint (%s.%s) violated"
*Cause: An UPDATE or INSERT statement attempted to insert a duplicate key.
For Trusted Oracle configured in DBMS MAC mode, you may see
this message if a duplicate entry exists at a different level.
*Action: Either remove the unique restriction or do not insert the key.
Update:
To have only one Y for each unique combination of 3 columns then try:
CREATE UNIQUE INDEX C1_2_3__def_col_only_one_y__U
ON TEST_TBL( COL1, COL2, COL3, CASE DEF_COL WHEN 'Y' THEN 'Y' END );

handling exception for insert into select from

SCHEMA 1
I have table transaction table
create table TXN_HEADER
(
txn_id NUMBER(10) not null,
txn_Date date
product_id NUMBER(10),
company_id NUMBER(10),
dealer_id NUMBER(10),
tran_amt number(10,2)
)
The above table having foreign key references to product.product_id and company.company_id.
This table having 5m rows
SCHEMA 2
create table TXN_HEADER_REPORTS
(
txn_id NUMBER(10) not null,
txn_Date date
product_id NUMBER(10),
company_id NUMBER(10),
dealer_id NUMBER(10),
tran_amt number(10,2)
)
here also we have the same constraints , having foreign key references to product.product_id and company.company_id.
in schema 2 we are trying to insert all the rows from schemea 1 to schema 2 in one shot, like this
begin
insert into TXN_HEADER_REPORTS (
txn_id, txn_Date ,product_id,company_id , dealer_id , tran_amt)
select
txn_id, txn_Date ,product_id,company_id , dealer_id , tran_amt
from schema1.TXN_HEADER;
commit;
exception
when others then
< ... procedure to log the errors >
end;
now we are trying to execute the above procedure , and it failed due to foreign key constraint of one rows. But entire my transaction rollback. Actually i dont want to use cursor to process the rows one by one , at it takes long time. So i used to "insert into .. SElect from " but due to constraints of 1 row all my transaction not moved to schema2.txn_Extract_hdr.
Is there any way to trap only that failed and to process the other rows without terminating
Please advice ..
You can create an error log table, and then use a single insert:
exec dbms_errlog.create_error_log(dml_table_name => 'TXN_HEADER_REPORTS');
insert into TXN_HEADER_REPORTS ( txn_id, txn_Date ,product_id,company_id ,
dealer_id , tran_amt)
select txn_id, txn_Date ,product_id,company_id , dealer_id , tran_amt
from schema1.TXN_HEADER
log errors into ERR$_TXN_HEADER_REPORTS reject limit unlimited;
Any rows that can't inserted will be recorded in the ERR table. Note that this is plain SQL, it doesn't need to be in a PL/SQL block.
Read more in the documentation.
I don't understand your constraint.
Does your insert fail because the product_id and company_id don't exists in schema2?
In that case, it may be better to insert the missing company and product records before you insert records into TXN_HEADER_REPORTS of schema2.
insert into company com_sch2
(col1, col2, col2,...)
select col1, col2, col3, ...
from schema1.company com_sch1
where not exists (select 'x'
from company com2
where com2.company_id = com_sch1.company_id);
And the seem for the product table.

Resources