An inconsistency with SQL in ORACLE - java

When I do the following query I get this error message:
ORA-29913: error in executing ODCIEXTTABLEFETCH callout
ORA-01722: invalid number
The query is
select l.LanguageName as language
from mpi_provider.mpiprovider m, mpi_provider.provlanguage l
where m.providerid = l.providerattestid and m.npi = 1548204944
I thought maybe it was just too high of a number, but when I use both/either
m.npi = 1548204943 or m.npi = 1548204945 the query works fine (though there are no records found). If I add another digit, m.npi = 15482049449, it works fine as well as if I cut off a digit.
I can't figure out what is going on. Any ideas?
npi is defined as Number(10) and can be null

Related

JDBC SqlSyntaxErrorException but statement works in SQL tool

I have a DB2 MERGE INTO statement that gives me a sql syntax exception when I try to execute it from Java, but when I put in the parameters in my SQL tool it runs just fine. I've run this repeatedly both in my SQL tool and in Java, and it always works in the former but fails in the latter. This is the SQL (table and column names have been changed to protect the innocent):
merge INTO sales_table AS target
USING (VALUES ( CAST('2020' AS CHAR(4)), CAST('12' AS CHAR(2)), CAST('AB01' AS CHAR(4)), CAST
(0555.0 AS
DECIMAL(9)), CAST(0.000 AS DECIMAL(9)), CAST('DP079616' AS CHAR(8)) )) AS input_data (
year, month, location, no, orig_no, last_upd_userid )
ON ( target.year = input_data.year
AND target.month = input_data.month
AND target.location = input_data.location )
WHEN matched THEN
UPDATE SET no = input_data.no,
orig_no = input_data.orig_no,
last_upd_userid = input_data.last_upd_userid,
last_upd_tmstmp = CURRENT TIMESTAMP
WHEN NOT matched THEN
INSERT ( year,
month,
location,
no,
orig_no,
creation_userid,
creation_tmstmp,
last_upd_userid,
last_upd_tmstmp )
VALUES ( input_data.year,
input_data.month,
input_data.location,
input_data.no,
input_data.orig_no,
input_data.last_upd_userid,
CURRENT TIMESTAMP,
input_data.last_upd_userid,
CURRENT TIMESTAMP )
In the query above I have replaces the ?s with hard-coded parameters that Java sets with the set... methods.
I get this exception in Java:
com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-270, SQLSTATE=42997, SQLERRMC=null, DRIVER=4.21.29
This is the relevant Java part:
req.setString(i++, roaa.getYear());
req.setString(i++, roaa.getMonth());
req.setString(i++, roaa.getLocatoin());
req.setFloat(i++, Float.parseFloat(roaa.getSalesNumber()));
req.setFloat(i++, Float.parseFloat(roaa.getOrigSalesNumber()));
req.setString(i++, userId);
req.executeUpdate();
Any thoughts on why I'd get a syntax exception on something that works in my SQL tool? Obviously the syntax is right if it works. I've Googled the SQL code -270, and various results say it's because it violates a constraint somewhere, but again, it works in the SQL tool, so I can't help thinking I'm overlooking something simple that should be obvious to me.

I keep getting SQLError using sql request built part by part

I have a JavaFX App where the user inputs some informations about him and then I have a class that is named DbUpdateBuilderthat allows me to create my sql resquest more easily. And then I use statement.executeUpdate(request)
The issue is that I always get this error :
java.sql.SQLSyntaxErrorException: syntax error near " line 1 So I thought something was wrong my request so I printed the request I got :
UPDATE `users` SET `users`.`first_name`=null,`users`.`last_name`=null,`users`.`age`=null,`users`.`role`=null,`users`.`gender`= 3 WHERE `users`.`id` LIKE 10
which works if I hard code it.
this do not work
// Some code
Statement stmt = con.createStatement();
String request = builder.getRequest();
System.out.println(request); // where the request in the
// working exemple comes from
if(stmt.executeUpdate(request) > 0) { // I get the error from this
// line
return UPDATE_SUCCESS;
}
return SQL_ERROR_OCCURED;
but this do work
Statement stmt = con.createStatement();
String request = builder.getRequest();
System.out.println(request);
if(stmt.executeUpdate("UPDATE `users` SET `users`.`first_name`=null,`users`.`last_name`=null,`users`.`age`=null,`users`.`role`=null,`users`.`gender`= 3 WHERE `users`.`id` LIKE 10") > 0) {
return UPDATE_SUCCESS;
}
return SQL_ERROR_OCCURED;
I must precize that the request hard coded in the working example comes from a copy & paste of the request printed in the not working example
Just a suggestion
LIKE work on string and normally is used with wild char
could be you need
if you need exact match then use equal
UPDATE `users`
SET `users`.`first_name`=null,
`users`.`last_name`=null,
`users`.`age`=null,
`users`.`role`=null,
`users`.`gender`= 3
WHERE `users`.`id` = 10
if you need a pattern match the use like and wildchar eg;
UPDATE `users`
SET `users`.`first_name`=null,
`users`.`last_name`=null,
`users`.`age`=null,
`users`.`role`=null,
`users`.`gender`= 3
WHERE `users`.`id` LIKE '%10%'
Thanks for your suggestion #scaisEdge. But I found the issue on my own : I compared the two strings (the one hard coded and the one generated by my builder) they had the same lenght so I printed char by char each string and then I found the issue ! some chars were equal to null so SQL didn't like it and generated error but when I printed it null chars were ignored...
Sometimes quotes ' are not acceptable by mysql but you can try this as simple with no qoutes
UPDATE users
SET first_name=null,
last_name=null,
age=null,
role=null,
gender='3'
WHERE id LIKE '%10%'
And make sure your datatypes are correct, also null is equal to empty you can try change it to '' like first_name=''

Oracle Package or function ... is in an invalid state

I stuck with Oracle store procedure calling. The code looks simple, but I seriously don't know how to make it work.
This is my code for creating the procedure
DELIMITER ##
CREATE OR REPLACE PROCEDURE updateAward(_total_amount in Number, _no_of_sales in Number, _agent in NUMBER, _id in NUMBER) AS
BEGIN
update Award set total_amount = _total_amount, no_of_sales = _no_of_sales, agent_id = _agent where ID = _id ##
commit ##
So, when I execute it through NetBean (it is the only tool I have at this moment), the code run well.
I also tried to run the compile statement
alter PROCEDURE updateAward compile;
and then, use
select *
from user_errors
where name = 'ORG_SPGETTYPE'
The select return empty, proving that the compile process is ok. However, when I trigger the procedure
call updateAward(1,1,1,1);
It returns the error
Package or function UPDATEAWARD is in an invalid state
and the command
SELECT object_name FROM user_objects WHERE status='INVALID';
return the name of the procedure. How can I solve this problem ?
Update 1:
if I use
BEGIN
updateAward(1,1,1,1);
End;
I got error
Error code 6550, SQL state 65000: ORA-06550: line 2, column 20:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
:= . ( % ;
Update 2:
The reason I put the deliminator is because i got error with ";" when working through some vpn to the other network (still not sure why). So, i updated the code like your answer, but then, with the End; in the end of the procedure and then, get the Invalid SQL statement1. If i remove it and execute (through Netbean), the procedure is created successfully. However, after compiling and check the user_errors, it got the
"PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following: ; "
First things first, your procedure syntax looks wrong. Don't use DELIMITER as that syntax is specific to MySQL. Instead, try something like the following.
CREATE OR REPLACE PROCEDURE updateAward(_total_amount in Number, _no_of_sales in Number, _agent in NUMBER, _id in NUMBER) AS
BEGIN
update Award set total_amount = _total_amount, no_of_sales = _no_of_sales, agent_id = _agent where ID = _id;
commit;
END;
Firstly, there are a couple of things wrong with your procedure:
You're not using delimiters correctly. Delimiters should be used to terminate the whole procedure, not each line.
The NetBeans SQL window doesn't know SQL very well so it cannot tell when the procedure ends and something else begins. Normally, it uses semicolons (;) to tell when one statement ends and another begins, but stored procedures can contain semicolons within them so that wouldn't work. Instead, we change the delimiter to something else so that the NetBeans SQL window sends the entire stored procedure to the database in one go.
Variable names are not allowed to begin with an underscore (_). In particular, rule 5 in the list of Schema Object Naming Rules at this Oracle documentation page states that
Nonquoted identifiers must begin with an alphabetic character from your database character set.
Underscores are not alphabetic characters.
I've taken your procedure, fixed the use of delimiters and added an extra p onto the front of each parameter name (p for 'parameter'), and I got the following, which ran successfully in NetBeans and created a procedure without errors:
delimiter $$
CREATE OR REPLACE PROCEDURE updateAward(p_total_amount in Number, p_no_of_sales in Number, p_agent in NUMBER, p_id in NUMBER) AS
BEGIN
update Award set total_amount = p_total_amount, no_of_sales = p_no_of_sales, agent_id = p_agent where ID = p_id;
commit;
END;
$$
delimiter ;
Secondly, you write
[...] and then, use
select *
from user_errors
where name = 'ORG_SPGETTYPE'
The select return empty, proving that the compile process is ok.
Um, no. This proves that there are no errors in the procedure ORG_SPGETTYPE (or no procedure with that name exists). Your procedure is named updateAward, which Oracle will capitalise to UPDATEAWARD. Try
select *
from user_errors
where name = 'UPDATEAWARD';
instead.

SQL Firebird implementation in java/ IBSQL

so tried to put that SQL code into my java-aplication:
SELECT DISTINCT
StRzImRo.Rohstoff, StRo.Bezeichnung,
CAST (SUM(BwLsImAt.Lieferungen * StRzImRo.Menge * StAt.PROD__REZEPTURGEWICHT / Coalesce(StRz.PARM__BEZUGSGROESSE,1)) AS NUMERIC (9,3)) Rohstoffverbrauch_Gesamt FROM BwLsImAt
JOIN StAt ON (StAt.IntRowId = BwLsImAt.Artikel)
JOIN StRz ON (StRz.IntRowId = StAt.PROD__REZEPTUR)
JOIN StRzImRo ON (StRzImRo.Master = StRz.IntRowId)
JOIN StRo ON (StRzImRo.Rohstoff = StRo.IntRowId)
WHERE StAt.IntRowId > 0
GROUP BY StRzImRo.Rohstoff, StRo.Bezeichnung
-- GROUP BY StRzImRo.Rohstoff, StRzImRo.Menge, StAt.PROD__REZEPTURGEWICHT, Coalesce(StRz.PARM__BEZUGSGROESSE,1)
The code is fully funcional and tested in IBSQL but not working in my java-application.
My app does work properly with other code. I get this error:
org.firebirdsql.jdbc.FBSQLException: GDS Exception. 335544569. Dynamic SQL Error
SQL error code = -104
Token unknown - line 1, column 266
ON
I would be very happy if someone could help me with this problem. Thanks!
P.S.: Sorry for my bad language, but i´m not a native speaker
The error suggests there is an ON in an unexpected place in your query, and as the query itself looks fine, my guess is the problem is with the way you construct the query in your Java application. There might be some whitespace missing in your query.
My guess is that you have something like
query = "SELECT * " +
"FROM table1" +
"JOIN table2 ON " //.....
The missing whitespace will make the SQL:
SELECT * FROM table1JOIN table2 ON ....
For the parser, this is perfectly valid until it encounters the ON token, which triggers the error. Eg the parser identifies it is a SELECT with * (all) columns from table1JOIN with alias table2. During parsing the server doesn't check if the table actually exists, so it doesn't trip over the fact that table1JOIN doesn't exist. That is checked after parsing is successfully completed.

Firebird SQL Query with # (at) sign - How to run the query in JDBC (Jaybird)?

I have an application that uses Firebird. The application performs a long list of queries e.g. each time you list your items. I want to take out these queries, and run them in my own Java application (so I can manipulate the list, display it, and so on.)
The problem is... there is a debug option in the application where you can see what kind of queries does your application run. Some of the original queries got # signs. If I run a query with an # in it, I get an error. If I take out that part of the query, everything runs and works "as expected". No errors, like a charm.
Detailed error message:
Error code: -104
Token unknown - line 8, column 32
We use IntelliJ IDEA which automatically applies escape characters when needed.
Such a part from the original query:
SELECT TBL4487."Id" "database.id",
TBL4487."Code" "database.code",
TBL4487."Name" "database.name",
TBL4487."Barcode" "database.barcode",
TBL4488."Name" "Datagroup",
TBL4489."Name" "Mey",
(SELECT FIRST 1 TBL4494."Price" / (CASE
WHEN (TBL4487."GrossPrices" = #Param4495) THEN 1
ELSE (TBL4492."Rate" + 100) / 100
END) "productprice.price"
FROM "ProductPrice" TBL4494
WHERE (TBL4494."Product" = TBL4487."Id") AND (TBL4494."PriceCategory" = #Param4497) AND (TBL4494."ValidFrom" <= #Param4498) AND (TBL4494."Currency" = #Param4499) AND (TBL4494."QuantityUnit" = TBL4487."QuantityUnit")
ORDER BY TBL4494."ValidFrom" DESC) "xyz",
(SELECT FIRST 1 TBL4500."Price" / (CASE
WHEN (TBL4487."GrossPrices" = #Param4501) THEN 1
ELSE (TBL4492."Rate" + 100) / 100
The question is.. how could I run this query? How do I replace the # symbol?
You can't run this query directly with Jaybird. These #ParamXXXX seem to be placeholders in the query for parameters. However Firebird nor Jaybird supports this type of placeholders (they only support ? as placeholder in DSQL).
To execute this with Jaybird, you will need to replace each instance of the #ParamXXXX either with a ? and set the right value for each placeholder in a PreparedStatement, or with the actual value in the query text itself.
The Firebird .NET provider does support #....-style placeholders (it translates them to Firebird-style ? placeholders), so you could try to use C#/.NET instead if you don't want to do replacing yourself.
Full disclosure: I am the developer of Jaybird

Categories