From section "6 Coding PL/SQL Subprograms and Packages" in the "Oracle Database Advanced Application Developer's Guide, 11g Release 2 (11.2), E41502-05"
To create without errors (to compile the subprogram or package successfully) requires these additional privileges:
- The owner of the subprogram or package must be explicitly granted the necessary object privileges for all objects referenced within the body of the code.
- The owner cannot obtain required privileges through roles.
You must have granted select permission on this table to the schema that owns the package.
It is important that this permissions are granted directly to the package owner and not
indirect using a role. Using a role is sufficient to select the table directly by Toad
but it is not sufficient if you want to select the table in a package.
As mentioned in the comments, Oracle doesn't like the square brackets around identifiers, and it also doesn't support TOP n clauses, however, you can work around that with a couple of simple changes:
SELECT T1.*,
(SELECT T3.SMN_DATEC-1
FROM
(SELECT T2.DET_NUMBERA
, T2.SMN_DATEC
, row_number() over (ORDER BY DET_NUMBERA,SMN_DATEC) AS top
FROM CHRISCS_EMSAL T2
) T3
WHERE T3.TOP=1
AND T3.DET_NUMBERA = T1.DET_NUMBERA
AND T3.SMN_DATEC > T1.SMN_DATEC
) AS EndDate,
T1.DET_NUMBERA,
T1.SMN_DATEC
FROM CHRISCS_EMSAL T1
ORDER BY T1.DET_NUMBERA,
T1.SMN_DATEC;
First off remove the square brackets, then move your ORDER BY
clause in T3 to a ROW_NUMBER
analytic function aliased as TOP
, then in the outer query of your scalar valued subquery (aliased as EndDate
) change the TOP 1
clause in the select statement to a TOP=1
predicate in the where clause.
Also remove the AS
from table aliases, Oracle doesn't allow it (but it's OK in column aliases.)
On closer inspection of your code it looks like your scalar valued subquery is just getting the next chronological
DET_NUMERA
's
SMN_DATEC
and subtracting 1 day from it. That's something really easy to do with Oracle analytic functions without all the scalar valued subqueries:
SELECT T1.*
, LEAD(T1.SMN_DATEC-1) OVER (PARTITION BY T1.DET_NUMBERA
ORDER BY T1.SMN_DATEC) EndDate
, T1.DET_NUMBERA
, T1.SMN_DATEC
FROM CHRISCS_EMSAL T1
ORDER BY T1.DET_NUMBERA,
T1.SMN_DATEC;
Best Answer
When ORA-00904: invalid identifier occurs, we must enter a valid column name as it is either missing or the one entered is invalid. This error most commonly happens when we are referencing an invalid alias in a select statement. Oracle's voice on ORA-00904 error:
To avoid ORA-00904, column names must