PL/SQL: Having trouble creating procedure - sql

I'm having issues creating a simple procedure in oracle 10g
All I'm trying to do is this procedure:
create procedure greetings
is
begin
dbms_output.put_line('Hello');
end;
I type this into the prompt:
#proc1.sql
and I get only this:
6
and I'm unable to get back to the prompt again without ctrl+c. I know it's ghetto but I'm using SSH if that helps solve the problem.

You need a / on a line by itself after the final end; so sqlplus will know it's time to execute. (sqlplus somehow fails to know how to parse the language which is its sole purpose for existing, so it doesn't know that the last end; you entered is the one that terminates the create command.)

Related

oracle sql developer first time user

I am new to plsql and trying to use oracle sql developer, I try to run a simple procedure with dbms output line and i get the following error,
ora-00904
, the code is
create or replace PROCEDURE proc_101 IS
v_string_tx VARCHAR2(256) := 'Hello World';
BEGIN
dbms_output.put_line(v_string_tx);
END;
whether i click the run(green colour) or debug(red colour) i get the same error.
You can see from the above code, procedure doesn't access any objects but still i get the same error.
Your procedure is fine. You may not have permissions to be able to Create a Procedure. If this is the case test your procedure/code without actually Creating it in the Database first. For example, when I'm testing code in my Production database my oracle user cannot Create Procedures, Packages, Tables etc... And so I test my Procedures within my Own PL/SQL Blocks. When the code is good to go I can get a database administrator to Create the Procedures and/or Packages for me.
The below screenshot is code that simply tests the Procedure:
The below screenshot is code that does much more and tests the Procedure from within a PL/SQL Block
For more advanced situations this allows you to do so much more as you can create all sorts of Procedures/Functions and/or Cursors and test them immediately without needing to CREATE these objects in your Oracle Database.
I'd say that there's some other code in the worksheet which raises that error, not just the CREATE PROCEDURE you posted. For example, something like this SQL*Plus example (just to show what's going on - you'd get the same result in SQL Developer):
SQL> select pixie from dual;
select pixie from dual
*
ERROR at line 1:
ORA-00904: "PIXIE": invalid identifier
SQL>
SQL> create or replace PROCEDURE proc_101 IS
2 v_string_tx VARCHAR2(256) := 'Hello World';
3 BEGIN
4 dbms_output.put_line(v_string_tx);
5 END;
6 /
Procedure created.
SQL>
See? The first part raised ORA-00904 as there's no PIXIE column in DUAL, while the procedure is created correctly.
So - remove code which fails and everything should be OK.
Check with your DBA to make sure the dbms_output package has been installed on your database, and that you have permissions on it.

oracle - run stored procedure from script

I've a script that I am using to build/drop tables and basically setting up the entire schema.
After googling, I still can't figure out how to run a stored procedure.
The script is a .txt file, and I run it using Apex SQL Oracle.
If I write only this line in a script:
execute procedurename(1); --where 1 is paramter.
You have requested to run a script that does not contain any runnable
statements.
SQL>create or replace procedure procedurename(p_num number)
as
begin
null;
end;
/
Procedure created.
SQL>execute procedurename(1);
PL/SQL procedure successfully completed.
everything seems ok on SQLPLUS with oracle 11.
so it must be an apex thing.
Since execute is a sqlplus statement ,try calling the procedure using begin-end PLSQL block in Apex SQL
BEGIN
procedurename(1);
END;
/
save this in a file proc_call.sql and then call it in your script like
#C:\proc_call.sql
where C: is the sample path
For some information refer the below link
https://forums.oracle.com/forums/thread.jspa?threadID=618393

How to run sql scripts from a pl sql procedure

I have a procedure like :
CREATE OR REPLACE PROCEDURE test is
BEGIN
DBMS_OUTPUT.PUT_LINE('This is a Test');
END;
I want to run some sql scripts stored in the current directory.
I could run them from sqlplus with '#scriptname.sql' but how can i do it from inside the procedure ? For ex:
CREATE OR REPLACE PROCEDURE test is
BEGIN
DBMS_OUTPUT.PUT_LINE('This is a Test');
#scriptname.sql
END;
This doesn't seem to work ! Is there a specific to run sql scripts from pl/sql procedures ?
You can't, in general, because the pl/sql is run in the database, on the server, and sqlplus is a client process. The server can't rely on even being on the same system as the client and its files, much less knowing anything about how to find the file the client is referring to. Even if the syntax were supported (and it isn't), your sql script would have to be on the server, in a location the server knew about and had access to.
Actually, you can do this in SQL*Plus - you just need to ensure the # is the first character on the line, e.g.:
CREATE OR REPLACE PROCEDURE test is
BEGIN
DBMS_OUTPUT.PUT_LINE('This is a Test');
#scriptname.sql
END;
SQL*Plus will read the entire contents of the script and insert it at that point in the procedure, then create the procedure as it is given. That means you can't have SQL*Plus commands in scriptname.sql. Also, there won't be any reference to #scriptname.sql in the actual procedure created on the database.
You could execute an OS command to start SQLPlus and have that execute the scripts. You can pass a filename into SQLplus at start up and it will execute it.
Google External Procedures and extproc or this article. Or something like call OS command with Java
You could write a Java Stored Procedure to open the file and return its contents as a String and then call Execute Immediate on the String.
Be VERY CAREFUL doing this though as any malicious sql in those files can do pretty much whatever it wants.
Even if there should be a solution, I would not recommend to to this. A PL/SQL procedure basically is a SQL script. Either
1. run your SQL scripts from outside the database, e.g. via shell script or
2. move the SQL code inside your procedure.

ORA:00900 Invalid sql statement

i created a procedure with 32 in argument,it sucessfully created.but when i am executing this in back end oracle the errror came ORA:00900 Invalid sql statement
Use:
SQL> alter procedure [your procedure name here] compile;
SQL> show errors
...to be able to diagnose the issue from the resulting error output.
Also look at view USER_ERRORS.
Sometimes, show errors does not show anything when in fact there are errors. Especially after alter compile.
Finally, re-compile in TOAD or SQL Developer and you can easily navigate to the error.
In Oracle SQL Developer you should execute it this way:
BEGIN
YOUR_PROCEDURE(PARAM1, PARAM2);
END;
If you use EXECUTE or EXEC (which work in SqlPlus) you get the ORA-00900 error.

Calling a Stored Procedure in Oracle

This should be something fairly simple and straight-forward but for some reason I can't get it to work. I've created my SProc like so:
create or replace procedure zadmit_migrate_data (zadmit_seq_no number)
is
thisPIDM number;
begin
select pidm into thisPIDM
from saturn.zadmit_core_data
where pk_seqno = zadmit_seq_no;
if thisPIDM is not null then
dbms_output.put_line('thisPIDM is NOT null!');
else
dbms_output.put_line('thisPIDM is null!');
end if;
end zadmit_migrate_data;
And now I've trying to call it like this:
call zadmit_migrate_data(4);
And then I get this error:
ORA-06575 Package or function is in an invalid state.
So I tried this:
execute zadmit_core_data(4);
And instead get this error:
ORA-00900 Invalid SQL statement.
It might be less time consuming to point out where I'm going right, but if anyone can tell me where I'm going wrong that would probably be more useful. :)
If you Google for error ORA-06575, you find:
You tried to execute an SQL statement
that referenced a PLSQL function that
is in an invalid state. This happens
when the function is compiled with
errors.
You can resolve this by:
Correct the errors and then re-compile
the function. Then re-execute the SQL
statement.
Once your procedure compiles, you could execute it like:
begin
zadmit_migrate_data(4);
end;
Shouldn't your execute statement be "execute zadmit_migrate_data(4);" ?
At any rate, running this command:
SELECT object_name FROM user_objects WHERE status='INVALID';
will tell you if you procedure is valid or not.
Executing your CREATE or REPLACE ... command, then immediately executing the command
SHOW ERRORS
should tell you what compilation errors were encountered.
Run this
SQL> alter procedure zadmit_migrate_data compile;
SQL> show errors
Given the simplicity of your procedure, it shouldn't be hard to diagnose the resulting error stack.