Oracle Create trigger statement fails with internal error code ORA-00600 - sql

Trying to execute a dumpfile created from Oracle. Everything gets created and altered fine until this block:
CREATE OR REPLACE TRIGGER "LABS"."CHANNEL_CHANNEL_ID_TRG" BEFORE INSERT ON channel
FOR EACH ROW
DECLARE
v_newVal NUMBER(12) := 0;
v_incval NUMBER(12) := 0;
BEGIN
IF INSERTING AND :new.CHANNEL_ID IS NULL THEN
SELECT channel_CHANNEL_ID_SEQ.NEXTVAL INTO v_newVal FROM DUAL;
-- If this is the first time this table have been inserted into (sequence == 1)
IF v_newVal = 1 THEN
--get the max indentity value from the table
SELECT NVL(max(CHANNEL_ID),0) INTO v_newVal FROM channel;
v_newVal := v_newVal + 1;
--set the sequence to that value
LOOP
EXIT WHEN v_incval>=v_newVal;
SELECT channel_CHANNEL_ID_SEQ.nextval INTO v_incval FROM dual;
END LOOP;
END IF;
--used to emulate LAST_INSERT_ID()
--mysql_utilities.identity := v_newVal;
-- assign the value from the sequence to emulate the identity column
:new.CHANNEL_ID := v_newVal;
END IF;
END;
This fails and severs the connection, giving the following rather cryptic error:
ORA-00603: ORACLE server session terminated by fatal error
ORA-00600: internal error code, arguments: [kqlidchg1], [], [], [], [], [], [], [], [], [], [], []
ORA-00604: error occurred at recursive SQL level 1
ORA-00001: unique constraint (SYS.I_PLSCOPE_SIG_IDENTIFIER$) violated
00603. 00000 - "ORACLE server session terminated by fatal error"
*Cause: An ORACLE server session is in an unrecoverable state.
*Action: Login to ORACLE again so a new server session will be create

Found a solution that worked. Surround the block of code with the following ALTER statements:
ALTER SESSION SET PLSCOPE_SETTINGS = 'IDENTIFIERS:NONE';
... sql statements here ...
ALTER SESSION SET PLSCOPE_SETTINGS = 'IDENTIFIERS:ALL';
For some reason, the Oracle database dump does not include these commands, but this fixed the problem.

Related

Why am I getting this error? PLS-00103: Encountered the symbol "CREATE" when expecting one of the following:

I am getting this error
Compilation failed, line 2 (11:52:47) The line numbers associated with compilation errors are relative to the first BEGIN statement. This only affects the compilation of database triggers.
PLS-00103: Encountered the symbol "CREATE" when expecting one of the
following: ( begin case declare exit for goto if loop mod null pragma
raise return select update while with << continue close current
delete fetch lock insert open rollback savepoint set sql execute
commit forall merge pipe purge json_exists json_value json_query
json_object json_array
Code:
create or replace trigger "GDS_CLIENT_T1"
BEFORE
insert or update or delete on GDS_CLIENT
for each row
begin
create or replace trigger "client insert"
before
insert on "Identify Client"
for each row
begin
select nv1(max(id),0)+1 into :NEW_ID FROM IDENTIFY CLIENT
end;
CREATE OR REPLACE TRIGGER is a DDL command that requires a full specification of the trigger to create; in your case, your first command has not ended:
create or replace trigger "GDS_CLIENT_T1"
BEFORE
insert or update or delete on GDS_CLIENT
for each row
begin
At this point it is expected that you will finish the definition of the GDS_CLIENT_T1 trigger; but instead, you have create or replace trigger which is not valid PL/SQL.

Oracle SQL commands that work when executed separately does not work when executed together. Why?

This is regarding a migration script. The required migration can be done by using 4 separate SQL commands. However, when they are combined to run as a script I am getting PLS-00103: Encountered the symbol error.
These are the SQL commands that I've used to do the migration. The table will have a PK which is a composite key of 3 columns in the table. First I need to remove the constraint, then add a new column called ID to that table, make it auto incremental by adding a sequence and then make it the new PK of the table.
Following commands work and does the exact job when I execute them one by one.
BEGIN
FOR item IN (
SELECT *
FROM all_constraints
WHERE table_name = 'TEST_DB_CHANGE'
)
LOOP
EXECUTE immediate 'ALTER TABLE TEST_DB_CHANGE DROP CONSTRAINT ' || item.CONSTRAINT_NAME;
END LOOP;
END;
/
BEGIN
EXECUTE IMMEDIATE 'CREATE SEQUENCE TEST_DB_CHANGE_SEQUENCE START WITH 1 INCREMENT BY 1 nomaxvalue';
END;
/
BEGIN
EXECUTE IMMEDIATE 'ALTER TABLE TEST_DB_CHANGE ADD (ID NUMBER DEFAULT TEST_DB_CHANGE_SEQUENCE.nextval)';
END;
/
BEGIN
EXECUTE IMMEDIATE 'ALTER TABLE TEST_DB_CHANGE ADD CONSTRAINT TEST_DB_CHANGE_PK PRIMARY KEY (ID)';
END;
But when they are executed together I get the following error.
*Query execution failed
Reason:
SQL Error [6550] [65000]: ORA-06550: line 11, column 1:
PLS-00103: Encountered the symbol "/" *
I tried removing the "/" from that line, from every line, from every line except the last line, but none of that fixed the issue. Then the error will change to:
Reason:
SQL Error [6550] [65000]: ORA-06550: line 12, column 1:
PLS-00103: Encountered the symbol "BEGIN"
What am I doing wrong here?
I don't know how you have combined them but it should be:
BEGIN
FOR item IN (
SELECT *
FROM all_constraints
WHERE table_name = 'TEST_DB_CHANGE'
)
LOOP
EXECUTE immediate 'ALTER TABLE TEST_DB_CHANGE DROP CONSTRAINT ' || item.CONSTRAINT_NAME;
END LOOP;
EXECUTE IMMEDIATE 'CREATE SEQUENCE TEST_DB_CHANGE_SEQUENCE START WITH 1 INCREMENT BY 1 nomaxvalue';
EXECUTE IMMEDIATE 'ALTER TABLE TEST_DB_CHANGE ADD (ID NUMBER DEFAULT TEST_DB_CHANGE_SEQUENCE.nextval)';
EXECUTE IMMEDIATE 'ALTER TABLE TEST_DB_CHANGE ADD CONSTRAINT TEST_DB_CHANGE_PK PRIMARY KEY (ID)';
END;

PLS-00103: Encountered the symbol "CREATE" when expecting one of the following:

Facing error on the code execution :-
ERROR at line 16: ORA-06550: line 16, column 5: PLS-00103: Encountered
the symbol "CREATE" when expecting one of the following: ( begin case
declare exit for goto if loop mod null pragma raise return select
update while with << continue close current
delete fetch lock insert open rollback savepoint set sql execute
commit forall merge pipe purge
Executed sql script with the below code as part of the script and facing error like below
BEGIN
CREATE TABLE orphansInconsistenDelProgress (currentTable VARCHAR(100), deletedCount INT, totalToDelete INT);
INSERT INTO orphansInconsistenDelProgress (currentTable, deletedCount,totalToDelete) values ('',0,0);
ALTER SESSION SET parallel_degree_policy = AUTO;
ALTER SESSION FORCE PARALLEL DML;
COMMIT;

Script Error in Oracle SQL

I have a sql file which has the following statments:
BEGIN
if (&&masterKey = 1) then
shutdown immediate;
startup restrict;
end if;
END;
/
In a different SQL file (defineVariables.sql) I have declared the variable masterKey.
DEFINE masterKey = 0;
and imported that sql here using
#defineVariables.sql
While I execute the script I get the following error. I am not sure if its because I use the shutdown statement? Can someone please help me with this query?
Error Message:
SQL> BEGIN
2 if (&&masterKey = 1) then
3 shutdown immediate;
4 startup restrict;
5 end if;
6 END;
7 /
old 2: if (&&masterKey = 1) then
new 2: if (0 = 1) then
shutdown immediate;
*
ERROR at line 3:
ORA-06550: line 3, column 10:
PLS-00103: Encountered the symbol "IMMEDIATE" when expecting one of the
following:
:= . ( # % ;
ORA-06550: line 5, column 1:
PLS-00103: Encountered the symbol "END" when expecting one of the following:
begin function pragma procedure subtype type <an identifier>
<a double-quoted delimited-identifier> current cursor delete
exists prior
You cannot do this in PLSQL since "shutdown immediate" is not a PLSQL or SQL command but a SQLplus command. One way of achieving a conditional execution of scripts is described in the answer to this question:
SQLplus decode to execute scripts
Basically, depending on the value of your masterkey you select one of two script names and subsequently execute the script with that name.
Based on the code from previous example.
sql> variable flag varchar2(7);
sql> exec :flag := '&&masterKey';
sql> column our_script new_value script noprint;
sql> select decode(:flag, '1',
'c:\sqlplus\shutdown_script.sql',
'c:\sqlplus\do_not_shutdown.sql'
) our_script
from dual;
Execution
sql> #&script;
shutdown_script would be
prompt shutting down
shutdown immediate;
do_not_shutdown script would be
prompt Not shutting down

Can not have CREATE TABLE inside if else

When I run this query
DECLARE
num NUMBER;
BEGIN
SELECT COUNT(*) INTO num FROM user_all_tables WHERE TABLE_NAME=upper('DatabaseScriptLog')
;
IF num < 1 THEN
CREATE TABLE DatabaseScriptLog
(ScriptIdentifier VARCHAR(100) NOT NULL,
ScriptType VARCHAR(50),
StartDate TIMESTAMP,
EndDate TIMESTAMP,
PRIMARY KEY (ScriptIdentifier)
);
END IF;
END;
When execute the above, I got the following:
PLS-00103: Encountered the symbol
"CREATE" when expecting one of the
following:
begin case declare exit for goto if
loop mod null pragma raise return
select update while with << close current delete
fetch lock insert open rollback
savepoint set sql execute commit
forall merge pipe
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
You cannot run DDL statements like that. You need to use dynamic SQL (EXECUTE IMMEDIATE).
IF num < 1 THEN
EXECUTE IMMEDIATE 'CREATE TABLE DatabaseScriptLog (ScriptIdentifier VARCHAR(100) NOT NULL, ScriptType VARCHAR(50), StartDate TIMESTAMP, EndDate TIMESTAMP, PRIMARY KEY (ScriptIdentifier))'
END IF;
You cannot do this like you could in SQLServer. You need to execute the create code through a stored procedure that is already in the proper schema. You pass the create code as a parameter and the stored procedure that has the correct privileges does it for you.
I use a version script that updates the schema to the latest by running schema altering operations separated by if-then clauses to check what version the db is at. After altering it increments the version so that the next if statements test passes and so on. If you are up to date and run the script the ifs skip all altering code. If your db is at version 46 and you run the script which has all changes up to 50, you execute only the blocks that represent versions 47-50.
You could execute immediate but would need elevated privileges which I would not recommend.
Hope this helps.