Leave column out of Oracle insert statement - oracle

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).

Related

EFCore error:- ORA-00904: "m"."Id": invalid identifier

I am working with an application using asp.net core 2.2 and efcore database first approach with Oracle database. I am using Oracle.EntityFrameworkCore (2.19.60) nuget package, successfully mapped db model, but when I try to fetch data from DBContext , getting error
ORA-00904: "m"."Id": invalid identifier
Oracle Database version: Oracle Database 12c Standard Edition Release 12.2.0.1.0 - 64bit Production
code:
var fetched = await myDatabaseContext.MyTableVersions.ToListAsync();
LinQ is generating following query :
SELECT "m"."Id", "m"."MAJORVERSION" FROM "MyTableVersions" "m"
Since It's not a correct syntax for PL/Sql query so getting error ORA-00904: "m"."Id": invalid identifier.
Is there any way to fix this error? Thanks.
I have searched on the web and found
Bug 30352492 - EFCORE: ORA-00904: INVALID IDENTIFIER
but that issue is related to schema.
The query is perfectly valid (db<>fiddle here), assuming your table looks something like
CREATE TABLE "MyTableVersions"
("Id" NUMBER,
MAJORVERSION NUMBER)
However, I suspect your table looks like
CREATE TABLE "MyTableVersions"
(ID NUMBER,
MAJORVERSION NUMBER)
I don't know what the class looks like that you're trying to fetch into, but I suspect it has a field named Id. If you can change the name of that field to ID (in other words, so that its capitalization matches the capitalization of the related database column) you might find it works then.
Double quotes are something you should avoid in Oracle world.
Your query:
SELECT "m"."Id", "m"."MAJORVERSION" FROM "MyTableVersions" "m"
means that column name is exactly Id (capital I followed by d). Only if that's really so, you should use double quotes. Otherwise, simply remove them:
SELECT m.id, m.MAJORVERSION FROM MyTableVersions m
The same goes for the table name - if it was created using mixed case (and you can't do that without double quotes), you'll have to use double quotes and exactly same letter case always. Otherwise, don't use them.
Oracle is case-insensitive regarding object and column names and are UPPERCASE by default:
SQL> create table test (id number);
Table created.
SQL> desc test
Name Null? Type
----------------------------------------- -------- -----------------
ID NUMBER
SQL> select table_name, column_name
2 from user_tab_columns
3 where table_name = 'TEST';
TABLE_NAME COLUMN_NAME
------------------------------ ------------------------------
TEST ID
SQL> insert into test (id) values (1);
1 row created.
SQL>
But, if you use mixed case with double quotes, then use them always:
SQL> create table "teST" ("Id" number);
Table created.
SQL> insert into test (id) values (1);
insert into test (id) values (1)
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> insert into "test" (id) values (1);
insert into "test" (id) values (1)
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> insert into "teST" (id) values (1);
insert into "teST" (id) values (1)
*
ERROR at line 1:
ORA-00904: "ID": invalid identifier
SQL> insert into "teST" ("Id") values (1);
1 row created.
SQL>
So: first make sure what table and column names really are, then use them appropriately.
To avoid problems with case-sensitivity, add the option to eliminate EF to add quotes to the generated queries. For instance, if you are using Devart Oracle provider, this is done in the following way:
public override void ConfigureServices(ServiceConfigurationContext context)
{
Configure<AbpDbContextOptions>(options =>
{
var config = Devart.Data.Oracle.Entity.Configuration.OracleEntityProviderConfig.Instance;
config.Workarounds.DisableQuoting = true;
...
}
}
If you are using official provider - just inherit from DbCommandInterceptor (do quotes replacement with empty character in dbCommand in ...Executing methods), add this interceptor to DbContextOptionsBuilder instance.

The data model cannot be executed because of an error, please contact the administrator

I have a data sets that contains a columns which the parameters is by name, date and number. But everytime I view the data there's a error said The data model cannot be executed because of an error, please contact the administrator. But it only show the message but didn't show the details of the error. I also have a list of values because I set the parameter type of my parameters for name and number as a menu which is the result
that will return for the number is based on the name because if I didn't base it on the name it will return a 100+ values which is not okay to my user.
My query for my data set for example is,
select a.name, a.date, a.type_name, b.number, c.address
from details1 a, details2 b, details3 c
where
a.id = b.id
and b.id = c.id
and a.name = :name
and a.date between :start_date and :end_date
and b.number = :number
Query of List of Values for name
select a.name from details1 a
where a.type_name = 'person'
Query of List of Values for num
select b.number
from details1 a, details2 b
where 1=1
and a.id = b.id
and a.name = :name
I don't know BI Publisher, but - as far as Oracle is concerned, column name can not be number. It is reserved word for a datatype:
SQL> create table test (number number);
create table test (number number)
*
ERROR at line 1:
ORA-00904: : invalid identifier
Your query uses such a column:
select b.number ...
which can't work, unless someone created such a table by enclosing column name into double quotes, e.g.
SQL> create table test ("number" number);
Table created.
SQL> desc test
Name Null? Type
----------------------------------------- -------- ---------------
number NUMBER
SQL>
However, you'll have to specify such a column name using double quotes every time, paying attention to letter case (meaning: if column was created as "NumBER", you have to reference it exactly that way. "numBER" or "NUMber" or "nUmBeR" or "number" or "NUMBER" won't work). A few examples:
SQL> insert into test (number) values (1);
insert into test (number) values (1)
*
ERROR at line 1:
ORA-00928: missing SELECT keyword
SQL> insert into test ("NUMber") values (1);
insert into test ("NUMber") values (1)
*
ERROR at line 1:
ORA-00904: "NUMber": invalid identifier
SQL> insert into test ("NUMBER") values (1);
insert into test ("NUMBER") values (1)
*
ERROR at line 1:
ORA-00904: "NUMBER": invalid identifier
SQL> insert into test ("number") values (1);
1 row created.
SQL> select t."number" from test t;
number
----------
1
SQL>
Therefore, I'd suggest you to check table description and try with what you see; maybe it is as simple as
select b."number" ...
If that's the case, it is more than obvious that doing something because you can do it doesn't mean that you should do it. Avoid such things, never enclose Oracle object names into double quotes, use defaults.

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.

Getting ORA-00904 invalid identifier executing multi table insert query

I'm just trying 'multi table insert'. Below is my insert query. I'm trying to insert values from employees table to table t1,t2 and t3. After executing the query i'm getting an error.
ERROR at line 4:
ORA-00904: "EMPLOYEES"."LAST_NAME": invalid identifier
The column last_name is exist in employees table. But why i'm getting this error.
insert all
into t1(id,l_name) values(employees.employee_id,employees.last_name)
into t2(id,l_name) values(employees.employee_id,employees.last_name)
into t3(id,l_name) values(employees.employee_id,employees.last_name)
select * from employees;
/
I've also tried replacing table name and column name to upper case. Still facing the same error. I'm using Oracle 10g.
Thanks
After removing the reference table name employees from column name its working.
Answer:
INSERT ALL
INTO t1(id, l_name) VALUES (employee_id, last_name)
INTO t2(id, l_name) VALUES (employee_id, last_name)
INTO t3(id, l_name) VALUES (employee_id, last_name)
SELECT * FROM employees;

Inner query not throwing error in postgres

There is a scenario in which we are retrieving some result from inner query and using the result to perform some operation
create table test1(key integer,value varchar)
insert into test1 values(1,'value 1');
insert into test1 values(2,'value 2');
insert into test1 values(3,'value 3');
second table as
create table test2(key1 integer, valuein varchar);
insert into test2 values (2,'value inside is 2');
insert into test2 values (4,'value inside is 4');
insert into test2 values (5,'value inside is 5');
the below query is giving result but in my view it should give an error
select * from test1 where key in
(select key from test2)
because key column does not exist in test2 table.
but it is giving result in postgres
but when run in oracle it is giving error as
ORA-00904: "KEY": invalid identifier
00904. 00000 - "%s: invalid identifier"
This is the correct behavior as specified in the SQL standard. The inner query has access to all columns of the outer query - and because test1 has a column named key (which, btw is a horrible name for a column) the inner select is valid.
See these discussions on the Postgres mailing list:
http://postgresql.nabble.com/BUG-13336-Unexpected-result-from-invalid-query-td5850684.html

Resources