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

I am trying to create a stored procedure in oracle 12c database and I am getting error when I am running code to store the procedure.
PLS-00103: Encountered the symbol "CREATE" when expecting one of the
There are multiple stack overflow question already asked on this topic. but they suggest some different syntax. which is deviation from actual oracle documentation. and even those didn't worked for me
I checked for documentation on multiple website including oracle documentation. oracle documetation suggest syntax as following
Oracle STORED PROCEDURE DOCUMENTATION
so I as per the syntax I wrote the following procedure.
CREATE PROCEDURE PDD_PROC_BASE
AS
--DROP TABLE BASE;
CREATE TABLE BASE as
SELECT idno
,DATE
,diff
,SUBSTR(idNO,7,2) AS PRD
,COMPLETED
,CATG
,OP_Number
FROM table1
WHERE = date >= '30-JUN-2018'
AND STATUS = 'G'
END;
and I got the following error.
Procedure PDD_PROC_BASE compiled
Errors: check compiler log
Errors for PROCEDURE AN_5043152.PDD_PROC_BASE:
LINE/COL ERROR
-------- ------------------------------------------------------------------------------
7/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 <an identifier>
<a double-quoted delimited-identifier> <a bind variable> <<
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
I checked other resources as well but still didn't understood what went wrong.
I even tried code example from Oracle documentation and got similar error.
I am using SQLdeveloper tool as client

You cant use directly sql ddl statements in plsql block, you can do the same thing using dynamic sql with the "EXECUTE IMMEDIATE" statement like this:
begin
execute immediate 'create table test_table1 (test_column1 varchar2(40))';--your create table statement here
end;

In Oracle in stored procedures you can use DDL statements only as dynamic SQL, that means as EXECUTE IMMEDIATE 'CREATE TABLE ' ...
Check here, for example: http://www.dba-oracle.com/t_using_ddl_create_index_table_plsql.htm

Related

Mysql Create Insert Procedure Statement incomplete

I'm trying to wirte a little log procedure for my database. I create a procedure with this statment:
create procedure prc_wirte_log (
in p_schema varchar(255),
in p_item varchar(255),
in p_message varchar(255)
)
begin
insert into weather.log (`schema`, item, message) values (p_schema, p_item, p_message);
end;
I get the error Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 7 0.063 sec
Why? The MySQL Workbench means Incomplet Statment: excepting ; after the insert query.
What could I do?
Multistatement procedures (assumed when BEGIN...END is present) require delimiter overrides to prevent the statements they contain from terminating the procedure definition prematurely.
Typically, you need to do something like:
DELIMITER //
CREATE PROCEDURE blah()
BEGIN
statements;
END//
DELIMITER ;
The first example on the documentation here demonstrates this (though the last two on that page seem to repeat your mistake.
If you are using WorkBench or similar tool just right click on StoredProcedures and click Create stored procedure the tool will create default structure like below and you could write your logic and hit on apply. Ensure to use semicolon at the end of the last statement (just before END).
CREATE PROCEDURE `new_procedure` ()
BEGIN
select * from tasks;
END

Error: ORA-00969: missing ON keyword - Oracle

I am using Oracle 11g XE database and Oracle SQL developer to execute SQL statements.
I have this SQL statement which is giving me the above compiler error when executing it.
CREATE OR REPLACE
TRIGGER "STD"."TRG_STUDENT"
BEFORE INSERT,DELETE
ON STUDENT
FOR EACH ROW
BEGIN
IF INSERTING THEN
DBMS_OUTPUT.PUT_LINE('Inserting !!');
END IF;
IF DELETING THEN
DBMS_OUTPUT.PUT_LINE('Deleting !!');
END IF;
END;
I tried some variations but I used to get other errors.
I placed the ON STUDENT just before the BEFORE INSERT,DELETE line and I get this error:
Error: ORA-04071: missing BEFORE, AFTER or INSTEAD OF keyword
What am I missing here?
BEFORE INSERT OR DELETE
More about Create Trigger syntax: http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_7004.htm
Use BEFORE INSERT OR DELETE instead of BEFORE INSERT, DELETE. Refer coding trigger for more in detail.

How to execute stored procedures containing dynamic SQL in oracle?

I've created the following procedure
Create or replace procedure abcd
(
tab_name in USER_TABLES.table_name%type
)
is
begin
execute immediate
'select * from'||tab_name;
end abcd;
The procedure gets compiled.
I am trying to get the output using the following
select abcd('Table') from dual ;
I am new to dynamic SQL and this does not seem to work for me. I keep getting the error
[Error] Execution (44: 8): ORA-00904: "ABCD": invalid identifier
Can someone please help ?
Regards,
Kshitij
You're missing a space before your table name:
create or replace procedure abcd (tab_name in USER_TABLES.table_name%type )
is
begin
execute immediate 'select * from '||tab_name;
end abcd;
This won't work because you're trying to call it as a function, not a procedure:
select abcd('Table') from dual ;
Your second attempt should now work:
exec abcd('Table');
... but will now get a different error. In PL/SQL you have to select into something. In this case you probably want to open a cursor with the dynamic string and do something with the results. Not really sure what your end goal is though.
You should also read up about SQL injection while you learn about dynamic SQL.
you cannot perform a select on a procedure, a function will work only if single record return.
use
begin
abcd();
end;
or use
execute keyword
ALSO use a space after from in query
It will not work.
When you invoke EXECUTE IMMEDIATE the sql statement is send to SQL engine. No results are passed back to the PL/SQL.
Writing "SELECT * FROM a_table" is not that hard and much safer.

PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following: ;

I am running the following script -
BEGIN
select department_name
from egpl_department
where department_id in (select department_id
from egpl_casemgmt_activity);
END ;
And got the Error -
PLS-00103: Encountered the symbol "end-of-file" when
expecting one of the following:
;
In a PL/SQL block select statement should have an into clause:
DECLARE
v_department egpl_department.department_name%type;
BEGIN
select department_name
into v_department
from egpl_department
where department_id in (select department_id from egpl_casemgmt_activity);
-- Do something useful with v_department
END;
PLS-00103 always means the compiler has hurled because we have made a syntax error. It would be really neat if the message text said: You have made a syntax error, please check your code but alas it doesn't.
Anyway, in this case the error is that in PL/SQL select statements must populate a variable. This is different from the behaviour of say T-SQL. So you need to define a variable which matches the projection of your query and select INTO that variable.
Oracle's documentation is comprehensive and online. You can find the section on integrating SQL queries into PL/SQL here. I urge you to read it, to forestall your next question. Because once you have fixed the simple syntax bloomer you're going to hit TOO_MANY_ROWS (assuming you have more than one department).
In PL/SQL you cannot just select some data. Where is the result supposed to go?
Your options are:
Remove BEGIN and END and run the SELECT with SQL*plus or some other tool that can run a SQL statement and present the result somewhere.
Use SELECT department_name INTO dep_name to put the result into a PL/SQL variable (only works if your SELECT returns a single row)
Use SELECT department_name BULK COLLECT INTO dep_name_table to put the result into a PL/SQL table (works for several rows)
Or maybe you can describe what you're trying to achieve and in what environment you want to run the SQL or PL/SQL code.
To avoid the too_many_rows problem, you could use a cursor, something like this (I haven't tested this, but along these lines )
DECLARE
v_department egpl_department.department_name%type;
cursor c_dept IS
select department_name
into v_department
from egpl_department
where department_id in (select department_id from egpl_casemgmt_activity)
order by department_name;
BEGIN
OPEN c_dept;
FETCH c_dept INTO v_department;
CLOSE c_dept;
-- do something with v_department
END;
This will put the first value it finds in the table into v_department. Use the ORDER BY clause to make sure the row returned would be the one you required, assuming there was the possibility of 2 different values.
Most people would not consider the call to be the issue,
but here's an amusing bug in Oracle Sql Developer that may emulate the issue..
exec dbowner.sp1 ( p1, p2, p3); -- notes about the fields
Error report -
ORA-06550: line 1, column 362:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
begin case declare end exception 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.
*Action:
exec dbowner.sp1 ( p1, p2, p3);
-- notes about the fields
PL/SQL procedure successfully completed.
DECLARE is only used in anonymous PL/SQL blocks and nested PL/SQL blocks.
You do not need to use the DECLARE key word before you 'introduce' a new variable in a Procedure block, unless .... the procedure is a nested PL/SQL block.
This is an example of how you would declare a variable without the 'DECLARE' Key word below.
eg.;
CREATE OR REPLACE PROCEDURE EXAMPLE( A IN NUMBER, B OUT VARCHAR2 )
IS
num1 number;
BEGIN
num1:=1;
insert into a (year) values(7);
END;
This question/answer explains it better
create procedure in oracle

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.