Anshul
Anshul

Reputation: 75

ORA-00904 Invalid Identifier. Query works in 12c but not in 11g

I have a LINQ query, trying to execute it on oracle 11g. The same query executes perfectly fine on 12c but it gives following error on 11g.

ORA-00904: "Extent1"."ID": invalid identifier

00904. 00000 -  "%s: invalid identifier"

*Cause:   
*Action:

Following is the generated SQL. Just showing the specific part of query which is giving error,

SELECT 
  "Extent1".ID,
  (
    SELECT "top".ADMIT_DATE
    FROM 
          ( 
            SELECT "Project7".ADMIT_DATE
              FROM ( SELECT "Extent7".ADMIT_DATE FROM SYS.Table2 "Extent7" WHERE "Extent1".ID = "Extent7".ID )"Project7"
              ORDER BY "Project7".ADMIT_DATE ASC
          )  "top"
    WHERE ( ROWNUM <= 1) AND ROWNUM <= 1
  ) 
AS C1 FROM SYS.Table1 "Extent1";

Can someone please explain what's causing the issue in 11g !! This query works fine on 12c.

UPDATE: Here are the linq queries. Above SQL is a part of SQL generated by these linq queries.

var query = from p in context.Person
            where p.Meetings.Any() &&
            p.Vitals.Any(v => v.WT.HasValue && v.WT >= 20 && v.WT <= 300) &&
            p.Detentions.Any(d => ((d.DX_TYPE != null && d.DX_TYPE == "09") || (d.DX_TYPE == null && "09" == null)) && d.DX.StartsWith("V") && d.DX != null) &&
            p.Meetings.Any(meet => meet.Detentions.Any(d => ((d.DX_TYPE != null && d.DX_TYPE == "09") || (d.DX_TYPE == null && "09" == null)) && d.DX.StartsWith("250") && d.DX != null))
            select new
            {
                p.HISPANIC,
                p.RACE,
                StudentID = p.PersonID,
                AdmissionOn = p.Meetings.OrderBy(meet => meet.ADMIT_DATE).Select(meet => meet.ADMIT_DATE).FirstOrDefault(),
                AdmissionYear = p.Meetings.OrderBy(meet => meet.ADMIT_DATE).Select(meet => meet.ADMIT_DATE).FirstOrDefault().Year,
                AdmissionMonth = p.Meetings.OrderBy(meet => meet.ADMIT_DATE).Select(meet => meet.ADMIT_DATE).FirstOrDefault().Month
            };

var g = query.GroupBy(k => new { Hispanic = k.HISPANIC, Race = k.RACE, AdmissionYear = k.AdmissionYear })
            .Select(k => new { Hispanic = k.Key.Hispanic, Race = k.Key.Race, AdmissionYear = k.Key.AdmissionYear, Persons = k.Count() }).ToList();

Upvotes: 0

Views: 1491

Answers (1)

Worthy7
Worthy7

Reputation: 1561

The issue is that Oracle 11g doesn't support referencing identifiers more than one query deep. SQL Server (created by Microsoft) does support this, which is why your LINQ is working fine, but the Oracle server doesn't like the SQL.

It is, essentially, a bug in oracle provider tools, which are not translating your LINQ into valid SQL for version 11g.

Upvotes: 2

Related Questions