Table,View Or Sequence reference 'SEQUENCE.NEXTVAL' not allowed in this context - sql

I'm having this error while trying to compile the following trigger :
CREATE OR REPLACE TRIGGER INCREMENTER_ID_CONSISTANCE
BEFORE INSERT ON BD.CONSISTANCE
for each row
BEGIN
:new.code := ID_CONSISTANCE.nextval;
END;
**ERROR** : Table,View Or Sequence reference 'ID_CONSISTANCE.nextval' not allowed in
this context
What is the problem here ? and how can I fix this ?

This syntax is only allowed in Oracle 11 or later.
The (unsupported and outdated) 9i version did not support direct assignment of a sequence value.
You need to use a select into instead:
select ID_CONSISTANCE.nextval
into :new.code
FROM dual;
And you should really plan an upgrade to a current version of Oracle (11.x or 12.x)

Related

Problem on creating postgres stored procedure | Postgres 12

I'm kinda new on Postgres, so I need your help for this one.
postgres=# select version();
version
------------------------------------------------------------
PostgreSQL 12.3, compiled by Visual C++ build 1914, 64-bit
(1 row)
1. CREATE PROCEDURE test (INT,varchar(200))
2. LANGUAGE plpgsql
3. AS $$
4. BEGIN
5.
6. create table test1 as
7. select id,name from mst_user_mobile limit 5
8.
9. COMMIT;
10. END;
11. $$;
From what I read, Postgres version above 10, support PROCEDURE method. But when I execute the code it always error on line 1 (on word PROCEDURE)
here error that i got:
ERROR: syntax error at or near "PROCEDURE"
LINE 1: CREATE PROCEDURE test (INT,varchar(200))
^
SQL state: 42601
Character: 8
any helps are welcome
You report to be using Postgres 12.3. Yet, the reported error message is exactly what I see in Postgres 10 (or older):
ERROR: syntax error at or near "PROCEDURE"
LINE 1: CREATE PROCEDURE test (INT,varchar(200))
db<>fiddle here
CREATE PROCEDURE was introduced with Postgres 11.
I suspect you are connected to the wrong / a different database.
All that aside, your example could just be a function. The COMMIT is pointless as last command. See:
In PostgreSQL, what is the difference between a “Stored Procedure” and other types of functions?
Or, it's another missing semicolon before the CREATE PROCEDURE command. (You show another one of those in the question.) I can reproduce the error message this way, too:
db<>fiddle here
I guess the problem are the ; on END and the last $$. You shouldn't use them in this cases!

ORA 00920 In CASE with "OR" inside Trigger Oracle

The syntax error seems to be in the WHEN INSERTING OR UPDATING (more specifically it underlines the OR), I don't understand why it doesn't work in a CASE condition but it works in a IF condition.
Its a BEFORE DELETE OR INSERT OR UPDATE Trigger
Code portion of the issue:
SELECT
field
INTO
my_var
FROM
my_table
WHERE
column1 = (CASE
WHEN INSERTING OR UPDATING THEN
:new.column2
ELSE --deleting
:old.column2
END);
What would be the solution?
Here is the rest of the trigger if anyone wants to test : https://pastebin.com/AJqGQyG8
Edit :
The issue seems to be that the WHEN condition needs an operator, so I tried using :
WHEN INSERTING = TRUE
But that just resulted in another error :
ORA-00904: "TRUE": Invalid identified
As Thorsten explained already, inserting and updating and deleting are predicates (expressions of BOOLEAN data type) which exist only in the PL/SQL code part of the trigger. They can't exist (or be used in any way) in a SQL statement, since Oracle SQL does not recognize/implement the BOOLEAN data type.
Moreover, :new.<whatever> and :old.whatever are also available only in the PL/SQL portion of your trigger; if you have embedded SQL, anything like :new.<whatever> is interpreted as a bind variable, not special in any way, and you will be prompted for a value (if you use an "advanced" front-end program) or you will simply get an error telling you that not all variables are bound.
Thorsten has already shown one way to do what you want to do. If you really need (want?) to use a case expression, you can do something like this:
Declare a variable (like v_value in Thorsten's answer) in the DECLARE section of the trigger. Assign the proper data type to it.
Then in the execution block write something like this, BEFORE the SQL statement:
v_value := case when inserting or updating then :new.column2 else :old.column2 end;
and then in the SQL statement compare to v_value, also as in Thorsten's answer.
Alternatively, you can assign the value to v_value in the DECLARE block, right as you declare it, in the usual (PL/SQL) way. In any case, the value must be assigned outside the SQL statement, not in it.
I think the problem is that PL/SQL knows these boolean variables inside a trigger, but Oracle's SQL doesn't know BOOLEAN unfortunately.
One solution may be:
IF INSERTING OR UPDATING THEN
v_value := :new.column2;
ELSE
v_value := :old.column2;
END IF;
...
SELECT field INTO my_var FROM my_table WHERE column1 = v_value;

Oracle: 'populate pending' index of another schema

When attempting to call ctx_ddl.populate_pending on an index of another schema...
call ctx_ddl.populate_pending ('OTHERSCHEMA.INDEX_NAME', null);
... I'm getting an Oracle error:
SQL-Fehler: ORA-20000: Oracle Text error:
DRG-10502: index INDEX_NAME does not exist
When I connect as OTHERSCHEMA user and execute the same statement, everything works fine.
Why does it tell me the index doesn't exist (it does, verified) here?
Am I missing any grants or anything else?
Constraint for proposed solutions: I don't want to have to use 'alter session' as a workaround.
EDIT:
Seems to be a bug in Oracle 11.2. With Oracle 12.1, the statement works fine. Treat the solution below as a workaround for Oracle 11.2.
Solved it with a delegation to a procedure in the target schema:
On target schema 'OTHERSCHEMA'
CREATE OR REPLACE PROCEDURE POPULATE_PENDING_INDEX IS
BEGIN
execute immediate 'call ctx_ddl.populate_pending(''INDEX_NAME'', NULL)';
END;
/
Execute with another schema user:
exec OTHERSCHEMA.POPULATE_PENDING_INDEX

Oracle dynamic sql, works everywhere but at customers

I have a stored procedure which executes some dynamic sql, shown below. I've tried to cut it down as much as possible so ignore any little errors.
In the office it works, everytime, on 11.2, 10.2, 10.1. At the customers it fails with a message:
Unexpected Error
Error Message = "Msg:
MyProc
ORA-06550: line 1, column 1:
PLS-00103: Encountered the symbol "" when expecting one of the following:
begin case declare exit for function goto if loop mod null
If I capture the dynamic sql that the customer is generating and place it in a variable like below, running on work machines, it works, so it's not that dodgy sql is getting generated. Normally the sql comes from the client so here I've doubled the to_date quotes.
mySQL := '
declare
pADMINDATE DATE := :1;
pEMPLOYEEIDLIKE VARCHAR2(40) := :2;
pINCLUDEEMPLOYEE number := :3;
begin
BEGIN OTHERPROC.OTHERPROC (1,TO_DATE(''2012-10-03'', ''YYYY-MM-DD''),TO_DATE(''2012-10-03'', ''YYYY-MM-DD''),0);
END;
INSERT INTO TP_EMPLOYEES (
ID,
EMPLOYEECODE
)
SELECT ROWNUM,
EMPLOYEECODE
FROM (
SELECT EMPLOYEECODE
FROM (SELECT DISTINCT EMPLOYEECODE FROM TP_EEF_TEMP) DISTINCTEMPCODES) A;
end; ';
EXECUTE IMMEDIATE
mySQL
using
pADMINDATE,
pEMPLOYEEIDLIKE,
pINCLUDEEMPLOYEE;
It's not the database version that causes the problem, could it be permissions? It calls another stored procedure within itself which we do regularly in non dynamic sql, could it be that?
At a loss here
Thanks
I have found the "works in this server but doesn't work on that server" type of problems before.
Usually it's related to implicit date <-> varchar conversions: since different database servers can have different default formats, it's possible for a statement with an implicit conversion to work in one place and fail in another.
I suggest you to try running your example removing the pADMINDATE date variable.
It was carriage returns! Adding this line fixed the problem
pQUERY2 := REPLACE(pQUERY, chr(13));
A similar problem was highlighted here
https://forums.oracle.com/forums/thread.jspa?threadID=1117462
Thanks for the answers anyway, if anyone knows I would be interested in knowing what setting/patch/whatever makes Oracle sensitive to this, i.e. I tried this on 3 different versions of oracle including the one the customer was on, -but- I didn't patch my 10.2.1 version.
Is it something that is fixed in a patch? Is it a setting?

Oracle 10gR2 trigger error

I have Oracle 10gR2. I am trying to create autoincrement trigger.
Here is the sample:
CREATE SEQUENCE TEST_SEQ
INCREMENT BY 1
START WITH 1
NOMAXVALUE
/
CREATE TABLE TESTER (
ID_TESTER INTEGER NOT NULL,
VAL VARCHAR2(20) NOT NULL
)
/
CREATE OR REPLACE TRIGGER TIB_TESTER BEFORE INSERT
ON TESTER FOR EACH ROW
BEGIN
SELECT TEST_SEQ.NEXTVAL
INTO :NEW.ID_TESTER
FROM DUAL;
END;
/
Trigger creation gives warning:
warning : ORA-24344: success with
compilation error
And when I get error value:
select OCI_SUCCESS_WITH_INFO;
/
It gives error:
error : ORA-00923: FROM keyword not
found where expected
What client are you using to issue these commands? ORA-24344 is a perculiar error.
In SQL*PLus we can get more information about compilation errors like this:
SQL> show errors
As for the ORA-00923 error, that is because in Oracle's version of SQL we always have to select from a table. So you should execute
select OCI_SUCCESS_WITH_INFO
from dual
/
I'm not sure how much sense that makes, but at least you won't get the error.
"It was Navicat problem"
That doesn't surprise me, as I ran your code against my database and it built without a hitch.
Maybe it will be useful for somebody:
If you are using Oracle 10g and OCI driver, ORA-24344 shows when trigger have carriage return sign (\r) in code eg. file was created with Windows end of line style.