Script Error in Oracle SQL - 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

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.

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;

Error occurs with anonymous block pl/sql for multiple Update statement

I am having problems in following pl/sql anonymous. Here is the scaled down version of my pl/sql block..What is wrong with my code?
WHENEVER SQLERROR EXIT sql.sqlcode ROLLBACK
WHENEVER oserror EXIT FAILURE ROLLBACK
SET SERVEROUTPUT ON
SET ECHO OFF
SET DEFINE OFF
SET LINESIZE 120
SET AUTOCOMMIT OFF
BEGIN
update MYTABLE set GID = '12345' where MYTABLE.COLUMN1=456456 and MYTABLE.PARTY<>0 and MYTABLE.EXPIRY = to_date('17/05/2013','DD/MM/YYYY')
AND EXISTS (SELECT PARTIES.LABEL
FROM PARTIES
WHERE PARTIES.m_id = MYTABLE.PARTY_ID and PARTIES.LABEL = 'PARTY_NAME');
dbms_output.put_line( 'Rows Updated : ' || to_char(sql%rowcount));
END;
/
Here is the output I get
update MYTABLE set GID = '12345' where MYTABLE.COLUMN1=456456 and MYTABLE.PARTY0 and MYTABLE.EXPIRY = to_date('17/05/2013','DD/MM/YYYY')
*
ERROR at line 2:
ORA-06550: line 2, column 8:
PL/SQL: ORA-06552: PL/SQL: Compilation unit analysis terminated
ORA-06553: PLS-488: invalid variable declaration: object 'TIMESTAMP' must be a type or subtype
ORA-06550: line 2, column 1:
PL/SQL: SQL Statement ignored
Based on the error you have reported I am guessing that somewhere in your PL/SQL block (which is not present in your question) you are referring to a table column named TIMESTAMP. Unfortunately this as a reserved word in Oracle as it is a column data type. I would recommend that you rename the column to something like DATE_CREATED and see if your code compiles.

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

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.

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.