SYS_B_x in Oracle query result column name - oracle

I've faced with some weird behaviour of Oracle 11 DB.
I have a simple query selecting data from a view:
select
(case when exists(select 1 from MY_VIEW v where v.func_name = 'NAME') then 1 else 0 end)
from dual
MY_VIEW looks like:
CREATE OR REPLACE VIEW MY_VIEW
AS
SELECT a.object_name AS func_name, NULL AS func_desc
FROM USER_ARGUMENTS a
In the result set, I receive result with columnName: (CASEWHENEXISTS(SELECT:"SYS_B_0"FROMMY_VIEWVWHEREV.FUNC_VALUE=:"SYS_B_1")THEN:"SYS_B_2"ELSE:"SYS_B_3"END)
And when I'm trying to execute this query by Hibernate, it falls with error
SQL Error: 17133 "Invalid identifier or literal"
while extracting column alias.
Error causes when I'm using ojdbc8 driver, with ojdbc6 driver all is OK.
Strange thing is that I have problems only when I'm querying from this particular view.
When I made same query from another View, created from simple table, everything was ok.
Furthermore, I have another Oracle 11 DB and there result set column name for this query looks common:
(CASEWHENEXISTS(SELECT1FROMMY_VIEWVWHEREV.FUNC_VALUE='NAME')THEN1ELSE0END)
My question is: why and when Oracle replaces values in query string on :"SYS_B_x" ?

First of all, you need to add an alias for that column.
You get :SYS_B_x because of the parameter cursor_sharing=force on that database.

Related

Error with a user defined report in Oracel SQL Developer - Missing IN or OUT parameter at index:: 1

I have created a user defined report within Oracle SQL Developer, with a bind variable.
The report consists of a master_report (style is table), child_report_a containing with a style of 'table' and child_report_b which is the same query, but with the style of 'script'.
I am able to select a cell/row of my master report, and child_report_a data changes accordingly (ie, it returns the cells of the same date I selected.
However, when I try to view this in child_report_b (With the 'script' style) it errors with "Missing IN or OUT parameter at index:: 1".
So the setup is:
tablea
id_pk (number)
name_pk (varchar2)
1
Jack
2
John
3
Amy
tableb
id_fk (number)
start_time(timestamp(6))
1
01-JAN-23 12.00.00.123000000
2
01-JAN-23 13.00.00.123000000
3
02-JAN-23 14.05.00.123000000
User Defined Report:
master_report (Style=Table):
SELECT * FROM tablea
child_report_a (Style=Table) & child_report_b (Style=Script):
SELECT * FROM tablea a INNER JOIN tableb b ON b.id_fk = a.id_pk WHERE trunc(start_time) = trunc(to_timestamp(:STARTTIME)
Any help is appreciated.
EDIT: Included better example of setup.
My goal is to be able to select a row from the master_report, and the child_report_b would return all results which match the date (as currently happens in child_report_a) in the script format.
Welcome to StackOverflow!
The reporting feature is one of my favorites in SQL Developer, so I will try to get you started.
We can't technically answer your question without a fair bit of guessing. We know what's happening on one side of the JOIN, you have a timestamp that you're using TRUNC on.
But we can't tell what is on the other side of the equality predicate in your JOIN.
Here's a working example, see if this helps you.
Parent Query
select trunc(systimestamp) A
from dual
Child Query
select object_name, object_type, last_ddl_time
from user_objects where :A > (trunc(systimestamp) - 30)
And the report -
To debug your report, substitute the bind :STARTTIME with a literal value that is equivalent to what your parent query would return. If that works, so should your report.
Disclaimer: I work for Oracle and am a product manager for SQL Developer.

Oracle.ManagedDataAccess.Client ORA-00903 invalid table name

I'm using the sqldeveloper and have one database connection with the following connection string:
MES#//localhost:1521/xepdb1
MES is the schema owner and a select statement like this shows me what I want to see:
select count(*) from site
I'm also using Visual Studio and I'm trying to connect to the database by using the Oracle.ManagedDataAccess.Client
I'm using exactly the same connection string.
The connect to the database works fine. But I'm always getting the 00903 error.
Any idea what the problem can be ?
Many thanks in advance
I've tried also something like this:
select count(*) from mes.site
If you have used quoted identifiers to create the table then you will need to use quoted identifiers (and use the same case) whenever you access the table.
For example:
CREATE TABLE MES."site" (something NUMBER);
Then you would need to use:
SELECT count(*) FROM MES."site";
or
SELECT count(*) FROM mes."site";
or
SELECT count(*) FROM mEs."site";
The MES schema name is unquoted so you can use any case (and Oracle will implicitly convert it to upper-case to look it up in the data dictionary); however, "site" is a quoted identifier and Oracle will respect the case-sensitivity of the identifier and you MUST use the correct case and the surrounding quotes.
You can see the exact case you need to use in the result of the query:
SELECT owner, table_name FROM all_tables WHERE UPPER(table_name) = 'SITE';
If the data dictionary shows that the table name is upper-case then you can use an unquoted identifier (assuming that all the other naming rules have been respected and the table name is not a reserved/keyword) otherwise you will need to use a quoted identifier.
Normally you would get the ORA-00942: table or view does not exist exception but you can get ORA-00903: invalid table name when you use a keyword for a table name:
CREATE TABLE "NUMBER" (x) AS
SELECT 1 FROM DUAL;
Then:
SELECT COUNT(*) FROM NUMBER;
Give the exception:
ORA-00903: invalid table name
And:
SELECT COUNT(*) FROM "NUMBER";
Works.
However, MES and SITE are not keywords that should trigger that.
fiddle

Entity-framework generated query throws ORA-12704 when using TO_CHAR or TO_NUMBER

I currently face the problem, that I get an exception when executing a query generated by the entity framework.
The query worked until I've joined another table using .include() to the existing entity. Now, whenever I execute the query, I get an Oracle error ORA-12704 character set mismatch.
I narrowed the problem down to the following:
Before joining the table, the generated SQL is a simple query with some join statements. After joining another table, the generated SQL cointains two subqueries which are combined using UNION ALL. In one of the subqueries, a lot of helper-columns are selected.
They look like this:
SELECT
... some other columns...
TO_NUMBER(NULL) AS C1,
TO_CHAR(NULL) AS C2,
...
If I remove those columns and also the corresponding ones in the other subquery, no error is thrown. When I replace the columns with NULL instead of TO_XXX(NULL), the query also works as expected.
Is there any way force the entity-framework not to use these problematic casts?
The problem is caused with the usage of a NVARCHAR2 column in combination with the function TO_CHAR (that returns VARCHAR2 data type) as illustrated below
create table tab
(txt nvarchar2(10));
select txt from tab
union all
select to_char(null) from dual;
ORA-12704: character set mismatch
So your goal is to motivate the tool to generate a query that uses either TO_NCHAR(null) or cast(null as nvarchar2(10)) - both will work.
To do this, you need to add the following data-annotation to the property of the corresponding entity:
[Column(TypeName = "NVARCHAR2")]
The given TypeName must match with the type of the column in the database.
After this addition, the entityframework will generate the correct casts. In this case, the following cast ist generated:
SELECT
...
TO_NCHAR(NULL),
...
You should see no problem with the to_number(null) if the corresponding UNIONcolumn is of a number datatype.

JMeter: Inserting record into database by using JDBC request

I have configured JDBC connection in my JMeter test plan.
The database settings in JDBC connection are configured correctly.
I am extracting employee id from one of my requests response.(i.e Employee_Id)
By using BSF PostProcessor, i have stored the employee id into a variable called as Emp_ID
I want to insert the extracted employee id into my database.
Database used is Oracle SQL developer, Version 4.0.1.14,Build MAIN-14.48.
Table name is : Employee_Details
Column name is : Employee_id ,Datatype: VARCHAR2
In JDBC request,i have selected "Query Type" as "Update Statement" and entered the following query:
Query 1: INSERT INTO Employee_Details (Employee_id)
VALUES (${Emp_ID})
Query 2: INSERT Employee_id='${Emp_ID}'
Parameter Types is given as VARCHAR2.
On both the execution, JMeter displays error as "Cannot create PoolableConnectionFactory (ORA-00923: FROM keyword not found where expected)"
Please provide your valuable suggestion on this.
I found a workaround for this that avoids having problems:
JDBC Request Query Type needs to be: Update Statement
The query needs to be processed as a block
BEGIN
SQL Statement
END;
My expectation is that you have incorrect validation query:
If you left the "Validation Query" box value default - Select 1 - Oracle may not like it very much. You need to change it to select 1 from dual
References:
DBCP - validationQuery for different Databases
The Real Secret to Building a Database Test Plan With JMeter
ORA-00923: FROM keyword not found where expected tips
There are 3 things as follows:
Check the validation query and keep it blank. Screenshot is attached
When you are executing insert query, keep Query Type as Updated
When you are executing query please add database.tablename and then execute.

Oracle 11g "INSERT INTO SELECT" Misbehaving

I am trying to do a very simple thing using the following sample query (can't post the actual query :( )
INSERT INTO Students( id, roll_number, Student_name)
SELECT 1, 2, 'MyName' FROM DUAL;
The ID Column has NOT NULL constraint set. When I execute this query I get the following error:
SQL Error: ORA-01400: cannot insert NULL into ("SCHEMA"."STUDENTS"."ID")
01400. 00000 - "cannot insert NULL into (%s)"
The ID column has datatype NUMBER.
Could anyone help what could be the problem.
Thanks in advance.
What I would do is run the Select part separately, and look at the data that comes back. i.e.
Select First_Field, Second_Field, 'Bob'
From MyTable
Where First_Field = NULL;
What that gives you. You could also do:
Select count(*)
From MyTable
Where coalesce(First_Field,1) =1;
by the way, you said your field is a Numeric, however just FYI. A '' inserted into a varchar2 field gets interpreted as a NULL. Found that out the hard way
Seems like I am creating a habit of answering my own questions...
When I took a closer look into the table I found that there were two ID columns(of course with different names):
One column stores primary key for this table whereas,
other column stores the foreign key as a link to other table.
Thanks everyone for your responses, those made me wonder as to what wrong was I doing.
Thanks a Ton :)

Resources