ORA-00904 Error - java

I'm using JDBC to access a database and I'm basically querying by calling a String.
Here is my table book_table:
| title | book no. | author_first | author_last |
The cat, 0, Tim, Joe
The dog, 1, John, Soe
The mouse, 2, Josh, Moe
And here is the SQL statement I am querying with:
"select title from book_table where author_first like '%tim%' and author_last like '%joe%'";
I keep getting the error exception in Java: ORA-00904: "author_last": invalid identifier"
Any clue what I'm doing wrong?

It would be very likely that you may have used double quotes while creating the DDL for the book_table.
From the error it means that there is no such column as author_first
The names used in database should be working fine and that uppercase and lowercase could be very well be ignored if you would have not used double quotes.
create table book_table(title varchar2(100));
create table BOOK_TABLE(TITLE varchar2(100));
In both the cases
select * from book_table where title like '%mytitle%';
should be working fine.
However if you would have used double quotes, then you would have to use the exact cases while performing sql operations.
create table "BOOK_TABLE"("Title" varchar2(100));
In this case
select * from book_table where title like '%mytitle%'
would not work
You have to make use of
select * from book_table where "Title" like '%mytitle%';
I'd suggest dropping the table and recreating them without double quotes and see if that solves it.

Related

Checking if table exist or not

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

Invalid character encountered error - selecting rows

I am trying to get rows from table using SQLDeveloper by writing simple query:
SELECT * FROM agreements WHERE agreementkey = 1;
SELECT * FROM agreements WHERE agreementkey = 4;
but getting invalid character encountered error. It's not a problem with query(working using other keys, i.e. agreementkey = 3) but with XMLType column in this table - there is something wrong with data in some rows. Is there a way to select this affected row(I know keys of this affected rows) using queries? Maybe export to file or something? Solution of copying value manually is not acceptable.
Create an empty copy of the table and then run an INSERT into it based on a select from the original table but do it using the DML error logging clause.
This should show you any rows that fail to load and the reason for the failure.
CREATE TABLE test_agreements
AS SELECT * FROM agreements
WHERE ROWNUM <1;
INSERT INTO test_agreements
SELECT *
FROM agreements
LOG ERRORS REJECT LIMIT UNLIMITED
This will create you an error logging table called ERR$TEST_AGREEMENTS which you can query to find the problem rows.
Problem is in WHERE key = 1 cause key is a Reserve Word in Oracle. You should escape it like
SELECT * FROM table WHERE "key" = 1;
KEY is a reserved word so to overcome that you need to use double quotes "".
SELECT * FROM table WHERE "key" = 1;
I think the problem can be solved by putting the argument in quotes:
SELECT * FROM agreements WHERE agreementkey = "1";
I wish I were familiar with XML, but I have run into this with VARCHAR2 columns that are supposed to have all numeric values. Oracle looks at the request
where agreementkey = 1
and tries to convert agreementkey to a number rather than 1 to a varchar2.
If the database contains invalid characters I would try one the following:
Maybe the solution of BriteSponge will work, using an insert statemant with error logging clause.
Use datapump to export the data to a file. I think the log will contain information to identify the invalid columns.
There was a tool called "character set scanner" that checked the characters of the data of a table, here is some documentation: CSSCAN. Or maybe you can use the Database Migration Assistent for Unicode (DMU) mentioned in the same manual.
4- You can write a small PL/SQL program that retrieves the rows row by row and in case of an error catches the exception and notifies you about the row.
DECLARE
invalid_character_detected EXCEPTION;
PRAGMA EXCEPTION_INIT(invalid_character_detected, ???); begin
for SELECT rowid into rec FROM agreements do begin
for
SELECT * into dummy
FROM agreements
where rowid=rec.rowid
do
null;
end loop;
except
WHEN invalid_character_detected THEN
dbms_ouput.put_line(rec.rowid)
end;
end loop;
end;
I did not compile and test the program. ??? is the (negative) error code, e.g. -XXXXX if the error is ORA-XXXXX

Using the IN clause with SQLite

In a development environment with SQLite 3.7.11 and Java, despite having read the following answers:
Answer 1
Answer 2
Answer 3,
am finding the usage of the SQLite IN clause not very straight-forward.
Assume a simple table TEST with the following structure:
----------------------------
id PRODUCT TAG
(int) (text) (text)
----------------------------
1 Cinthol Soap, Bath, Cleaning
2 Vim Dishwash, Kitchen, Cleaning
3 Burger Food, Breakfast, Lunch, Dinner
The following queries are behaving this way:
----------------------------------------------------------------------------
Query Result Expected
----------------------------------------------------------------------------
SELECT PRODUCT FROM TEST WHERE TAG IN('Soap') Cinthol Cinthol
SELECT PRODUCT FROM TEST WHERE TAG IN('Dinner') <EMPTY> Burger
SELECT PRODUCT FROM TEST WHERE TAG IN('Soap', 'Bath') <EMPTY> Cinthol
SELECT PRODUCT FROM TEST WHERE TAG IN('Cleaning') <EMPTY> Cinthol, Vim
So the questions are:
Except the first query, why are the others not producing the expected results? Is there something fundamentally wrong in the understanding?
If wrong, what is the right query to get the expected results (without using the instr function)?
Furthermore, the TAG column eventually has to be bound with an array of tokens in Java, building the query dynamically. The answers listed above have pointers to that, though.
Thanks in advance!
In clause doesn't work like this.assume if you had one TAG each column you could get the results.you need to add another table to keep your tags.in this table you need pk , foreign key(id deom tests) ,and tag so that you wil have multitags for each product.this is a solution you can make different.You had better search database notmalization first.gl

How to use Collate in Hibernate for french char set

Hi I'm trying to use collate on a view which has column name per_va_first_name when I use the following query :
SELECT *
FROM person_view
WHERE NLSSORT(per_va_first_name, 'NLS_SORT = FRENCH_AI') = NLSSORT('mickaël', 'NLS_SORT =FRENCH_AI')
I get the error
ORA-12702: invalid NLS parameter string used in SQL function
I'm new to oracle and this nlssort. Can anyone help me in pointing out what's my mistake?
And at the same time I want to use collate in Hibernate for Java. Same french char set.
Edit:
When I use these commands in sql
alter session set nls_sort=French_AI;
alter session set nls_comp=linguistic;
I get the desired output when this query is executed
SELECT * FROM v_myuser_search_test_ea4 where per_va_first_name like 'Mickaël%'
How to do this in Hibernate? Is there a way I can append 'CI' to French_AI to make it 'French_AI_CI'
According to Oracle documentation found on http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions113.htm#SQLRF51562
you might change your query to
SELECT *
FROM person_view
ORDER BY NLSORT(per_va_first_name, 'NLS_SORT = FRENCH_AI_CI')
Hibernate should understand it, however you are already losing database portability as this is Oracle-specific function.

Discriminator value from separate table in Hibernate inheritance mapping

I have two tables, price_feed and price_feed_type. Price feed will contain the data for different implementations of price feeds.
Now, this would usually be done through the <discriminator> tag, with a discriminator field in the price_feed table. However, it would be preferable in the context of the system to have this field in the price_feed_type table. I've come across the <discriminator formula="..."/> method, and tried working with this.
One solution is as follows (assume this is entered in the formula attribute):
(SELECT implementing_class FROM price_feed_type INNER JOIN price_feed ON price_feed.price_feed_type_id = price_feed_type.price_feed_type_id")
Another solution is the following, and I should note that I've tried with both priceFeedID and price_feed.price_feed_type_id
(SELECT implementing_class FROM price_feed_type WHERE price_feed_type.price_feed_type_id = price_feed.price_feed_type)
Either one gives the following error:
org.postgresql.util.PSQLException: ERROR: column timedprice3_.implementing_class does not exist
An extra note, without the parantheses, I get the error:
org.postgresql.util.PSQLException: ERROR: syntax error at or near "SELECT"
Any ideas as to how to fix this?
EDIT:
To add what I've found so far:
Using either one, if I do SELECT *, and then do SELECT implementing_class outside, it gives a different error. Full code:
(SELECT implementing_class FROM (SELECT * FROM price_feed_type INNER JOIN price_feed ON price_feed.price_feed_type_id = price_feed_type.price_feed_type_id) AS foo)
The error is:
org.postgresql.util.PSQLException: ERROR: syntax error at or near "."

Categories