i have created stored procedure but can't able to execute same - oracle

I defined a stored procedure
create or replace procedure spfirst
(
sp_loc out varchar,
sp_sal out int
)
as
begin
select LOCATION, MONTHLY_SALARY
into sp_loc, sp_sal
from nilesh;
end;
I then call the procedure and get an error
begin
spfirst;
end;
ORA-06550: line 2, column 1:
PLS-00201: identifier 'SPNAME' must be declared
ORA-06550: line 2, column 1:
PL/SQL: Statement ignored

Your procedure takes two parameters. You are calling it without any parameters. Oracle thus looks for a procedure named spfirst that takes no parameters, finds no such procedure, and throws an error.
Something like
DECLARE
l_location nilesh.location%type;
l_salary nilesh.monthly_salary%type;
BEGIN
spfirst( l_location, l_salary );
END;
should work. Of course, you'd generally want to do something with the variables that are returned. If you've enabled dbms_output, you could print them out
DECLARE
l_location nilesh.location%type;
l_salary nilesh.monthly_salary%type;
BEGIN
spfirst( l_location, l_salary );
dbms_output.put_line( 'Location = ' || l_location );
dbms_output.put_line( 'Salary = ' || l_salary );
END;
Be aware that your procedure will throw an error unless the nilesh table has exactly one row. It seems likely that you either want the procedure to take an additional parameter that is the key to the table so that the select into always returns a single row or that you want a function that returns a sys_refcursor rather than a procedure that has multiple out parameters.

Related

ORA-06550: line 1, column 7: PLS-00306: wrong number or types of arguments in call to 'TEST' ORA-06550: line 1, column 7: PL/SQL: Statement ignored

create or replace procedure test(supplierid in number,
supplier out varchar) is
begin
select first_name
into Supplier
from lup_sup_master
where sup_id = supplierid;
end;
/
execute test(2279, :supplierid);
or
begin
execute test(2279, :supplierid); dbms_output.enable; dbms_output.put_line(supplier);
end;
Your procedure test has 2 input arguments. One has type IN which is supposed to be used with the procedure and the other parameter supplier has type OUT which means this parameter is suppose to hold the value which the Procedure returns.
As explained by #Barbaros, if you want to execute the Procedure via a SQL command prompt, you can follow the steps.
However the second way you showed was using a PLSQL Block.
begin
execute test(2279, :supplierid);
dbms_output.enable;
dbms_output.put_line(supplier); end;
In the above example of calling, you must note that Execute keyword is only be used whenever you use a SQL command line prompt. While using a PLSQL block you cal directly call the Procedure by its name as shown below. Also note that incase you have a OUT parameter, you must have OUT parameter passed to the Procedure since the Procedure is expecting 2 arguments. See below demo.
declare
var varchar2(100);
begin
test( supplierid => 2279, supplier =>var);
dbms_output.enable;
dbms_output.put_line(var);
End;
It seems you're confused between supplierid and supplier. You need to define a variable for supplier to get an output line printed, and most probably you defined a command line variable supplierid which's numeric type.
So, use below :
SQL> set serveroutput on;
SQL> var supplier varchar2;
SQL> execute test(2279, :supplier);
supplier
---------
Afzal -- as an example name

How to run Oracle function which returns more than one value

My test function is this
CREATE OR REPLACE FUNCTION MULTI_VAL
(MYNAME OUT EMP2017.ENAME%TYPE)
RETURN NUMBER AS
MYSAL EMP2017.SAL%TYPE;
BEGIN
SELECT SAL, ENAME INTO MYSAL, MYNAME FROM EMP2017 ;
RETURN MYSAL;
END;
/
When I run it like
variable mynm varchar2(20)
SELECT MULTI_VAL(:mynm) FROM dual;
it gives this error
ERROR at line 1:
ORA-06553: PLS-561: character set mismatch on value for parameter 'MYNAME'
The error you get now indicates a datatype mismatch.
However there is a fundamental problem with your code. We cannot use functions which have OUT parameters in SQL. So once you have fixed the datatype issue you will get this error: ORA-06572: Function MULTI_VAL has out arguments.
You can run it like this:
declare
n varchar2(20);
x number;
begin
x := multi_val(n);
end;
/
Generally, functions with OUT parameters are considered bad practice. The syntax allows them, but the usage is hard to understand. It's better to use a procedure with two OUT parameters (because we can only call the program in PL/SQL anyway) or else have the function return a user-defined type.
CREATE TABLE EMP2017(ENAME VARCHAR2(10),SAL NUMBER);
INSERT INTO EMP2017 VALUES ('SMITH',5000);
INSERT INTO EMP2017 VALUES ('JOHNS',1000);
COMMIT;
CREATE TYPE RET_MULT AS OBJECT
(ENAME VARCHAR2(10),SAL NUMBER);
CREATE TYPE T_RET_MULT AS TABLE OF RET_MULT;
CREATE OR REPLACE FUNCTION MULTI_VAL RETURN T_RET_MULT PIPELINED IS
MYSAL RET_MULT;
BEGIN
FOR I IN(SELECT SAL, ENAME FROM EMP2017) LOOP
MYSAL := RET_MULT(I.ENAME,I.SAL);
PIPE ROW(MYSAL);
END LOOP ;
RETURN ;
END;
SELECT * FROM TABLE(MULTI_VAL());
I think this question can be solved without using pipeline functions. Just like this. All pre required data as described #Sedat.Turan except function. Sorry for copy/past.
CREATE TABLE EMP2017(ENAME VARCHAR2(10),SAL NUMBER);
INSERT INTO EMP2017 VALUES ('SMITH',5000);
INSERT INTO EMP2017 VALUES ('JOHNS',1000);
COMMIT;
CREATE TYPE RET_MULT AS OBJECT
(ENAME VARCHAR2(10),SAL NUMBER);
CREATE TYPE T_RET_MULT AS TABLE OF RET_MULT;
create or replace function MULTI_VAL return T_RET_MULT is
RET_SET T_RET_MULT;
begin
select RET_MULT(ENAME, SAL) bulk collect into RET_SET from EMP2017;
return RET_SET;
end;

PLS-00306: wrong number or types of arguments in call

I've got the following procedure to insert a user into a database. I use 4 in-parameters and 1 out-parameter. The out-parameter is filled with a sequence.
The table structure is a user_id and the names of the 4 first parameters.
CREATE OR REPLACE PROCEDURE insert_user
(
P_USERNAME IN varchar2,
P_PASSWORD IN varchar2,
P_COMPANY_EMAIL IN varchar2,
P_EMPLOYEE_ID IN number,
P_USER_ID OUT int)
AS
BEGIN
INSERT INTO system_users.users(USERNAME, PASSWORD, COMPANY_EMAIL, EMP_ID)
VALUES
(P_USERNAME, P_PASSWORD, P_COMPANY_EMAIL, P_EMPLOYEE_ID);
dbms_output.put_line (P_USER_ID);
END;
I get the error that I have the wrong number or types of arguments when I call the procedure. I called it like so:
insert_user('user', 'password', 'mail#mail.com', '5');
I have tried calling it with begin and end around it, I have tried the number without quotes, all wiithout quotes.
What am I doing wrong? Can it have something to do with the trigger not filling the user_id?
Regards,
Vincent
EDIT: JIC it is the trigger, here is the code for the trigger. It worked, but I saw a snag in the code so I edited it. FSR it says that the table I am trying to find (system_users.users) does not exist (invalid identfier)
PLEASE PLEASE help me!
create or replace trigger user_trig_bi
Before insert on system_users.users
for each row
begin
select user_seq.nextval
into new.user_ID
From dual
where exists (SELECT emp_id
from hrm_repo.employees
where hrm_repo.employees.emp_id = system_users.users.emp_id);
END;
The error I get is:
Error(7,55): PL/SQL: ORA-00904: "SYSTEM_USERS"."USERS"."EMP_ID": invalid identifier
Since your procedure has an out variable, provide it with a fifth variable to get the out value:
declare
p_user_id int;
begin
insert_user('user', 'password', 'mail#mail.com', '5', p_user_id);
end;
Note: You don't set the value in your procedure, so you could either let it away at all or you have to set it in your procedure.

SQLPLUS object system. is invalid

I'm stuck with some simple procedure and I can't figure out why.
This is my code, which I'm running in sqlplus:
CREATE OR REPLACE PROCEDURE NormalizeName(fullname IN NVARCHAR2)
IS
BEGIN
SELECT TRIM(fullname) INTO fullname FROM DUAL;
DBMS_OUTPUT.PUT_LINE(fullname);
END NormalizeName;
/
BEGIN
NormalizeName('Alice Wonderland ');
END;
/
When I run it, I get the error:
Warning: Procedure created with compilation errors.
NormalizeName('Alice Wonderland ');
*
ERROR at line 2:
ORA-06550: line 2, column 2:
PLS-00905: object SYSTEM.NORMALIZENAME is invalid
ORA-06550: line 2, column 2:
PL/SQL: Statement ignored
What's wrong?
1) Never create objects in the SYS or SYSTEM schema. Those are reserved for Oracle. If you want to create objects, create a new schema first.
2) When you see that a procedure has been created with compilation errors in SQL*Plus, type show errors to see the errors.
3) The error appears to be that your SELECT statement is trying to write to the fullname parameter. But that parameter is defined as an IN parameter, not IN OUT, so it is read-only. If you define the parameter as IN OUT, though, you could not pass a string constant to the procedure, you'd need to define a local variable in your calling block. It doesn't make a lot of sense to have a procedure that doesn't do anything other than call dbms_output since there is no guarantee that anyone will see the data written to that buffer. My guess is that you really want a function that returns a normalized name. Something like
CREATE OR REPLACE FUNCTION NormalizeName( p_full_name IN VARCHAR2 )
RETURN VARCHAR2
IS
BEGIN
RETURN TRIM( p_full_name );
END;
which you can then call
DECLARE
l_normalized_name VARCHAR2(100);
BEGIN
l_normalized_name := NormalizeName( 'Alice Wonderland ' );
dbms_output.put_line( l_normalized_name );
END;
If you really need a procedure because this is a homework assignment
CREATE OR REPLACE PROCEDURE NormalizeName( p_fullname IN VARCHAR2 )
AS
BEGIN
dbms_output.put_line( TRIM( p_fullname ));
END;
In the real world, you should only be using procedures when you want to manipulate the state of the database (i.e. you're doing INSERT, UPDATE, DELETE, MERGE, etc.). You use functions when you want to perform calculations without changing the state of the database or when you want to manipulate data passed in parameters.

How to create/call procedure in oracle 10g?

I created a procedure in Oracle but am getting an error when I attempt to execute it. Below are listed the steps I am taking to create this procedure:
SQL> ed getuserinfo
create or replace procedure getUserInfo
( p_username out Users.username%TYPE,
p_password out Users.password%TYPE ) IS
BEGIN
select username,password into p_username,p_password from users where username='yogi';
END;
/
SQL> exec getuserinfo
BEGIN getuserinfo; END;
*
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00201: identifier 'GETUSERINFO' must be declared
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
What is the problem and how can I solve it? Olease can anyone help me?
You need to actually create the procedure, which you haven't done. You need a semi-colon after end and if you're creating it in SQL*Plus you need to add / to inform SQL*Plus that the block is finished:
create or replace procedure getUserInfo
( p_username out Users.username%TYPE,
p_password out Users.password%TYPE ) IS
BEGIN
select username,password into p_username,p_password from users;
END;
/
show error
It's always wise to add show error afterwards as well so that any errors are returned to the console in an understandable format.
Did you actually execute your create procedure statement? Did you get a "Procedure created." message? The fact that Oracle does not know of your getuserinfo procedure indicates to me that this statement was not performed.
I think it's procedure calling mistake!! Calling Should be like below:
SQL>var var1 varchar2(50);
SQL>var var2 varchar2(50);
SQL> exec getuserinfo(:var1, :var2);
SQL> print var1, var2;
Have Fun!!
You need to specify the out parameters when you call the procedure.
DECLARE
x Users.username%TYPE;
y Users.password%TYPE;
BEGIN
getuserinfo(x, y);
DBMS_OUTPUT.PUT_LINE('username: ' || x || ', password: ' || y);
END;

Resources