Query failing on Oracle 12c and 18c when changing NLS_SORT to BINARY_CI - oracle

I have the following query which correctly returns records when I execute it via code or Oracle SQL Developer.
SELECT TABLE_T.COL_P,
1234 AS COL_C,
TABLE_T.COL_D,
SUM(SOME_COLUMN) Value
FROM TABLE_T
INNER JOIN TABLE_E E ON TABLE_T.COL_P = E.COL_P
AND TABLE_T.COL_C = E.COL_C
AND TABLE_T.COL_CC = E.COL_CC
AND TABLE_T.COL_CL = E.COL_CL
INNER JOIN TABLE_C C1 ON C1.COL_P = E.COL_P
AND C1.COL_C = E.COL_C
INNER JOIN TABLE_C C2 ON C2.COL_P = C1.COL_P
AND C2.COL_CX = C1.COL_CX
AND C2.COL_CY = C1.COL_CY
AND C2.COL_CZ = C1.COL_CZ
WHERE TABLE_T.COL_P = 'Some Text'
AND C2.COL_C = 1234
AND TABLE_T.COL_CL IN
(SELECT COL_CL
FROM TABLE_CL
WHERE COL_P = 'Some Text'
AND ((COL_CLTYPE = 'VALUE_A')
OR (COL_CLTYPE = 'VALUE_B')
OR (COL_CLTYPE = 'VALUE_C')
OR (COL_CLTYPE = 'VALUE_D')) )
GROUP BY TABLE_T.COL_P,
TABLE_T.COL_D
However, it fails to return records once I execute the following session commands:
ALTER SESSION SET NLS_COMP = LINGUISTIC;
ALTER SESSION SET NLS_SORT = BINARY_CI;
This problem only occurs when I'm running against an Oracle 12c or 18c database.
It works find with/without the session commands when running against an Oracle 12C R2 or 11g database.
I've already checked the Explain Plan for 12c/18c and 12cR2 and its creating the same plan.
I found out that by adding an ORDER BY clause to the query (ORDER BY TABLE_T.COL_D, it resolves the problem.
Any ideas on what might be causing this problem?
I know the ORDER BY solution works, but I'd like to know what the underlying cause is and if there's a better solution to it.

Related

Oracle Update Statement Using a Join

I need some help with this query. It works fine in SSMS, but will not work in Oracle. How can I rewrite this so it will work in Oracle? I really appreciate the help.
UPDATE reservation_Daily_elements e
SET e.MARKET_CODE = 'PHEE',
e.ORIGIN_OF_BOOKING = 'DESKTOP'
FROM reservation_Daily_elements e
LEFT JOIN reservation_daily_element_name dn
ON e.resv_daily_el_seq = dn.resv_daily_el_seq
WHERE dn.RESV_NAME_ID IN ('3747957');
Commit;
How about MERGE?
merge into reservation_daily_elements e
using (select dn.resv_daily_el_seq
from reservatioin_daily_element_name dn
where dn.resv_name_id = '3747957'
) x
on (e.resv_daily_el_seq = x.resv_daily_el_seq)
when matched then update set
e.market_code = 'PHEE',
e.origin_of_booking = 'DESKTOP';
If it has to be UPDATE, then
update reservation_daily_elements e set
e.market_code = 'PHEE',
e.origin_of_booking = 'DESKTOP'
where exists (select null
from reservation_daily_element_name dn
where dn.resv_daily_el_seq = e.resv_daily_el_seq
and dn.resv_name = '3747957'
);
Btw, oracle supports updatable inline views, so you can use simple update (Select... From t1 join t2 on...) set... .
Also as I see your 'left join' is really 'inner join' because of the predicates in where clause.

Using Merge in Oracle for updating a parent table using Joins

I have a SQL statement as below, I couldn't execute the statement in Oracle as it doesn't support Joins in Update. I have tried using updating using a temp table and completed the job, however, I would like to use the Merge statement and failed to write the code.
Let me know if there is a possibility in Oracle use joins in an update which doesn't add any performance overhead.
UPDATE A SET
YearStartPD = B.PERIOD_CODE,
YearEndPD = A.PERIOD_CODE,
PY_YearStartPD = C.PERIOD_CODE,
PY_YearEndPD = A.PY_WeekEndPD
FROM dbo.DIM_TIME A
INNER JOIN
(
select PERIOD_CODE,WEEK_PERIOD_CODE,YEAR_CODE from dbo.DIM_TIME
WHERE WEEK_PERIOD_CODE = '01'
) B ON B.Year_Code = A.Year_Code
LEFT JOIN
(
select PERIOD_CODE,WEEK_PERIOD_CODE,YEAR_CODE from dbo.DIM_TIME
WHERE WEEK_PERIOD_CODE = '01'
) C ON C.Year_Code = A.Year_Code - 1

Oracle 18.1 sql query error

I have an sql query
SELECT ("SL/VL".TOTAL_SICK_LEAVE - SUM(EMPLOYEE_INFO.DAYS_TAKEN_SICK))
FROM EMPLOYEE_INFO
INNER JOIN "SL/VL"
ON EMPLOYEE_INFO.EMPLOYEE_NAME = "SL/VL".EMPLOYEE_NAME
where contract_year='Year 1'and
employee_info.EMPLOYEE_NAME = :P4_EMPLOYEE_NAME
GROUP BY "SL/VL".TOTAL_SICK_LEAVE
which was giving me the desired results when I had oracle apex 5.1. Now I upgraded my database to oracle 18.1 I am getting the"ORA-20999: Column name "("SL/VL".TOTAL_SICK_LEAVE-SUM(EMPLOYEE_INFO.DAYS_TAKEN_SICK))" is invalid for the LOV SQL query. Make sure that you use valid alias names for your columns."
It appears that 18.1 wants a column alias on the expression ("SL/VL".TOTAL_SICK_LEAVE - SUM(EMPLOYEE_INFO.DAYS_TAKEN_SICK)). Perhaps if you redo your SQL as
SELECT (s.TOTAL_SICK_LEAVE - SUM(e.DAYS_TAKEN_SICK)) AS SICK_LEAVE_REMAINING
FROM EMPLOYEE_INFO e
INNER JOIN "SL/VL" s
ON e.EMPLOYEE_NAME = s.EMPLOYEE_NAME
WHERE CONTRACT_YEAR = 'Year 1' AND
e.EMPLOYEE_NAME = :P4_EMPLOYEE_NAME
GROUP BY s.TOTAL_SICK_LEAVE
the database will be happier.

Query results in Oracle SQL Developer are different to those using .Net ODP

I have this query:
SELECT
case
when
AddressType IN (select code.codeid from code where code.codeset = AddressTypeCodeSet and code.description like 'Postal%')
then 'PostalAddress'
when
AddressType IN (select code.codeid from code where code.codeset = AddressTypeCodeSet and code.description like 'Email%')
then
'EmailAddress'
else
'OtherAddress'
end as AddressType
FROM VW_Address
WHERE AddressId=190000;
When I execute this query using SQL Developer, the result returned is OtherAddress. This is what I'm expecting. However, when I execute the same query via this code:
Dim cmd =
New Oracle.DataAccess.Client.OracleCommand(["SQL HERE]")
cmd.Parameters.Add(":p0", OracleDbType.Int32)
cmd.Parameters(0).Value = 190000
cmd.BindByName = True
Dim r = cmd.ExecuteReader()
I get a different result: EmailAddress
ODP is version 2.111.7.20, Oracle is 11g.

ORA-01779 on Join Update

I'm developing a web application on vb.net with Oracle as DB and I'm having some problems to update a table by using joins.
Since I'm kind of a newbie on Oracle, I can't make my query go on...i've been reading and I knew that oracle doesn't supports Joins on UPDATE, so i searched for alternatives and I finished on this:
UPDATE (SELECT pda.id_propuesta
FROM abd_prop_det_archivos pda
INNER JOIN abd_participantes_invitados pi
ON (pda.id_solicitud = pi.id_solicitud)
AND pi.id_solicitud = :id_solicitud
AND pi.rut = :rut)
SET id_propuesta = :id_propuesta
I'm using Oracle 11g.
It appears that you want a WHERE EXISTS clause
UPDATE abd_prop_det_archivos pda
SET ID_PROPUESTA = :ID_PROPUESTA
WHERE EXISTS( SELECT 1
FROM ABD_PARTICIPANTES_INVITADOS pi
WHERE pda.id_solicitud = pi.id_solicitud)
AND pi.id_solicitud = :id_solicitud
AND PI.RUT = :RUT )

Resources