SELECT INTO variable not working in PL\SQL function - oracle

Not sure why this isn't working, when the base query (the commented out line) does return a value when I pass in the same id - when executed as a straight query outside the function.
Maybe I need to use execute immediate, but not sure why this is returning a "no data" error. I got it working with execute immediate, but interested to know what the issue is with the syntax below.
create or replace FUNCTION "FN_GET_CSP_TEMPLATE_DETAILS" (
TEMPLATE_USED_ID IN VARCHAR2 DEFAULT NULL
)
RETURN VARCHAR2 AS
v_out VARCHAR2(100);
BEGIN
dbms_output.put_line(TEMPLATE_USED_ID);
SELECT i_chronicle_id INTO v_out from TABLE_A where R_OBJECT_ID = TEMPLATE_USED_ID;
-- // this works // SELECT i_chronicle_id INTO v_out from TABLE_A where R_OBJECT_ID = 'ID_99';
dbms_output.put_line(v_out);
return v_out;
end;
So this returns a count of 1:
select count(*) from TABLE_A WHERE R_OBJECT_ID = 'ID_99';
But this returns a null value, function defined as above:
select FN_GET_CSP_TEMPLATE_DETAILS('ID_99') from dual;
This works fine:
create or replace FUNCTION "FN_GET_CSP_TEMPLATE_DETAILS" (
TEMPLATE_USED_ID IN VARCHAR2 DEFAULT NULL
)
RETURN VARCHAR2 AS
v_out VARCHAR2(100);
sql_stmt VARCHAR2(1000);
BEGIN
dbms_output.put_line(TEMPLATE_USED_ID);
sql_stmt := 'SELECT i_chronicle_id from TABLE_A where R_OBJECT_ID = :a';
EXECUTE IMMEDIATE sql_stmt into v_out using TEMPLATE_USED_ID;
dbms_output.put_line(v_out);
return v_out;
end;
Seems it only doesnt work against this production table. If I recreate a dummy table and a function against the dummy table it works.
CREATE TABLE "ANALYTICS"."TEST_CSP_FUNCTION"
( "R_OBJECT_ID" VARCHAR2(20 BYTE),
"I_CHRONICLE_ID" VARCHAR2(20 BYTE)
)
INSERT INTO TEST_CSP_FUNCTION
(R_OBJECT_ID, I_CHRONICLE_ID)
VALUES
('ID_100', 'Doc A');
INSERT INTO TEST_CSP_FUNCTION
(R_OBJECT_ID, I_CHRONICLE_ID)
VALUES
('ID_101', 'Doc B');
INSERT INTO TEST_CSP_FUNCTION
(R_OBJECT_ID, I_CHRONICLE_ID)
VALUES
('ID_102', 'Doc C');
INSERT INTO TEST_CSP_FUNCTION
(R_OBJECT_ID, I_CHRONICLE_ID)
VALUES
('ID_103', 'Doc D');
create or replace FUNCTION "FN_GET_TEST_CSP_TEMPLATE_DETAILS" (
TEMPLATE_USED_ID IN NVARCHAR2 DEFAULT NULL
)
RETURN VARCHAR2 AS
v_out VARCHAR2(100);
BEGIN
dbms_output.put_line(TEMPLATE_USED_ID);
SELECT I_CHRONICLE_ID INTO v_out FROM TEST_CSP_FUNCTION WHERE R_OBJECT_ID = TEMPLATE_USED_ID;
dbms_output.put_line(v_out);
return v_out;
end;
select FN_GET_TEST_CSP_TEMPLATE_DETAILS(N'ID_103') from dual;
returns:
Doc D

It works if row with appropriate ID exists in the table.
Demo:
SQL> select * From table_a;
R_OBJ I_CHRONICLE_ID
----- --------------
id_99 100
Function:
SQL> create or replace function fn_get_csp_template_details
2 (template_used_id in varchar2 default null)
3 return varchar2 as
4 v_out varchar2(100);
5 begin
6 dbms_output.put_line(template_used_id);
7 select i_chronicle_id
8 into v_out
9 from table_a
10 where r_object_id = template_used_id;
11 dbms_output.put_line(v_out);
12 return v_out;
13 end;
14 /
Function created.
Testing:
SQL> set serveroutput on
SQL> select fn_get_csp_template_details('id_99') as result from dual;
RESULT
--------------------------------------------------------------------------------
100
id_99
100
SQL>
If ID doesn't exist, it'll return NULL (if called from SELECT statement, as in my example):
SQL> select fn_get_csp_template_details('ABC') as result from dual;
RESULT
--------------------------------------------------------------------------------
SQL>
It'll return an exception if called elsewhere:
SQL> declare
2 l_result varchar2(100);
3 begin
4 l_result := fn_get_csp_template_Details('ABC');
5 end;
6 /
declare
*
ERROR at line 1:
ORA-01403: no data found
ORA-06512: at "SCOTT.FN_GET_CSP_TEMPLATE_DETAILS", line 6
ORA-06512: at line 4
SQL>
That's how it goes. So, what did you do to make it NOT work as expected?

Related

How to write PL/SQL function which returns the result of a select statement having as pararmeters the function's parameters?

I tried to write a PL/SQL function having as parameters a tablename and a column name, which returns the result of the query as a table.
Here's what I tried:
CREATE TYPE TABLE_RES_OBJ AS OBJECT (
employee_id number(30) ,
person_id number(30),
joined_year number(4),
salary number(20,2),
qualification varchar2(45),
department varchar2(45)
);
/
create TYPE table_ret as TABLE OF table_res_obj;
/
create or replace function select_custom(p_tablename varchar2, p_colname varchar2 ) return table_ret
is
ret table_ret;
query_txt varchar2(100) := 'SELECT :a from :b';
begin
execute immediate query_txt bulk collect into ret using p_colname, p_tablename;
return ret;
end select_custom;
As you can see, this is not that general as wanted, but still not working, it says the table doesn't exist, even when I try to run it with an existing table.
Exactly, it won't work that way. You'll have to concatenate table and column name into the select statement. For (simple) example:
SQL> create or replace type table_res_obj as object
2 (ename varchar2(20));
3 /
Type created.
SQL> create or replace type table_ret as table of table_res_obj;
2 /
Type created.
SQL> create or replace function select_custom
2 (p_tablename varchar2, p_colname varchar2 )
3 return table_ret
4 is
5 ret table_ret;
6 query_txt varchar2(100);
7 begin
8 query_txt := 'select table_res_obj(' || dbms_assert.simple_sql_name(p_colname) ||') from ' ||
9 dbms_assert.sql_object_name(p_tablename);
10 execute immediate query_txt bulk collect into ret;
11 return ret;
12 end select_custom;
13 /
Function created.
Does it work?
SQL> select select_custom('dept', 'deptno') from dual;
SELECT_CUSTOM('DEPT','DEPTNO')(ENAME)
--------------------------------------------------------------------------------
TABLE_RET(TABLE_RES_OBJ('10'), TABLE_RES_OBJ('20'), TABLE_RES_OBJ('30'), TABLE_R
ES_OBJ('40'))
SQL>

How to pass string of comma-separated numbers to stored procedure in condition for numeric field?

I have a stored procedure like below where multiple employee IDs will be passed as comma-separated value (multiple IDs). It is throwing error as "ORA-01722: invalid number". I know it's because of passing varchar2 variable for the numeric ID column. But is there any way we can achieve this simply?
create or replace PROCEDURE Fetch_Emp_Name(Emp_id in varchar2)
IS
BEGIN
select Name from EMP where id in (emp_id);
END;
You can use dynamic sql.
create or replace PROCEDURE Fetch_Emp_Name(emp_id in varchar2) IS
v_result varchar2;
begin
execute immediate
'select Name from EMP where id in (' || 'emp_id' || ')'
into
v_result;
end;
Also you can use package dbms_sql for dynamic sql.
Update
Another approach. I think may be better.
create or replace PROCEDURE Fetch_Emp_Name(emp_id in varchar2) IS
v_result varchar2;
begin
select
Name
from
EMP
where
id in
(
select
to_number(regexp_substr(emp_id, '[^,]+', 1, level))
from
dual
connect by regexp_substr(emp_id, '[^,]+', 1, level) is not null
);
exception
when no_data_found then
-- error1;
when too_many_rows then
-- error2;
end;
Sorry for before, I did not get the question in the right way. If you get a lot of IDs as different parameters, you could retrieve the list of names as an string split by comma as well. I put this code where I handled by regexp_substr the name of different emp_ids you might enter in the input parameter.
Example ( I am assuming that the IDs are split by comma )
create or replace PROCEDURE Fetch_Emp_Name(p_empid in varchar2) IS
v_result varchar2(4000);
v_append emp.name%type;
v_emp emp.emp_id%type;
counter pls_integer;
i pls_integer;
begin
-- loop over the ids
counter := REGEXP_COUNT(p_empid ,'[,]') ;
--dbms_output.put_line(p_empid);
if counter > 0
then
i := 0;
for r in ( SELECT to_number(regexp_substr(p_empid,'[^,]+',1,level)) as mycol FROM dual CONNECT BY LEVEL <= REGEXP_COUNT(p_empid ,'[,]')+1 )
loop
--dbms_output.put_line(r.mycol);
v_emp := r.mycol ;
select name into v_append from emp where emp_id = v_emp;
if i < 1
then
v_result := v_append ;
else
v_result := v_result ||','|| v_append ;
end if;
i := i + 1;
end loop;
else
v_emp := to_number(p_empid);
select name into v_result from emp where emp_id = v_emp;
end if;
dbms_output.put_line(v_result);
exception
when no_data_found
then
raise_application_error(-20001,'Not Employee found for '||v_emp||' ');
when too_many_rows
then
raise_application_error(-20002,'Too many employees for id '||v_emp||' ');
end;
Test
SQL> create table emp ( emp_id number, name varchar2(2) ) ;
Table created.
SQL> insert into emp values ( 1 , 'AA' );
1 row created.
SQL> insert into emp values ( 2 , 'BB' ) ;
1 row created.
SQL> commit;
SQL> insert into emp values ( 3 , 'CC' ) ;
1 row created.
SQL> select * from emp ;
EMP_ID NA
---------- --
1 AA
2 BB
3 CC
SQL> exec Fetch_Emp_Name('1') ;
AA
PL/SQL procedure successfully completed.
SQL> exec Fetch_Emp_Name('1,2,3') ;
AA,BB,CC
PL/SQL procedure successfully completed.
SQL>

ORA-06553: PLS-801: internal error [55018] when testing function returning ROWTYPE

I need to test some function returning ROWTYPE variable in Toad. When I try to run it, I aget the Internal error.
I run is as
SELECT MYPACKAGE.MyFunction(param1, aram2, param3) FROM DUAL
Is there any way to test a function returning ROWTYPE for Toad?
As you just want to test the function you could use an anonymous PL/SQL block to call it and assign its result to a matching rowtype variable, e.g.:
declare
l_row mytable%rowtype;
begin
-- call the function and assign the result to a variable
l_row := mypackage.myfunction(1, 2, 3);
-- do something with the result
dbms_output.put_line(l_row.some_columns);
end;
/
Quick demo with a made-up table and expanded function:
create table mytable (col1, col2, col3, col4, col5) as
select 1, 2, 3, 'test', sysdate from dual;
create or replace package mypackage as
function myfunction (param1 number, param2 number, param3 number)
return mytable%rowtype;
end mypackage;
/
create or replace package body mypackage as
function myfunction (param1 number, param2 number, param3 number)
return mytable%rowtype is
l_row mytable%rowtype;
begin
select * into l_row
from mytable
where col1 = param1
and col2 = param2
and col3 = param3;
return l_row;
end myfunction;
end mypackage;
/
Calling from SQL gets the same error you see now:
select mypackage.myfunction(1, 2, 3) from dual;
SQL Error: ORA-06553: PLS-801: internal error [55018]
But with a block (run here through SQL Developer with output enabled):
set serveroutput on
declare
l_row mytable%rowtype;
begin
-- call the function and assign the result to a variable
l_row := mypackage.myfunction(1, 2, 3);
-- do something with the result
dbms_output.put_line(l_row.col4 ||':'|| l_row.col5);
end;
/
test:2019-04-29
PL/SQL procedure successfully completed.
db<>fiddle
True, it won't work. Function, when used in a SQL query, is supposed to return a SQL datatype, while %ROWTYPE is a PL/SQL record.
This is what you, probably, have now:
SQL> create or replace function f_test (par_deptno in number)
2 return dept%rowtype
3 is
4 retval dept%rowtype;
5 begin
6 select deptno, dname, loc
7 into retval
8 from dept
9 where deptno = par_deptno;
10 return retval;
11 end;
12 /
Function created.
SQL> select f_test(10) From dual;
select f_test(10) From dual
*
ERROR at line 1:
ORA-06553: PLS-801: internal error [55018]
SQL>
Option you might choose is to create (and return) an object type. Here's an example:
SQL> create or replace type dept_type as object
2 (deptno number,
3 dname varchar2(20),
4 loc varchar2(20));
5 /
Type created.
SQL> create or replace function f_test (par_deptno in number)
2 return dept_type
3 is
4 retval dept_type;
5 begin
6 select dept_type(deptno, dname, loc)
7 into retval
8 from dept
9 where deptno = par_deptno;
10 return retval;
11 end;
12 /
Function created.
SQL> select f_test(10).dname From dual;
F_TEST(10).DNAME
--------------------
ACCOUNTING
SQL>

Using count(*) to fetch more than one row in a SQL Procedure

I'm trying to return the number of rows per invoice_id using a function and procedure. Some invoice_id's have more than one row and I'm not sure how to fetch the count when I execute my procedure. As an example invoice_id(7) has just one row, but invoice_id(100) has four rows of information.
Create or replace function return_num_rows_function(invoice_id_text in varchar2)
Return varchar2
Is inv_id varchar2(20);
Begin
Select count(*)invoice_id into inv_id from invoice_line_items where invoice_id=invoice_id_text;
Return inv_id;
End;
Create or replace procedure return_num_rows (invoice_id_text in varchar2)
Is inv_id varchar(20);
line_item_desc invoice_line_items.line_item_description%type;
Begin
inv_id := return_num_rows_function(invoice_id_text);
If inv_id is not null then
Select count(*)invoice_id, line_item_description into inv_id,line_item_desc
From invoice_line_items where invoice_id = inv_id;
dbms_output.put_line('The number of rows returned:'|| inv_id);
dbms_output.put_line('Item description(s):'|| line_item_desc);
End if;
End;
set serveroutput on;
execute return_num_rows(7);
First of all do not use a string type variable for a numeric one
(invoice_id_text).
For your case it's better to use a procedure instead of called
function ( return_num_rows_function ), since you need two out
arguments returned.
A SQL Select statement cannot be used without Group By with aggegated and non-aggregated columns together ( i.e. don't use this one :
Select count(*) invoice_id, line_item_description
into inv_id,line_item_desc
From invoice_line_items
Where invoice_id = inv_id;
)
So, Try to create below procedures :
SQL> CREATE OR REPLACE Procedure
return_num_rows_proc(
i_invoice_id invoice_line_items.invoice_id%type,
inv_id out pls_integer,
line_item_desc out invoice_line_items.line_item_description%type
) Is
Begin
for c in
(
Select line_item_description
into line_item_desc
From invoice_line_items
Where invoice_id = i_invoice_id
)
loop
line_item_desc := line_item_desc||' '||c.line_item_description;
inv_id := nvl(inv_id,0) + 1;
end loop;
End;
/
SQL> CREATE OR REPLACE Procedure
return_num_rows(
i_invoice_id pls_integer
) Is
inv_id pls_integer;
line_item_desc invoice_line_items.line_item_description%type;
Begin
return_num_rows_proc(i_invoice_id,inv_id,line_item_desc);
If inv_id is not null then
dbms_output.put_line('The number of rows returned:' || inv_id);
dbms_output.put_line('Item description(s):' || line_item_desc);
End if;
End;
/
and call as in your case :
SQL> set serveroutput on;
SQL> execute return_num_rows(7);
Replace inv_id varchar2(20) with inv_id number;
and also if you want to get two outputs from procedure better to use refcursor.

how to dynamically insert column value into table2 select * from another table1 which is having one column less than table2

table1 :
t1_column1 t1_column2 t1_column3
1 2 3
2 3 4
table2 is :
t2_column1 t2_column2 t2_column3 t2_column4
1 2 3 0
2 3 4 0
Here I am passing the value as function argument, but when I am
trying to create this function getting this error : Error(11,7):
PL/SQL: SQL Statement ignored and Error(11,108): PL/SQL: ORA-00917:
missing comma
CREATE OR REPLACE FUNCTION NEW_PURCHASE(GODOWN_CODE IN VARCHAR2,MON IN
VARCHAR2)
RETURN VARCHAR2 IS
var_mon VARCHAR2(100);
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
var_mon:=MON;
INSERT INTO table2
( t2_column1, t2_column2, t2_column3, var_mon)
SELECT t1_column1, t1_column2, t1_column3
FROM table1 WHERE t1_column1=GODOWN_CODE;
DELETE FROM table1 WHERE t1_column1=GODOWN_CODE;
COMMIT;
RETURN 'done';
END NEW_PURCHASE;
Try this:
CREATE OR REPLACE FUNCTION NEW_PURCHASE (GODOWN_CODE IN VARCHAR2,
MON IN VARCHAR2)
RETURN VARCHAR2
IS
--var_mon VARCHAR2 (100);
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
--var_mon := 'MON';
INSERT INTO table2 (t2_column1,
t2_column2,
t2_column3,
t2_column4)
SELECT t1_column1, t1_column2, t1_column3, mon
FROM table1
WHERE t1_column1 = GODOWN_CODE;
DELETE FROM table1
WHERE t1_column1 = GODOWN_CODE;
COMMIT;
RETURN 'done';
END NEW_PURCHASE;
Note that if you do DML operation in a function, you cannot call it from a select statement, You would need a PLSQL block to run it.
For ex:
You can call it as below:
declare
l_message varchar2(30);
begin
l_message := test_func('123');
end;
... but not like this:
select test_func(empno) from emp;

Resources