How to get result/data from procedure from SQL command line - sql

looking for what causing to error message below:
[Error] Execution (4: 5): ORA-06550: line 4, column 5: PLS-00306:
wrong number or types of arguments in call to 'ALLOC_DAY_GRAPH_KTL'
ORA-06550: line 4, column 39: PLS-00363: expression '01-Oct-2012'
cannot be used as an assignment target ORA-06550: line 4, column 5:
PLS-00306: wrong number or types of arguments in call to
'ALLOC_DAY_GRAPH_KTL' ORA-06550: line 4, column 5: PL/SQL: Statement
ignored
Thanks in advance

If this is a package existing in a db, you could write a simple pl sql procedure block which calls the required procedure in the package and save it a s a sql script. It can then be called from the sqlplus commandline using #filename.sql

This could be a simple way:
setup:
SQL> CREATE OR REPLACE PACKAGE reports AS
2 TYPE rep_type IS REF CURSOR;
3 END reports;
4 /
Package created.
SQL>
SQL> create or replace procedure testCursor(pIn in number, pCursOut in OUT reports.rep_type) is
2 begin
3 open pCursOut for
4 select 1 as one,
5 'something' as text,
6 pIn as parameter
7 from dual;
8 end;
9 /
Procedure created.
call:
SQL> variable vCur refCursor
SQL> exec testCursor(999, :vCur);
PL/SQL procedure successfully completed.
SQL> print :vCur
ONE TEXT PARAMETER
---------- --------- ----------
1 something 999
SQL>
If you are not in SQLPlus, you may need a plSQL block:
declare
vCur reports.rep_type;
begin
testCursor(999, vCur);
/* code to use the cursor */
end;
/
In this case you need to write some code to do whatever you need with the cursor returned by the procedure.

Related

How to get procedure to run 'PLS-00201: identifier "DISP_GUIDE_NAM' must be declared" oracle

I'm a beginner to sql and I'm having troubling calling a stored procedure. I have put down the procedure, call, and error. I have been researching for hours trying to figure out why I'm getting this error. I am a database administrator. Oracle Database 11g Express Edition.
Thanks for any help.
Procedure
CREATE OR REPLACE PROCEDURE DISP_GUIDE_NAM (I_GUIDE_NUM IN GUIDE.GUIDE_NUM%TYPE) AS
I_LAST_NAME GUIDE.LAST_NAME%TYPE;
I_FIRST_NAME GUIDE.FIRST_NAME%TYPE;
SET SERVEROUTPUT ON;
BEGIN
SELECT LAST_NAME, FIRST_NAME
INTO I_LAST_NAME, I_FIRST_NAME
FROM GUIDE
WHERE GUIDE_NUM=I_GUIDE_NUM;
DBMS_OUTPUT.PUT_LINE(RTRIM(I_FIRST_NAME)||' '||RTRIM(I_LAST_NAME));
END;
/​
Call
begin
disp_guide_nam('AM01');
end;
/
Error:
ORA-06550: line 2, column 1:
PLS-00201: identifier 'DISP_GUIDE_NAM' must be declared
ORA-06550: line 2, column 1:
PL/SQL: Statement ignored
1. begin
2. disp_guide_nam('AM01');
3. end;
4. /
it should give me guide number, first name last name.

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

Cursor with a where clause

I am trying to use a cursor with a where clause. It shouldnt be much different from the simple cursor I assume. I tried the following in my case but doesnt seem to print any output.
Declare
BG_TOTAL number;
SDO_GEOM varchar2 ;
cursor c1 is
select
SDO_GEOM.SDO_AREA(GEOMETRY, 0.005,'unit = SQ_MILE')
from <tablename>
where SITE_ID= 8;
Type SITE_TAB_TYPE is table of c1%ROWTYPE;
SITE_LIST SITE_TAB_TYPE;
Begin
open c1;
FETCH c1 BULK COLLECT INTO SITE_LIST;
close c1;
DBMS_OUTPUT.PUT_LINE(SITE_LIST(1).SDO_GEOM);
Exception
When others then
DBMS_OUTPUT.PUT_LINE(sqlerrm);
End;
[Error] Execution (3: 11): ORA-06550: line 3, column 11:
PLS-00215: String length constraints must be in range (1 .. 32767)
ORA-06550: line 7, column 5:
PLS-00487: Invalid reference to variable 'SDO_GEOM'
ORA-06550: line 7, column 5:
PLS-00487: Invalid reference to variable 'SDO_GEOM'
ORA-06550: line 22, column 40:
PLS-00302: component 'SDO_GEOM' must be declared
ORA-06550: line 22, column 6:
PL/SQL: Statement ignored
The problem is that you are trying to output the whole table object to standard output, whereas you should iterate through it. Instead of:
DBMS_OUTPUT.PUT_LINE(SITE_LIST.SDO_GEOM);
Use something like this:
FOR v_i IN 1..SITE_LIST.LAST
LOOP
dbms_output.put_line(SITE_LIST(v_i).SOME_COLUMN_NAME);
END LOOP;
Replace the SOME_COLUMN_NAME with a name of a column selected in your cursor.
Edit
You have to provide an index of the element of your collection to which you want to refer to:
dbms_output.put_line(site_list(1).SDO_GEOM);
Edit #2
Yes.. you didn't specify column alias for value selected in your query. Try this cursor instead:
cursor c1 is
select
SDO_GEOM.SDO_AREA(GEOMETRY, 0.005,'unit = SQ_MILE') AS my_value
from <tablename>
where SITE_ID= 8;
And then:
dbms_output.put_line(site_list(1).my_value);

Call oracle procedure with an empty cursor?

I have a procedure that looks like this:
PROCEDURE myprocedure (
i_area IN somenumber%TYPE,
io_areadata IN OUT areacursor
);
And I wonder if there is any simple way to call procedure? I have data for the first argument but for the second I do not know what enter. Is there any way I could enter an empty cursor or something similar?
This is what I've done so far:
BEGIN
package.myprocedure (
12345
);
END;
And that gives me the following error:
Error report:
ORA-06550: row 2, column 1:
PLS-00306: wrong number or types of arguments in call to 'MYPROCEDURE'
ORA-06550: row 2, column 1:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
Thank you in advance and sorry for my (maybe) poor explanation. I have little experience in PL/SQL.
You have to supply that parameter. You could do this:
DECLARE
dummy package.areacursor;
BEGIN
package.myprocedure (
12345, dummy
);
END;
By this way you can call a stored procedure by passing particular value to a particular parameter.
Try this.
Begin
package.myprocedure(i_area => '12345);
end;

OracleEE 11g WITH clause causing error procedure won't compile

This is my first run through with PL SQL so I might be making some sort of silly error.
I am trying to write a procedure in Oracle Express Edition 11g. I am running into an error that has to do with my WITH clause in the procedure body.
Whenever I try and run it I see two errors.
Error report:
ORA-06550: line 14, column 50:
PL/SQL: ORA-00918: column ambiguously defined
ORA-06550: line 12, column 7:
PL/SQL: SQL Statement ignored
ORA-06550: line 29, column 3:
PLS-00306: wrong number or types of arguments in call to 'FIND_HIGH_AVG'
ORA-06550: line 29, column 3:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Code for the procedure is found below.
DECLARE
myTerm courses.term%type;
myLine courses.lineno%type;
procedure find_high_avg (term IN courses.term%type,
line IN courses.lineno%type,
s_fname OUT students.fname%type,
s_lname OUT students.lname%type,
s_sid OUT students.side%type,
s_avg OUT number) is
begin
WITH grades as (select * from components co --line 12 here
join scores sc on co.term = sc.term and co.lineno = sc.lineno and CO.COMPNAME = SC.COMPNAME
where sc.lineno = line and sc.term = term) --line 14 here
select *
into s_fname, s_lname, s_sid, s_avg
from (
select s.fname, s.lname, s.sid, round(sum(points/maxpoints * weight),0) as AV
from grades, students
join students s on grades.sid = s.sid
group by s.sid, s.fname, s.lname
order by AV)
where rownum = 1;
end;
BEGIN
myTerm:='F12';
myLine:='1031';
find_high_avg(myTerm, myLine); --line 29 here
END;
The error at line 10 occurs because parameters to stored procedures do not take a length. The procedure declaration should be something like
procedure find_high_avg (term IN courses.term%type,
line IN courses.lineno%type,
s_fname OUT students.fname%type,
s_lname OUT students.lname%type,
s_sid OUT students.side%type,
average OUT number)
is
The error at line 14 is likely because the parameters to your procedure have the same name as columns in your table. In a SQL statement's scope resolution rules, column names take precedence over local PL/SQL variables. So when you code something like
sc.term = term
Oracle tries to resolve the unqualified TERM using a column in one of the tables. If both tables have a column named TERM, that generates an ambiguous column reference-- Oracle doesn't know which of the two tables to use. Of course, in reality, you don't want it to use the column from either table, you want it to use the parameter. The most common approach to this problem is to add a prefix to your parameter names to ensure that they do not collide with the column names. Something like
procedure find_high_avg (p_term IN courses.term%type,
p_line IN courses.lineno%type,
s_fname OUT students.fname%type,
s_lname OUT students.lname%type,
s_sid OUT students.side%type,
p_average OUT number)
is
The error on line 29 occurs because the procedure takes 6 parameters-- 2 IN and 4 OUT. In order to call it, therefore, you would need to use 6 parameters. Something like
DECLARE
myTerm courses.term%type;
myLine courses.lineno%type;
l_fname students.fname%type;
l_lname students.lname%type;
l_sid students.side%type;
l_avg number;
BEGIN
myTerm:='F12';
myLine:='1031';
find_high_avg(myTerm, myLine,l_fname,l_lname, l_sid, l_avg);
END;
I think it should be end find_high_avg; instead of end; after the select statement.