Oracle: function only returning null - sql

The following function is supposed to return information relating to a particular meeting, stored in a meeting table:
CREATE TABLE "MEETING"
( "MEETING_ID" NUMBER(10,0) NOT NULL ENABLE,
"TIME" TIMESTAMP (4) NOT NULL ENABLE,
"LOCATION" VARCHAR2(40),
"MAP_HREF" VARCHAR2(140),
"FK_INTEREST_ID" CHAR(4) NOT NULL ENABLE,
"ADDITIONAL_INFO" CLOB,
"PASSED" NUMBER(1,0),
"TITLE" VARCHAR2(20),
CONSTRAINT "MEETING_PK" PRIMARY KEY ("MEETING_ID") ENABLE
) ;
The code compiles just fine, and runs fine as well.
However, if the meeting exists, only null is returned. If the meeting doesn't exist the exception prints 'UNKNOWN APPOINTMENT' correctly.
CREATE OR REPLACE FUNCTION GetMeeting
(meetingnumber MEETING.MEETING_ID%TYPE)
RETURN VARCHAR
IS
CURSOR current_meeting(meetingnumber MEETING.MEETING_ID%TYPE)
IS
SELECT TITLE
FROM MEETING
WHERE MEETING_ID = meetingnumber;
r_meeting current_meeting%ROWTYPE;
BEGIN
OPEN current_meeting(meetingnumber);
FETCH current_meeting INTO r_meeting;
IF current_meeting%NOTFOUND THEN
r_meeting.TITLE := 'UNKNOWN APPOINTMENT';
END IF;
CLOSE current_meeting;
RETURN r_meeting.TITLE;
END;
SELECT GetMeeting (27) appointment
FROM MEETING;

Seems this is an exercise in using cursors? Its much more complicated than it needs to be. Try something like (untested):
create or replace function get_meeting(i_meetingnumber MEETING.MEETING_ID%TYPE)
RETURN VARCHAR2
IS
l_title MEETING.TITLE%TYPE;
BEGIN
select title
into l_title
FROM MEETING
WHERE MEETING_ID = i_meetingnumber;
return l_title;
EXCEPTION
when no_data_found then
return 'UNKNOWN APPOINTMENT';
when others then raise;
END;
This is also a bit unnecessary to put this small logic in a function, I would simply select it as needed (via a join of a larger SQL or individually in a larger pl/sql procedure)
Also, I notice that your original function returns VARCHAR where title is VARCHAR2. Not sure off hand if the conversion is done implicitly by Oracle, but something worth mentioning.

SELECT NVL(TITLE, 'UNKNOWN APPOINTMENT') FROM MEETING WHERE MEETING_ID = meetingnumber;
Is much cleaner.

check below statement:
IF r_meeting%NOTFOUND THEN
r_meeting.TITLE := 'UNKNOWN APPOINTMENT';
END IF;
CLOSE current_meeting;
RETURN r_meeting.TITLE;
END;

PL/SQL Function works fine. It returns the desired result, but your select returns as much datasets, as are present in MEETING. You should select from dual instead.

Related

PL/SQL returning contents of a table

Currently working on a .Net application for viewing the contents of a table, I faced difficulties with the function that returns the contents of the table.
I use the Oracle WebLogic Server to interface comics and application, I have a function that returns a desired record of the table but I unable to adapt to it returns all records of table.
I used tables, objects I have not been able to operate the pipeline.
Here is the function that returns a record.
CREATE OR REPLACE TYPE DMD_REC AS OBJECT
(
matricule VARCHAR2(10),
nom VARCHAR2(15),
prenom VARCHAR2(15),
adresse VARCHAR2(10),
profile VARCHAR2(15),
service VARCHAR2(15),
date_dmd DATE
);
CREATE OR REPLACE FUNCTION aff_dmd
(dmd_mat IN VARCHAR2)
RETURN DMD_REC IS
dmd_found demande%rowtype;
dmd_rtn DMD_REC;
BEGIN
SELECT *
INTO dmd_found
FROM demande
WHERE demande.matricule=dmd_mat;
dmd_rtn := DMD_REC
(
dmd_found.matricule,
dmd_found.nom,
dmd_found.prenom,
dmd_found.adresse,
dmd_found.profile,
dmd_found.service,
dmd_found.date_dmd
);
RETURN dmd_rtn;
END aff_dmd;
Sorry for my english
Your function returns an object which fits one row. You need it to return a table of such objects.
So first you need a table type:
CREATE OR REPLACE TYPE DMD_NT AS TABLE OF DMD_REC;
Then you have to convert your function to use this new type. In the absence of any other requirements this revision will select all records in the demande table when a NULL is passed:
CREATE OR REPLACE FUNCTION aff_dmd
(dmd_mat IN VARCHAR2 := null)
RETURN DMD_NT
IS
dmd_rtn DMD_NT;
BEGIN
SELECT DMD_REC
(
d.matricule,
d.nom,
d.prenom,
d.adresse,
d.profile,
d.service,
d.date_dmd
)
BULK COLLECT INTO dmd_rtn
FROM demande
WHERE
( dmd_mat is null
or d.matricule = dmd_mat
);
RETURN dmd_rtn;
END aff_dmd;
/
The BULK COLLECT populates a collection which is stored in session memory. If your table is large (say more than 5000) this may create problems with memory resources. In which case you should consider creating a pipelined function:
CREATE OR REPLACE FUNCTION aff_dmd
(dmd_mat IN VARCHAR2 := null)
RETURN DMD_NT PIPELINED
IS
dmd_rtn DMD_NT;
BEGIN
loop
SELECT DMD_REC
(
d.matricule,
d.nom,
d.prenom,
d.adresse,
d.profile,
d.service,
d.date_dmd
)
BULK COLLECT INTO dmd_rtn limit 1000
FROM demande
WHERE
( dmd_mat is null
or d.matricule = dmd_mat
);
exit when dmd_rtn.count() = 0;
for idx in 1..dmd_rtn.count() loop
pipe row (dmd_rtn(idx));
end loop;
end loop
RETURN;
END aff_dmd;
Pipelined functions have an overhead. You should consider whether there is a way you can make it work with SYS_REFCURSOR as #BobJarvis suggested.

How to resolve the component must be declared using oracle Types

I have two oracle types like
create or replace
TYPE T_EMPLOYEE
AS TABLE OF O_EMPLOYEE;
And my O_EMPLOYEE TYPE is
create or replace
TYPE O_EMPLOYEE
AS OBJECT
(
EMP_NAME VARCHAR2(50),
EMP_ID VARCHAR2(50),
EMP_DES VARCHAR2(50)
);
I am using this as an input in a store procedure where i need to check for the validation of name,id and designation. Using following i can convert the table in a select statement.
TABLE(CAST( I_T_EMPLOYEE AS T_EMPLOYEE)) emp,
but I tried to read the value like like T_EMPLOYEE.EMP_NAME, it is saying componenet EMP_NAME must be decalred.
can any one help?
thank you shabilan. There are two ways we can retrieve the value. first one
By writing following in store procedure. you need to declare the variables for
IS
V_EMP_NAME VARCHAR2(50),
V_EMP_ID VARCHAR2(50),
V_EMP_DES VARCHAR2(50),
BEGIN
SELECT PROCESS_TYPE,REQUEST_TYPE,STATUS,EFFECTIVE_DATE_RANGE
INTO V_EMP_NAME, V_EMP_ID, V_EMP_DES
FROM TABLE(CAST(I_T_EMPLOYEE AS T_EMPLOYEE)) ITE;
--validation part
if(V_EMP_NAME is null or V_EMP_ID or V_EMP_DES ) then RAISE NULL_DATA;
END IF;
This one will fail if you pass multiple Object Types in store procedure like
T_E_I :=T_EMPLOYEE(O_EMPLOYEE('Test','1234','manager'),
O_EMPLOYEE('Test','1234','manager'));
GET_DATA_PKG.EMPLOYEE_ORDER_BY_ROLE(T_E_I, :o_error_code,
:o_error_message);
2nd approach handles the multiple inputs. this is done using cursor.you need to declare the variables for
IS
OEM O_EMPLOYEE := O_EMPLOYEE(null,null,null);
c_EMP_NAME OEM.EMP_NAME%type,
c_EMP_ID OEM.EMP_ID%type,
c_EMP_DES OEM.EMP_DES%type,
Cursor emp_role_curs is
SELECT PROCESS_TYPE,REQUEST_TYPE,STATUS,EFFECTIVE_DATE_RANGE
FROM TABLE(CAST(I_T_EMPLOYEE AS T_EMPLOYEE)) ITE;
BEGIN
OPEN emp_role_curs;
loop
fetch emp_role_curs into C_EMP_NAME, C_EMP_ID, C_EMP_DES
FROM TABLE(CAST(I_T_EMPLOYEE AS T_EMPLOYEE)) ITE;
--validation part
if(V_EMP_NAME is null or V_EMP_ID or V_EMP_DES ) then RAISE NULL_DATA;
END IF;
EXIT WHEN emp_role_curs%notfound;
END LOOP;
CLOSE emp_role_curs;
I hope this is useful if some one want to read the object type as a table.

Loop in function does not work as expected

Using PostgreSQL 9.0.4
Below is a very similar structure of my table:
CREATE TABLE departamento
(
id bigserial NOT NULL,
master_fk bigint,
nome character varying(100) NOT NULL
CONSTRAINT departamento_pkey PRIMARY KEY (id),
CONSTRAINT departamento_master_fk_fkey FOREIGN KEY (master_fk)
REFERENCES departamento (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
And the function I created:
CREATE OR REPLACE FUNCTION fn_retornar_dptos_ate_raiz(bigint[])
RETURNS bigint[] AS
$BODY$
DECLARE
lista_ini_dptos ALIAS FOR $1;
dp_row departamento%ROWTYPE;
dpto bigint;
retorno_dptos bigint[];
BEGIN
BEGIN
PERFORM id FROM tbl_temp_dptos;
EXCEPTION
WHEN undefined_table THEN
EXECUTE 'CREATE TEMPORARY TABLE tbl_temp_dptos (id bigint NOT NULL) ON COMMIT DELETE ROWS';
END;
FOR i IN array_lower(lista_ini_dptos, 1)..array_upper(lista_ini_dptos, 1) LOOP
SELECT id, master_fk INTO dp_row FROM departamento WHERE id=lista_ini_dptos[i];
IF dp_row.id IS NOT NULL THEN
EXECUTE 'INSERT INTO tbl_temp_dptos VALUES ($1)' USING dp_row.id;
WHILE dp_row.master_fk IS NOT NULL LOOP
dpto := dp_row.master_fk;
SELECT id, master_fk INTO dp_row FROM departamento WHERE id=lista_ini_dptos[i];
EXECUTE 'INSERT INTO tbl_temp_dptos VALUES ($1)' USING dp_row.id;
END LOOP;
END IF;
END LOOP;
RETURN ARRAY(SELECT id FROM tbl_temp_dptos);
END;
$BODY$
LANGUAGE plpgsql VOLATILE
Any questions about the names I can translate ..
What is the idea of the function? I first check if the temporary table already exists (perform), and when the exception occurs I create a temporary table.
Then I take each element in the array and use it to fetch the id and master_fk of a department. If the search is successful (check if id is not null, it is even unnecessary) I insert the id in the temporary table and start a new loop.
The second loop is intended to get all parents of that department which was previously found by performing the previous steps (ie, pick a department and insert it into the temporary table).
At the end of the second loop returns to the first. When this one ends I return bigint[] refers to what was recorded in the temporary table.
My problem is that the function returns me the same list I provide. What am I doing wrong?
There is a lot I would do differently, and to great effect.
Table definition
Starting with the table definition and naming conventions. These are mostly just opinions:
CREATE TEMP TABLE conta (conta_id bigint primary key, ...);
CREATE TEMP TABLE departamento (
dept_id serial PRIMARY KEY
, master_id int REFERENCES departamento (dept_id)
, conta_id bigint NOT NULL REFERENCES conta (conta_id)
, nome text NOT NULL
);
Major points
Are you sure you need a bigserial for departments? There are hardly that many on this planet. A plain serial should suffice.
I hardly ever use character varying with a length restriction. Unlike with some other RDBMS there is no performance gain whatsoever by using a restriction. Add a CHECK constraint if you really need to enforce a maximum length. I just use text, mostly and save myself the trouble.
I suggest a naming convention where the foreign key column shares the name with the referenced column, so master_id instead of master_fk, etc. Also allows to use USING in joins.
And I rarely use the non-descriptive column name id. Using dept_id instead here.
PL/pgSQL function
It can be largely simplified to:
CREATE OR REPLACE FUNCTION f_retornar_plpgsql(lista_ini_depts VARIADIC int[])
RETURNS int[] AS
$func$
DECLARE
_row departamento; -- %ROWTYPE is just noise
BEGIN
IF NOT EXISTS ( -- simpler in 9.1+, see below
SELECT FROM pg_catalog.pg_class
WHERE relnamespace = pg_my_temp_schema()
AND relname = 'tbl_temp_dptos') THEN
CREATE TEMP TABLE tbl_temp_dptos (dept_id bigint NOT NULL)
ON COMMIT DELETE ROWS;
END IF;
FOR i IN array_lower(lista_ini_depts, 1) -- simpler in 9.1+, see below
.. array_upper(lista_ini_depts, 1) LOOP
SELECT * INTO _row -- since rowtype is defined, * is best
FROM departamento
WHERE dept_id = lista_ini_depts[i];
CONTINUE WHEN NOT FOUND;
INSERT INTO tbl_temp_dptos VALUES (_row.dept_id);
LOOP
SELECT * INTO _row
FROM departamento
WHERE dept_id = _row.master_id;
EXIT WHEN NOT FOUND;
INSERT INTO tbl_temp_dptos
SELECT _row.dept_id
WHERE NOT EXISTS (
SELECT FROM tbl_temp_dptos
WHERE dept_id =_row.dept_id);
END LOOP;
END LOOP;
RETURN ARRAY(SELECT dept_id FROM tbl_temp_dptos);
END
$func$ LANGUAGE plpgsql;
Call:
SELECT f_retornar_plpgsql(2, 5);
Or:
SELECT f_retornar_plpgsql(VARIADIC '{2,5}');
ALIAS FOR $1 is outdated syntax and discouraged. Use function parameters instead.
The VARIADIC parameter makes it more convenient to call. Related:
Pass multiple values in single parameter
You don't need EXECUTE for queries without dynamic elements. Nothing to gain here.
You don't need exception handling to create a table. Quoting the manual here:
Tip: A block containing an EXCEPTION clause is significantly more
expensive to enter and exit than a block without one. Therefore, don't
use EXCEPTION without need.
Postgres 9.1 or later has CREATE TEMP TABLE IF NOT EXISTS. I use a workaround for 9.0 to conditionally create the temp table.
Postgres 9.1 also offer FOREACH to loop through an arrays.
All that said, here comes the bummer: you don't need most of this.
SQL function with rCTE
Even in Postgres 9.0, a recursive CTE makes this a whole lot simpler:
CREATE OR REPLACE FUNCTION f_retornar_sql(lista_ini_depts VARIADIC int[])
RETURNS int[] AS
$func$
WITH RECURSIVE cte AS (
SELECT dept_id, master_id
FROM unnest($1) AS t(dept_id)
JOIN departamento USING (dept_id)
UNION ALL
SELECT d.dept_id, d.master_id
FROM cte
JOIN departamento d ON d.dept_id = cte.master_id
)
SELECT ARRAY(SELECT DISTINCT dept_id FROM cte) -- distinct values
$func$ LANGUAGE sql;
Same call.
Closely related answer with explanation:
Tree Structure and Recursion
SQL Fiddle demonstrating both.
I managed to fix my code. At the end of this response is its final form, but if you have any suggestions for improvement are welcome. Here are the changes:
1 - I have provided the essential structure of my table, but in reality it is much bigger. Before master_fk field, there is a field called account_fk, and because of the variable department dp_row%**ROWTYPE** the entire structure of my table is copied to the variable, so if I fill only the first two fields, i.e., id and account_fk, then master_fk that is the third field will be null.
2 - #Nicolas was right, and I ended up using the variable dpto for the second loop. And I had forgotten to fill it inside the loop. Besides using it in the search done within the loop.
3 - I added an if statement to make sure that would not have duplicates in the temporary table.
Correction in the structure of my table:
CREATE TABLE departamento
(
id bigserial NOT NULL,
account_fk bigint NOT NULL,
master_fk bigint,
nome character varying(100) NOT NULL,
CONSTRAINT departamento_pkey PRIMARY KEY (id),
CONSTRAINT departamento_account_fk_fkey FOREIGN KEY (account_fk)
REFERENCES conta (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT departamento_master_fk_fkey FOREIGN KEY (master_fk)
REFERENCES departamento (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
My function as it is now:
CREATE OR REPLACE FUNCTION fn_retornar_dptos_ate_raiz(bigint[]) RETURNS bigint[] AS
$BODY$
DECLARE
lista_ini_dptos ALIAS FOR $1;
dp_row departamento%ROWTYPE;
dpto bigint;
BEGIN
BEGIN
PERFORM id FROM tbl_temp_dptos;
EXCEPTION
WHEN undefined_table THEN
EXECUTE 'CREATE TEMPORARY TABLE tbl_temp_dptos (id bigint NOT NULL) ON COMMIT DELETE ROWS';
END;
FOR i IN array_lower(lista_ini_dptos, 1)..array_upper(lista_ini_dptos, 1) LOOP
SELECT id, conta_fk, master_fk INTO dp_row FROM departamento WHERE id=lista_ini_dptos[i];
EXECUTE 'INSERT INTO tbl_temp_dptos VALUES ($1)' USING dp_row.id;
dpto := dp_row.master_fk;
-- RAISE NOTICE 'dp_row: (%); ', dp_row.master_fk;
WHILE dpto IS NOT NULL LOOP
SELECT id, conta_fk, master_fk INTO dp_row FROM departamento WHERE id=dpto;
IF NOT(select exists(select 1 from tbl_temp_dptos where id=dp_row.id limit 1)) THEN
EXECUTE 'INSERT INTO tbl_temp_dptos VALUES ($1)' USING dp_row.id;
END IF;
dpto := dp_row.master_fk;
-- RAISE NOTICE 'dp_row: (%); ', dp_row.master_fk;
END LOOP;
END LOOP;
RETURN ARRAY(SELECT id FROM tbl_temp_dptos);
END;
$BODY$
LANGUAGE plpgsql VOLATILE

Oracle Package or function in invalid state

Trying to create a pl/sql cursor based function to return details from an oracle database.
The relevant table, MEETING, has the columns
MEETING_ID: number(10), TIME: timestamp(4), TITLE: varchar(20)
CREATE OR REPLACE FUNCTION GetMeeting
(meetingnumber MEETING.MEETING_ID%TYPE)
RETURN VARCHAR
IS
CURSOR current_meeting(meetingnumber MEETING.MEETING_ID%TYPE)
IS
SELECT TITLE, TIME
FROM MEETING
WHERE MEETING_ID = meetingnumber;
r_meeting current_meeting%ROWTYPE;
BEGIN
OPEN current_meeting(meetingnumber);
FETCH current_meeting INTO r_meeting;
IF current_meeting%NOTFOUND THEN
r_meeting.TITLE := 'UNKNOWN APPOINTMENT';
END IF;
CLOSE current_meeting;
RETURN r_meeting.TITLE;
END;
SELECT GetMeeting (27) name
FROM MEETING;
The function seems to compile okay - but when called throws
ORA-06575: Package or function GETMEETING is in an invalid state
Perhaps this will work better for you:
create or replace function
getmeeting(
meeting_id number)
return
varchar
is
meeting_title meeting.title%Type;
begin
select title
into meeting_title
from meeting
where meeting_id = getmeeting.meeting_id;
return meeting_title;
exception
when NO_DATA_FOUND then
return 'UNKNOWN APPOINTMENT';
end;
/
Not syntax checked.
Error being generated by column identifier 'TIME' which is an SQL keyword; triggering a runtime error when executed.
Code unfortunately returns NULL when 'TIME' is removed
There aren't enough facts to know but I would look into some form of circular dependency.
select *
from user_dependencies
where referenced_name = 'GETMEETING'
and referenced_type = 'FUNCTION';
The best way to avoid circular dependencies is to use packages where references to other packages are made in the body only. Avoid standalone function and procedure objects.

Want to insert timestamp through procedure in oracle

I have this procedure
PROCEDURE insertSample
(
return_code_out OUT VARCHAR2,
return_msg_out OUT VARCHAR2,
sample_id_in IN table1.sample_id%TYPE,
name_in IN table1.name%TYPE,
address_in IN table1.address%TYPE
)
IS
BEGIN
return_code_out := '0000';
return_msg_out := 'OK';
INSERT INTO table1
sample_id, name, address)
VALUES
(sample_id_in, name_in, address_in);
EXCEPTION
WHEN OTHERS
THEN
return_code_out := SQLCODE;
return_msg_out := SQLERRM;
END insertSample;
I want to add 4th column in table1 like day_time and add current day timestamp in it.. ho can i do that in this procedure.. thank you
Assuming you you have (or add) a column to the table outside of the procedure, i.e.
ALTER TABLE table1
ADD( insert_timestamp TIMESTAMP );
you could modify your INSERT statement to be
INSERT INTO table1
sample_id, name, address, insert_timestamp)
VALUES
(sample_id_in, name_in, address_in, systimestamp);
In general, however, I would strongly suggest that you not return error codes and error messages from procedures. If you cannot handle the error in your procedure, you should let the exception propagate up to the caller. That is a much more sustainable method of writing code than trying to ensure that every caller to every procedure always correctly checks the return code.
Using Sysdate can provide all sorts of manipulation including the current date, or future and past dates.
http://edwardawebb.com/database-tips/sysdate-determine-start-previous-month-year-oracle-sql
SYSDATE will give you the current data and time.
and if you add the column with a default value you can leave your procedure as it is
ALTER TABLE table1 ADD when_created DATE DEFAULT SYSDATE;