I creates a procedure using Toad Client that is
create or replace procedure getuid(eid_pro varchar2)is
l_value number;
begin
select uniqueid
into l_value
from enrollment
where eid=eid_pro;
end ;
When I execute it by Followings
begin
getuid('245698154');
end;
It executed successfully but result is not displayed in data grid.
Please help me in this
Turn the procedure into a function.
create or replace function getuid(eid_pro varchar2) return number is
l_value number;
begin
select uniqueid
into l_value
from enrollment
where eid = eid_pro;
return l_value;
end;
And then select the function.
select getuid('245698154') from dual;
In that case please use a function and then you can use it in a select statement which can show the result in the grid.
create or replace function getuid(eid_pro varchar2) return number is
l_value number;
begin
select uniqueid
into l_value
from enrollment
where eid=eid_pro;
return l_value;
end ;
select getuid('245698154') from dual;
Related
How to create a function to display all employees of one department?
I'd tried this code but it returns only "cursor" value.
CREATE OR REPLACE FUNCTION emp_dept (dept_id IN NUMBER)
RETURN SYS_REFCURSOR
IS
emp_name SYS_REFCURSOR;
BEGIN
OPEN emp_name
FOR SELECT last_name
FROM employees
WHERE department_id = dept_id;
RETURN emp_name;
END emp_dept;
You may use these options to read and display output from the cursor that's returned from your function.
Use a simple select
select emp_dept(10) from dual;
Result
EMP_DEPT(10)
--------------------
CURSOR STATEMENT : 1
CURSOR STATEMENT : 1
LAST_NAME
-------------------------
Whalen
Use DBMS_SQL.RETURN_RESULT ( Oracle 12c and above)
DECLARE
l_cur SYS_REFCURSOR := emp_dept(10) ;
BEGIN
DBMS_SQL.RETURN_RESULt(l_cur);
END;
/
Result
PL/SQL procedure successfully completed.
ResultSet #1
LAST_NAME
-------------------------
Whalen
FETCH from the cursor into a local collection. A slight variation could be also be used to fetch into a scalar variable.
DECLARE
l_cur SYS_REFCURSOR := emp_dept(10) ;
TYPE l_last_name_tab IS TABLE OF employees.last_name%TYPE;
l_lnt l_last_name_tab;
BEGIN
FETCH l_cur BULK COLLECT INTO l_lnt;
for i in 1..l_lnt.count
loop
dbms_output.put_line(l_lnt(i));
end loop;
END;
/
Result
Whalen
PL/SQL procedure successfully completed.
I have the following function on a package. When running that the function against non-matching record, the result is always 1.
This is how I run the function and see output in DBMS Output window
set serveroutpuut on;
begin
dbms_output.put_line (BP$BUSINESSPARNTNER_CODE.Check_rec('44887'));
end;
This is the function that's responsible for query and counting the records
FUNCTION Check_rec(PartnerNumber IN VARCHAR2) RETURN NUMBER IS v_count NUMBER;
BEGIN
select count(PartnerNumber) into v_count from BusinessPartner where PartnerNumber = PartnerNumber and code is not null;
RETURN v_count;
END;
I have tried running with the hard-coded value and it works
FUNCTION Check_rec(PartnerNumber IN VARCHAR2) RETURN NUMBER IS v_count NUMBER;
BEGIN
select count(PartnerNumber) into v_count from BusinessPartner where PartnerNumber = '44887' and code is not null;
RETURN v_count;
END;
Please try changing the name of your IN parameter so that it does not conflict with column names:
FUNCTION Check_rec(pn IN VARCHAR2) RETURN NUMBER IS v_count NUMBER;
BEGIN
SELECT COUNT(PartnerNumber) INTO v_count
FROM BusinessPartner
WHERE PartnerNumber = pn AND code IS NOT NULL;
RETURN v_count;
END;
I don't know the rules which Oracle would use to resolve your current situation, but even if it could be made to work, it would probably be best to just avoid it.
My question is pretty basic but I am complete newbie to stored procedure and need to get around quickly. Any help will be appreciated,
Below is the current stored procedure we have,
PROCEDURE get_something(
type IN VARCHAR2,
value IN VARCHAR2,
i_type OUT VARCHAR2,
i_id OUT VARCHAR2)
AS
TYPE t_array
IS
TABLE OF VARCHAR2(320);
identifers t_array;
column_name VARCHAR2(32);
info_qry VARCHAR2(500);
top_row_qry VARCHAR2(500);
BEGIN
identifers := t_array('ABC');
column_name := get_column_name(type);
info_qry := 'select INS.i_id, INS.model from table1 INS where INS.'||column_name||'='''||value||''' order by INS.version desc';
top_row_qry := 'select * from ('||info_qry||') where rownum<=1';
EXECUTE immediate top_row_qry INTO i_id, i_type;
EXCEPTION
WHEN OTHERS THEN
i_id := NULL;
i_type := NULL;
END get_something;
Now when I execute this procedure, it gives me one record due to the rownum condition.
My requirement is to remove the rownum from the top_row_qry, so result would be multiple rows.
I want to store each field into some variable out of which I will use one of the variable for some comparison in the procedure itself.
So basically i want to store the results which I can later loop over and compare with a list of values.
Also, I need to define the list of values in this procedure itself.
Something like below:
list_of_vals:= t_array('ABC','XYZ');
Can anyone help me on this.
I guess you need to use cursor as OUT parameter for your procedure and later use it for your requirement.
Also for "I need to define the list of values in this procedure itself."
you can use a collection the way I have used in the procedure code (v_array).
You can then use a loop to traverse through multiple values for the v_array.
CREATE ORE REPLACE PROCEDURE get_something(type IN VARCHAR2,
value IN VARCHAR2,
out_param OUT sys_refcursor)
AS
TYPE t_array IS TABLE OF VARCHAR2(320);
v_array t_array:=t_array();
-- identifers t_array;
column_name VARCHAR2(32);
info_qry VARCHAR2(500);
top_row_qry VARCHAR2(500);
BEGIN
--To define multiple values for a collection e.g. ABC, XYZ
v_array.EXTEND(2) ;
v_array(1):='ABC' ;
v_array(2):='XYZ' ;
--identifers := t_array('ABC') ;
column_name := get_column_name(TYPE) ;
info_qry := 'select INS.i_id, INS.model from table1 INS where INS.'||column_name||'='''||VALUE||''' order by INS.version desc';
top_row_qry := 'select * from ('||info_qry||')' ;
OPEN out_param FOR top_row_qry ;
EXCEPTION
WHEN OTHERS THEN
OPEN out_param FOR 'select null, null from dual' ;
END get_something ;
/
--To execute the procedure
DECLARE
lv_type VARCHAR2(200);
lv_id NUMBER;
lv_cur SYS_REFCURSOR;
BEGIN
get_something('param1','param2',lv_cur);
LOOP
FETCH lv_cur INTO lv_id,lv_type;
EXIT WHEN lv_cur%NOTFOUND;
--Do something.....
END LOOP;
END;
I want to write a function to return the row count of a table whose name is passed in as a variable. Here's my code:
create or replace function get_table_count (table_name IN varchar2)
return number
is
tbl_nm varchar(100) := table_name;
table_count number;
begin
select count(*)
into table_count
from tbl_nm;
dbms_output.put_line(table_count);
return table_count;
end;
I get this error:
FUNCTION GET_TABLE_COUNT compiled
Errors: check compiler log
Error(7,5): PL/SQL: SQL Statement ignored
Error(9,8): PL/SQL: ORA-00942: table or view does not exist
I understand that tbl_nm is being interpreted as a value and not a reference and I'm not sure how to escape that.
You can use dynamic SQL:
create or replace function get_table_count (table_name IN varchar2)
return number
is
table_count number;
begin
execute immediate 'select count(*) from ' || table_name into table_count;
dbms_output.put_line(table_count);
return table_count;
end;
There is also an indirect way to get number of rows (using system views):
create or replace function get_table_count (table_name IN varchar2)
return number
is
table_count number;
begin
select num_rows
into table_count
from user_tables
where table_name = table_name;
return table_count;
end;
The second way works only if you had gathered statistics on table before invoking this function.
I want to call a function inside a procedure and get it's value so I can use within the procedure but I have no idea how to do that ??
Any ideas ??
My function is the following one , this function returns a collection of employees' ids whose salary out of the max or min salary , I want to use this value in a procedure to update the salaries of those employees to the minimum salary ??
create or replace procedure P
a_variable_name number;
begin
a_variable := a_function('a parameter');
end;
/
Here is the sample code that will help you calling a function from a procedure.
create or replace FUNCTION ADD_TEN(P_IN VARCHAR2) RETURN VARCHAR2
AS
L_RESULT VARCHAR2(4000);
BEGIN
L_RESULT:=P_IN+10;
RETURN L_RESULT;
END;
create or replace PROCEDURE CALL_FUNCTON(P_IN VARCHAR2)
AS
L_FINAL_RESULT VARCHAR2(4000);
BEGIN
L_FINAL_RESULT:=ADD_TEN(P_IN);
DBMS_OUTPUT.PUT_LINE(L_FINAL_RESULT);
-- USE L_FINAL_RESULT IN YOUR BUSINESS LOGIC
END;
EXECUTE CALL_FUNCTON(5);
declare
procedure my_proc;
function my_func return number is
begin
my_proc;
return 3;
end my_func;
procedure my_proc is
begin
dbms_output.put_line('22');
end my_proc;
begin
dbms_output.put_line(my_func);
end;