This question already has answers here:
How To Handle Table Column Named With Reserved Sql Keyword?
(4 answers)
Closed 19 days ago.
I'm trying to add a column to my users table but I get an error every time I try to create a new user I end up getting the error "java.sql.SQLSyntaxErrorException: ORA-00904: "IMPORTED": invalid identifier".
In the database my column is created but I still get an error when i try to create a user.
I'm trying to create a column that receives true or false in my user table.
When I try to create a new user by jpa I end up getting an error
My Model:
#Column(name = "IMPORTED")
#Enumerated(EnumType.STRING)
private Eboolean imported;
My query:
alter table USER add IMPORTED VARCHAR2(1);
What you are saying is not (entirely) true. Table name certainly isn't user:
SQL> create table user (id number);
create table user (id number)
*
ERROR at line 1:
ORA-00903: invalid table name
Why? Because it is reserved for function that returns currently logged user:
SQL> select user from dual;
USER
--------------------------------------------------------------------------------
SCOTT
SQL>
Therefore, table name is something else; or, if it is user, then you must have created it by enclosing its name into double quotes, but then you have to use double quotes every time, matching letter case every time:
SQL> create table "user" (id number);
Table created.
Presuming that this is what you have, then:
SQL> alter table "user" add imported varchar2(1);
Table altered.
SQL>
Furthermore, error you specified is
ORA-00904: invalid identifier
If you got it when inserting a row into a table, it means that there's no such column in that table. Possible causes:
you misspelled its name
there's really no such column there
double quotes and letter case matching issue
Presuming that column exists (see above alter table), insert also works:
SQL> insert into "user" (id, imported) values (1, 0);
1 row created.
SQL> select * from "user";
ID IMPORTED
---------- ----------
1 0
SQL>
Related
I am retrieving data from database using jdbc. In my code I am using 3-4 tables to get data. But sometimes if table is not present in database my code gives exception. How to handle this situation. I want my code to continue working for other tables even if one table is not present. Please help.
I have wrote a code like this
sql="select * from table"
now Result set and all.
If table is not present in database it give exception that no such table. I want to handle it. In this code I cannot take tables which are already present in advance . I want to check here itself if table is there or not.
Please do not mark it as a duplicate question. The link you shared doesnot give me required answer as in that question they are executing queries in database not through JDBC code
For Sybase ASE the easiest/quickest method would consist of querying the sysobjects table in the database where you expect the (user-defined) table to reside:
select 1 from sysobjects where name = 'table-name' and type = 'U'
if a record is returned => table exists
if no record is returned => table does not exist
How you use the (above) query is up to you ...
return a 0/1-row result set to your client
assign a value to a #variable
place in a if [not] exists(...) construct
use in a case statement
If you know for a fact that there won't be any other object types (eg, proc, trigger, view, UDF) in the database with the name in question then you could also use the object_id() function, eg:
select object_id('table-name')
if you receive a number => the object exists
if you receive a NULL => the object does not exist
While object_id() will obtain an object's id from the sysobjects table, it does not check for the object type, eg, the (above) query will return a number if there's a stored proc named 'table-name'.
As with the select/sysobjects query, how you use the function call in your code is up to you (eg, result set, populate #variable, if [not] exists() construct, case statement).
So, addressing the additional details provided in the comments ...
Assuming you're submitting a single batch that needs to determine table existence prior to running the desired query(s):
-- if table exists, run query(s); obviously if table does not exist then query(s) is not run
if exists(select 1 from sysobjects where name = 'table-name' and type = 'U')
begin
execute("select * from table-name")
end
execute() is required to keep the optimizer from generating an error that the table does not exist, ie, the query is not parsed/compiled unless the execute() is actually invoked
If your application can be written to use multiple batches, something like the following should also work:
# application specific code; I don't work with java but the gist of the operation would be ...
run-query-in-db("select 1 from sysobjects where name = 'table-name' and type = 'U'")
if-query-returns-a-row
then
run-query-in-db("select * from table-name")
fi
This is the way of checking if the table exists and drop it:
IF EXISTS (
SELECT 1
FROM sysobjects
WHERE name = 'a_table'
AND type = 'U'
)
DROP TABLE a_table
GO
And this is how to check if a table exists and create it.
IF NOT EXISTS (
SELECT 1
FROM sysobjects
WHERE name = 'a_table'
AND type = 'U'
)
EXECUTE("CREATE TABLE a_table (
col1 int not null,
col2 int null
)")
GO
(They are different because in table-drop a temporary table gets created, so if you try to create a new one you will get an exception that it already exists)
Before running the query which has some risk in table not existing, run the following sql query and check if the number of results is >= 1. if it is >= 1 then you are safe to execute the normal query. otherwise, do something to handle this situation.
SELECT count(*)
FROM information_schema.TABLES
WHERE (TABLE_SCHEMA = 'your_db_name') AND (TABLE_NAME = 'name_of_table')
I am no expert in Sybase but take a look at this,
exec sp_tables '%', '%', 'master', "'TABLE'"
Sybase Admin
So i have two table
Detail table
Name - Admno - ModuleCode - PASS
John , 127261, 87772, -
candy , 923823, 2323, -
result table
Admno - ModuleCode - PASS
127261, 87772,Yes
923823, 2323,No
Notice that result table don't have name whereas detail table have and detail table.PASS was not been filled .What i was trying is to fill Detail table column 'PASS' from result table column 'PASS' WHERE both detail.admno = result.admno AND detail.ModuleCode = result.ModuleCode
INSERT into detail SET detail.PASS= `result`.PASS FROM
`result`, detail WHERE `result`.Admno = detail.Admno
AND `result`.Code = detail.ModuleCode
but the error i got was Error code 1064, SQL state 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM result, detail WHERE result.Admno = detail.Admno AND result.Code = ' at line 1
Line 1, column 1
By the way I'm using java in netbean to do sql statement.
You'll need to have a join statement to combine data from multiple tables. Below is likely what you're looking for.
UPDATE detail INNER JOIN `result` ON `result`.Admno = detail.Admno
AND `result`.Code = detail.ModuleCode SET detail.PASS=`result`.PASS
i tried to write a named query in my Entity Java Bean class , and tried also to write the same query as a native query , its job is to delete records which difference between their timestamp column and current time stamp, are greater than 2 hours.
my query :
DELETE FROM APP.WEATHER WHERE timestampdiff(SQL_TSI_HOUR,APP.WEATHER.SINCE,CURRENT_TIMESTAMP) > 2;
but it failed , and this error message appeared to me :
Error code -1, SQL state 42X04: Column 'SQL_TSI_HOUR' is either not in any table in the FROM list or appears within a join specification and is outside the scope of the join specification or appears in a HAVING clause and is not in the GROUP BY list. If this is a CREATE or ALTER TABLE statement then 'SQL_TSI_HOUR' is not a column in the target table.
Line 1, column 1
I have a project named 'Online Recruitment System'. There is problem in database connectivity.
First i made table 'registration' and 'clogindetails' without using quotes in sqlplus.
Then all data used to save properly, but on login I was getting following error:
java.sql.SQLException: ORA-00904: PASSWORD: invalid identifier
After this I read stackoverflow multiple examples. And I added "double quotes" to the table items in database and kept them in lowercase.
Now the data is not even getting saved. I tried to look in 'object browser' in 'data' tab and there was following error:
failed to parse SQL query:
ORA-00904: "pass": invalid identifier
As far I know the project is made all right. Only there is problem in making tables.
Here is code from one of the page which use table 'clogindetails':
String usrname=getClogid();
String pass=getCpassword();
if(usrname!=null && pass!=null && usrname.length()>0 && pass.length()>0)
{
ps = con.prepareStatement("select * from clogindetails where logid=? and password=?");
ps.setString(1,usrname);
ps.setString(2,pass);
rs=ps.executeQuery();
HttpSession session=request.getSession(true);
if(!rs.next())
{
errors.add("invalid", new ActionMessage("errors.invalidusername"));
}
}
rs.close();
ps.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
if(getClogid()==null || getClogid().length()<1)
{
errors.add("logid", new ActionMessage("errors.logid.required"));
}
if(getCpassword()==null || getCpassword().length()<1)
{
errors.add("password", new ActionMessage("errors.password.required"));
}
return errors;
the schema of clogindetails is
CREATE TABLE "CLOGINDETAILS"(
"ADMITID" NUMBER(15,0),
"NAME" VARCHAR2(25),
"LOGID" VARCHAR2(10),
"PASS" VARCHAR2(20)
)
ORA-00904 is common when invalid alias in a select statement is referred.
To overcome this error, you need enter a valid column name.
To avoid ORA-00904, column names cannot be a reserved word, and must contain these four criteria to be valid:
begin with a letter
be less than or equal to thirty characters
consist only of alphanumeric and the special characters ($_#); other characters need double quotation marks around them
For more info you can refer this link.
http://www.dba-oracle.com/t_ora_00904_string_invalid_identifier.htm
By looking at user exception error i could say that such type of exceptions are occurred when we are using different column names.
for ex:
i have created table in database i.e
create table tb_login(username varchar(10), password varchar(10));
and my java code is
ps=con.prepareStatement("select * from tb_login where username=? and pass=?");
//error bcz pass
This code will produce same exception bcz in my database second column name is password not pass.
so check your database schema and java code. or else share your database schema.
k now try this
ps=con.prepareStatement("select * from clogindetails where logid=? and pass=?");
rest of the code keep as it is..
Try to do this one:
Change your query
from: select * from clogindetails where logid=? and password=?
to: select * from clogindetails where logid='?' and password='?'
Point is that when you make queries, you should spot your identifiers with ' ', otherwise it seems like you are comparing some unknown table column's data (variables) with data from existing columns.
ex: select e.full_name from e.employees where e.full_name like '&Medet&'
otherwise you'll get an error.
Hope this will help you!
This error occurs when your column name not as same as you are writing in your query . and avoid to give column names which are Keyword of oracle , Group this cant be your column name or table name.
I have a table which contains huge data. I want to add hibernate automatic versioning to this table. But adding a not-null column to this table for versioning is very expensive due to huge data. Is there a workaround so hibernate can work with nullable column? Currently hibernate gives NPE because it tries to increment a null value.
( As hibernate manages this internally, changing the version value on client side is out of question )
Any other versioning startegy is welcome too. Thanks in advance
If your flavour of database permits it you could use the DEFAULT option. This is against Oracle ...
SQL> create table t23 as select object_id as id from user_objects;
Table created.
SQL> desc t23
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NUMBER
SQL> alter table t23 add hibernate_version_number number default 0 not null;
Table altered.
SQL> desc t23
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NUMBER
HIBERNATE_VERSION_NUMBER NOT NULL NUMBER
SQL> select count(*) from t23 where hibernate_version_number = 0;
COUNT(*)
----------
504
SQL>
However, you may still want to benchmark its performance against a realistic volume of data. It may not solve your problem.