ORA-00904: "DWH_ITEM_SEQ"."NEXTAVL": invalid identifier - oracle

Can anyone explain to me why I get a 00904 error when I run the following
I need to insert into tabel a 100 rows and I dont no why the error
drop table DWH_ITEM_DIM;
CREATE TABLE DWH_ITEM_DIM(
ITEM_ID NUMBER NOT NULL,
ITEM_NAME VARCHAR2(5) NOT NULL,
ITEM_TYPE VARCHAR2(1) NOT NULL,
ITEM_COST NUMBER(10,2) NOT NULL,
ITEM_PRICE NUMBER(10,2) NOT NULL,
ITEM_FOR_SALE VARCHAR2(1) NOT NULL,
CONSTRAINT ITEM_ID_PK PRIMARY KEY (ITEM_ID));
DROP SEQUENCE DWH_ITE_SEQ;
CREATE SEQUENCE DWH_ITE_SEQ
START WITH 1
INCREMENT BY 1
NOCACHE
NOCYCLE;
TRUNCATE TABLE DWH_ITEM_DIM;
INSERT INTO DWH_ITEM_DIM
SELECT DWH_ITE_SEQ.NEXTAVL AS ITEM_ID,
DBMS_RANDOM.STRING('U',5) AS ITEM_NAME,
DBMS_RANDOM.STRING('U',1) AS ITEM_TYPE,
ROUND(dbms_random.value(LOW =>25,HIGH =>300),2) AS ITEM_COST,
ROUND(dbms_random.value(LOW =>25,HIGH =>300),2) AS ITEM_PRICE,
DBMS_RANDOM.STRING('U',1) AS ITEM_FOR_SALE
FROM DUAL
CONNECT BY LEVEL <= 100;
COMMIT;
When I run this the 'script Output' is:
table DWH_ITEM_DIM dropped.
table DWH_ITEM_DIM created.
sequence DWH_ITE_SEQ dropped.
sequence DWH_ITE_SEQ created.
table DM.DWH_ITEM_DIM truncated.
Error starting at line 19 in command:
INSERT INTO DM.DWH_ITEM_DIM
SELECT DWH_ITE_SEQ.NEXTAVL AS ITEM_ID,
DBMS_RANDOM.STRING('U',5) AS ITEM_NAME,
DBMS_RANDOM.STRING('U',1) AS ITEM_TYPE,
ROUND(dbms_random.value(LOW =>25,HIGH =>300),2) AS ITEM_COST,
ROUND(dbms_random.value(LOW =>25,HIGH =>300),2) AS ITEM_PRICE,
DBMS_RANDOM.STRING('U',1) AS ITEM_FOR_SALE
FROM DUAL
CONNECT BY LEVEL <= 100
Error at Command Line:20 Column:8
Error report:
SQL Error: ORA-00904: "DWH_ITE_SEQ"."NEXTAVL": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
commited.
I don't no why? please help me I'm new in oracle

It's a typo, change NEXTAVL TO NEXTVAL.

Try NEXTVAL instead of NEXTAVL.

Related

Trigger to raise raise an application error with a meaningful message if og_id refers to a cancelled Olympic Game

These are the following tables I have created:
CREATE TABLE Country
(
country_id NUMBER(3) PRIMARY KEY,
country_name VARCHAR(3) UNIQUE NOT NULL,
CONSTRAINT check_country_id CHECK (country_id > 0)
);
CREATE TABLE OG_Type
(
og_type_id NUMBER(3) PRIMARY KEY,
og_type_title VARCHAR(20) UNIQUE NOT NULL
);
CREATE TABLE Olympic_Game
(
og_id NUMBER(3) PRIMARY KEY,
og_type_id NUMBER(3) NOT NULL,
og_year NUMBER(4) NOT NULL,
og_website VARCHAR(150),
og_cancel VARCHAR(1) NOT NULL,
country_id NUMBER(3) NOT NULL,
CONSTRAINT check_og_id CHECK (og_id > 0),
CONSTRAINT check_og_year_og_type UNIQUE (og_type_id, og_year),
CONSTRAINT fk_og_type_id FOREIGN KEY(og_type_id) REFERENCES OG_Type(og_type_id),
CONSTRAINT fk_country_id FOREIGN KEY(country_id) REFERENCES Country(country_id)
);
CREATE TABLE Sport
(
sport_id NUMBER(3) PRIMARY KEY,
sport_title VARCHAR(100) UNIQUE NOT NULL,
CONSTRAINT check_sport_id CHECK (sport_id > 0)
);
CREATE TABLE Event
(
event_id NUMBER(6) PRIMARY KEY,
sport_id NUMBER(3) NOT NULL,
og_id NUMBER(3) NOT NULL,
event_title VARCHAR(100) NOT NULL,
event_team VARCHAR(1) NOT NULL,
no_per_team NUMBER(2) NOT NULL,
event_gender VARCHAR(1) NOT NULL,
CONSTRAINT check_event_id CHECK (event_id > 0),
CONSTRAINT check_event_title_sport_id_og_id_event_team_event_gender UNIQUE (event_title, sport_id, og_id, event_team, event_gender),
CONSTRAINT check_event_team CHECK (event_team IN ('Y','N')),
CONSTRAINT check_event_team_no_per_team CHECK ((event_team='N' AND no_per_team=1) OR (event_team='Y' AND no_per_team>1)),
CONSTRAINT check_event_gender CHECK (event_gender IN ('M','F')),
CONSTRAINT fk_sport_id FOREIGN KEY(sport_id) REFERENCES Sport(sport_id),
CONSTRAINT fk_og_id FOREIGN KEY(og_id) REFERENCES Olympic_Game(og_id)
);
After creating these 4 tables, I have inserted the values for these 4 tables:
insert into country(country_id,country_name) values (country_seq.nextval,'FRA');
insert into country(country_id,country_name) values (country_seq.nextval,'GBR');
insert into country(country_id,country_name) values (country_seq.nextval,'GRE');
insert into country(country_id,country_name) values (country_seq.nextval,'USA');
insert into og_type(og_type_id, og_type_title) values(og_type_seq.nextval,'Summer');
insert into og_type(og_type_id, og_type_title) values(og_type_seq.nextval,'Winter');
insert into og_type(og_type_id, og_type_title) values(og_type_seq.nextval,'Special');
insert into og_type(og_type_id, og_type_title) values(og_type_seq.nextval,'Youth');
insert into og_type(og_type_id, og_type_title) values(og_type_seq.nextval,'Senior');
insert into olympic_game(og_id,og_type_id,og_year,og_website,og_cancel,country_id) values(og_seq.nextval,1,1896,null,'N',3);
insert into olympic_game(og_id,og_type_id,og_year,og_website,og_cancel,country_id) values(og_seq.nextval,1,1900,null,'N',1);
insert into olympic_game(og_id,og_type_id,og_year,og_website,og_cancel,country_id) values(og_seq.nextval,1,1904,null,'N',4);
insert into olympic_game(og_id,og_type_id,og_year,og_website,og_cancel,country_id) values(og_seq.nextval,1,1908,'op1908.org','N',2);
insert into olympic_game(og_id,og_type_id,og_year,og_website,og_cancel,country_id) values(og_seq.nextval,2,1924,null,'N',1);
insert into olympic_game(og_id,og_type_id,og_year,og_website,og_cancel,country_id) values(og_seq.nextval,1,1944,null,'Y',2);
insert into olympic_game(og_id,og_type_id,og_year,og_website,og_cancel,country_id) values(og_seq.nextval,3,1944,null,'Y',2);
insert into olympic_game(og_id,og_type_id,og_year,og_website,og_cancel,country_id) values(og_seq.nextval,4,1944,null,'Y',2);
insert into olympic_game(og_id,og_type_id,og_year,og_website,og_cancel,country_id) values(og_seq.nextval,5,1944,null,'Y',2);
insert into olympic_game(og_id,og_type_id,og_year,og_website,og_cancel,country_id) values(og_seq.nextval,1,2012,'https://www.olympic.org/london-2012','N',2);
insert into sport(sport_id,sport_title) values(sport_seq.nextval, 'Track and Field');
insert into sport(sport_id,sport_title) values(sport_seq.nextval, 'Tennis');
insert into sport(sport_id,sport_title) values(sport_seq.nextval, 'Speed Skating');
insert into event(event_id,sport_id,og_id,event_title,event_team,no_per_team,event_gender) values(event_seq.nextval,1,1,'100m','N',1,'M');
insert into event(event_id,sport_id,og_id,event_title,event_team,no_per_team,event_gender) values(event_seq.nextval,2,1,'Double','Y',2,'M');
insert into event(event_id,sport_id,og_id,event_title,event_team,no_per_team,event_gender) values(event_seq.nextval,1,2,'200m','N',1,'M');
insert into event(event_id,sport_id,og_id,event_title,event_team,no_per_team,event_gender) values(event_seq.nextval,2,2,'Single','N',1,'M');
insert into event(event_id,sport_id,og_id,event_title,event_team,no_per_team,event_gender) values(event_seq.nextval,1,3,'400m','N',1,'F');
insert into event(event_id,sport_id,og_id,event_title,event_team,no_per_team,event_gender) values(event_seq.nextval,1,3,'100m','N',1,'F');
insert into event(event_id,sport_id,og_id,event_title,event_team,no_per_team,event_gender) values(event_seq.nextval,1,4,'1500m','N',1,'F');
insert into event(event_id,sport_id,og_id,event_title,event_team,no_per_team,event_gender) values(event_seq.nextval,3,5,'800m','N',1,'F');
insert into event(event_id,sport_id,og_id,event_title,event_team,no_per_team,event_gender) values(event_seq.nextval,1,10,'100m','N',1,'M');
insert into event(event_id,sport_id,og_id,event_title,event_team,no_per_team,event_gender) values(event_seq.nextval,1,10,'100m','N',1,'F');
Now this is the question:
Create a trigger called TR_event_on_cancelled_og. This trigger fires before inserting or updating a row in the Event table. The trigger should raise an application error with a meaningful message if og_id refers to a cancelled Olympic Game.
Now this is the solution I came up with:
CREATE OR REPLACE TRIGGER TR_event_on_cancelled
BEFORE INSERT OR UPDATE
ON Event
FOR EACH ROW
BEGIN
DECLARE
v_og_cancel Olympic_Game.og_cancel%TYPE;
BEGIN
SELECT og_cancel INTO V_og_cancel
FROM Olympic_Game, Event
WHERE Olympic_Game.og_id = Event.og_id
AND Olympic_Game.og_id = :NEW.og_id;
IF (v_og_cancel = 'N')
THEN
RAISE_APPLICATION_ERROR(-20001, 'This Olympic Game is cancelled already');
END IF;
END;
END;
Trigger TR_EVENT_ON_CANCELLED compiled
The trigger is compiled successfully but the problem is when I was trying to test the trigger I got the following error messages:
insert into event(event_id,sport_id,og_id,event_title,event_team,no_per_team,event_gender) values(event_seq.nextval,1,8,'400m','N',1,'F');
Error report -
SQL Error: ORA-01403: no data found
ORA-06512: at "OG_JC480454.TR_EVENT_ON_CANCELLED", line 5
ORA-04088: error during execution of trigger 'OG_JC480454.TR_EVENT_ON_CANCELLED'
01403. 00000 - "no data found"
*Cause: No data was found from the objects.
*Action: There was no data from the objects which may be due to end of fetch.
insert into event(event_id,sport_id,og_id,event_title,event_team,no_per_team,event_gender) values(event_seq.nextval,1,2,'400m','N',1,'F');
Error report -
SQL Error: ORA-01422: exact fetch returns more than requested number of rows
ORA-06512: at "OG_JC480454.TR_EVENT_ON_CANCELLED", line 5
ORA-04088: error during execution of trigger 'OG_JC480454.TR_EVENT_ON_CANCELLED'
01422. 00000 - "exact fetch returns more than requested number of rows"
*Cause: The number specified in exact fetch is less than the rows returned.
*Action: Rewrite the query or change number of rows requested
Where did I exactly go wrong ? ...
It would be really helpful if the trigger solution code is provided.
I think your problem is that your trigger code is selecting from EVENT. It doesn't need to do this because the rule applies only to the current event you're inserting or updating.
So:
SELECT og_cancel INTO V_og_cancel
FROM Olympic_Game
WHERE Olympic_Game.og_id = :NEW.og_id;
Incidentally, the next test looks wrong:
IF (v_og_cancel = 'N')
Unless you're using Y/N flags in an odd manner this would be testing if the Games have not been cancelled. Surely that should be = 'Y'?
" I want to get this error message"
I have created an Oracle LiveSQL with your data and my version of the trigger. Find it here (but you will need an OTN account to look at it).
When I attempt this INSERT ...
insert into event(event_id,sport_id,og_id,event_title,event_team,no_per_team,event_gender)
values(event_seq.nextval,2,9,'Double','Y',2,'M');
it fails on
ORA-20001: This Olympic Game is cancelled already
Quite rightly, because the games where og_id = 9 has og_cancel='Y'.
Now you say you're getting ORA-20977: the game was cancelled. That's a user-defined error (in the range -20999 to -20000. So it's some part of your codebase which is hurling it.
If you don't know what part it is you will need to dig into your data dictionary. I can't solve that for you. But here's a thought: have you got more than one trigger on the table? Your homework says to create a trigger called TR_event_on_cancelled_og but the code you published does CREATE OR REPLACE TRIGGER TR_event_on_cancelled. Perhaps you previously created a trigger with the other name, and that's what is raising the ORA-20977.

sql script not running

Here is a code snippet of a sql script which is giving me error,I have to generate a sequence on the primary_key of the table without using triggers in oracle:
CREATE SEQUENCE t1_seq START WITH 1 INCREMENT BY 1;
DROP TABLE CPR_SOURCE_SYSTEM_METADATA;
CREATE TABLE CPR_SOURCE_SYSTEM_METADATA
(
SYSTEM_ID NUMBER(4) NOT NULL t1_seq.nextval,
SYSTEM_NAME VARCHAR2(200),
DATE_FORMAT VARCHAR2(200),
CREATED_BY VARCHAR2(200),
MODIFIED_BY VARCHAR2(200),
CREATED_ON NUMBER(20),
MODIFIED_ON NUMBER(20),
IS_DELETED VARCHAR2(1),
CONSTRAINT "CPR_SOURCE_SYSTEM_PK" PRIMARY KEY ("SYSTEM_ID")
);
It is giving me the below error :
DROP TABLE CPR_SOURCE_SYSTEM_METADATA
* ERROR at line 1: ORA-00942: table or view does not exist
SYSTEM_ID NUMBER(4) NOT NULL t1_seq.nextval,
* ERROR at line 3: ORA-00907: missing right parenthesis
Not able to figure out the error,can anyone help??
SYSTEM_ID NUMBER(4) NOT NULL t1_seq.nextval,
The t1_seq.nextval segment is not valid - you cannot specify an auto-incrementing column like that.
The SQL parser is expecting to see:
SYSTEM_ID NUMBER(4) NOT NULL,
and throws the exception as the comma is not where it expects.
In Oracle 12c you can use an identity column but in earlier versions you will either need to:
Use the sequence in the SQL insert statement;
Use a trigger to insert the correct sequence value; or
Create a stored procedure to handle inserts and manage the sequence through that (disallowing direct inserts that could bypass this).

oracle cannot insert values into a nested table field

I have the sample CUSTOMERS table.
I exported the values from table as INSERT script.
I took one row, modified the value from PK and re-executed the insert.
Insert into oe.customers (CUSTOMER_ID,
CUST_FIRST_NAME,
CUST_LAST_NAME,
CUST_ADDRESS,
PHONE_NUMBERS,
NLS_LANGUAGE,
NLS_TERRITORY,
CREDIT_LIMIT,
CUST_EMAIL,
ACCOUNT_MGR_ID,
CUST_GEO_LOCATION,
DATE_OF_BIRTH,
MARITAL_STATUS,
GENDER,
INCOME_LEVEL,
CREDIT_CARDS)
values ( oe.customer_seq.nextval,
'Donald',
'Hunter',
OE.CUST_ADDRESS_TYP('5122 Sinclair Ln','21206','Baltimore','MD','US'),
OE.PHONE_LIST_TYP('+1 410 123 4795'),
'us',
'AMERICA',
2600,
'Donald.Hunter#CHACHALACA.EXAMPLE.COM',
145,
MDSYS.SDO_GEOMETRY(2001,8307,MDSYS.SDO_POINT_TYPE(-76.545732,39.322775,NULL),NULL,NULL),
to_date('20-JAN-60','DD-MON-RR'),
'married',
'M',
'G: 130,000 - 149,999',
OE.TYP_CR_CARD_NST(OE.TYP_CR_CARD('Visa',100000000000011)));
and I have the following error message:
Error at Command Line : 32 Column : 29 Error report - SQL Error:
ORA-00904: : invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
which refers to line:
OE.TYP_CR_CARD_NST(OE.TYP_CR_CARD('Visa',100000000000011)));
The definition of types are:
create or replace type typ_cr_card as object
( card_type VARCHAR2(25)
, card_num NUMBER);
create or replace type typ_cr_card_nst as table of typ_cr_card;
Can someone tell me, please, what is wrong with this insert line as it is the one provided by SQL DEVELOPER?
NOTE: Also, I tried to use a procedure which inserts values in this table and the error refers to TYP_CR_CARD_NST datatype.

Leave column out of Oracle insert statement

I have an insert statement that I am trying to run against an Oracle database. The insert statement is very simple, and I only want to insert a value into one column. The table itself has more than one column, but I am only interested in filling one of the columns with data. The format of my query is similar to the one below:
insert into myTable (col1) Values (val1)
However, this throws the following error:
ORA-00904: "col1": invalid identifier
I've checked to make sure that the column is named correctly, and my only other thought is that something is wrong with my syntax. There are no constraints on the table such as primary keys. Is it possible to only insert values into certain columns when executing an insert statement in Oracle?
Check that you didn't quote the column name on creation of the table. If you did, the column name is stored as you quoted it. For example:
create table table1 (
id number(2)
)
has a different column name to this
create table table2 (
"id" number(2)
)
Oracle by default stores the (unquoted) column names in uppercase. Quoted are stored as is.
You can use a DESC table_name to see how the columns are stored.
The following
select id from table1
select iD from table1
select ID from table1
fetch the records, while only
select "id" from table2
will fetch records.
select id from table2
will throw the ORA-00904 : "ID" : invalid identifier error.
You may have inadvertently done the quoting during creation while using tools as established below:
https://community.oracle.com/thread/2349926
Btw: Yes it is possible to insert records for only one column, as long as the other columns do not have a NOT NULL constraint on them.
Actually, I think you may be double quoting the column in the insert statement (not on table creation), although your example is misleading. I guess this because of your error, which says "col1" is invalid, not "COL1" is invalid. Consider this simple test:
SQL> create table mytable
(
col1 varchar2(10)
)
Table created.
SQL> -- incorrect column name, without double quotes
SQL> insert into mytable(col2) values ('abc')
insert into mytable(col2) values ('abc')
Error at line 9
ORA-00604: error occurred at recursive SQL level 1
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 11
ORA-00904: "COL2": invalid identifier
SQL> -- incorrect column name, with double quotes
SQL> insert into mytable("col2") values ('abc')
insert into mytable("col2") values ('abc')
Error at line 12
ORA-00604: error occurred at recursive SQL level 1
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 11
ORA-00904: "col2": invalid identifier
SQL> -- correct column name, without double quotes (works)
SQL> insert into mytable(col1) values ('abc')
1 row created.
SQL> -- correct column name, with double quotes (fails)
SQL> insert into mytable("col1") values ('abc')
insert into mytable("col1") values ('abc')
Error at line 18
ORA-00604: error occurred at recursive SQL level 1
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 11
ORA-00904: "col1": invalid identifier
The last failed insert attempt is what I think you may be doing:
insert into mytable("col1") values ...
based on the error message:
ORA-00904: "col1": invalid identifier
So the solution would simply be remove the double quoting around the column name in the insert.
It's most probably a syntax error
Desc myTable;
insert into myTable (col1) Values ('val1')
Ensure col1 is a valid column in the table, and you're simply not trying to say 'select the left-most column.
edit: Is it possible to only insert values into certain columns when executing an insert statement in Oracle?
Yes if you want to only insert into certain columns then simply specify it
e.g.
insert into myTable (col1, col2, col6) Values ('val1', 'val2', 'val3');
This will only work if the column itself doesn't have a NOT NULL constraint - in which case it will not allow a value to be enetered (unless again there's a default value).

SQL invalid identifier

I checked some of the SQL invalid identifier solutions, but none of them helped me to fix my prob.
CREATE OR REPLACE PROCEDURE add_order(
p_cust_id cust_order.cust_id%TYPE,
p_order_id cust_order.order_id%TYPE,
p_order_date cust_order.order_date%TYPE,
p_sales_made_by cust_order.sales_made_by%TYPE,
p_qty cust_order.qty%TYPE,
p_product_id cust_order.product_id%TYPE,
p_cust_charged cust_order.cust_charged%TYPE,
p_company_price cust_order.company_price%TYPE,
p_shipper_id cust_order.shipper_id%TYPE)
IS
BEGIN
IF p_shipper_id = 'FEDEX' OR p_shipper_id = 'USPS' OR p_shipper_id = 'CUST' THEN
INSERT INTO productss (cust_id, order_id, order_date, sales_made_by, qty,
product_id, cust_charged, company_price, shipper_id)
VALUES (p_cust_id, p_order_id, p_order_date, p_sales_made_by, p_qty,
p_product_id, p_cust_charged, p_company_price, p_shipper_id);
ELSE
dbms_output.PUT_LINE('This shipper IS NOT allowed');
END IF;
END add_order;
/
The error is:
17/1 PL/SQL: SQL Statement ignored
17/116 PL/SQL: ORA-00904: "SHIPPER_ID": invalid identifier
This is the table:
CREATE TABLE CUST_ORDER
(
CUST_ID NUMBER(15),
ORDER_ID NUMBER(20),
ORDER_DATE DATE,
SALES_MADE_BY VARCHAR(40),
QTY NUMBER,
PRODUCT_ID NUMBER,
CUST_CHARGED NUMBER,
COMPANY_PRICE NUMBER,
SHIPPER_ID NUMBER,
PRIMARY KEY (ORDER_ID),
FOREIGN KEY (CUST_ID) REFERENCES CUST_INFO(CUST_ID),
FOREIGN KEY (PRODUCT_ID) REFERENCES PRODUCTS(PRODUCT_ID)
FOREIGN KEY (SHIPPER_ID) REFERENCES SHIPPING(SHIPPER_ID)
);
I guess that it is obvious mistake, but I was working too much.
You have an extra 'S' in the PRODUCTS table name:
INSERT INTO PRODUCTSS
^
And as #Alex Poole points out, you appear to be inserting into the wrong table anyway, given the naming of the proc.

Resources