Return multiple values from a function in a SELECT statement
I hope I've provided enough information here. If not, let me know what I'm missing.I create a view that will combine the information from several tables. Most are pretty simple, but there are a couple of columns in the view that I need to get by running a function within a package. Even if this is quite simple (I have a function named action_date in a package called rp, for example, that I can use to return the date that I need through SOME rp.action_date (sequence_number).
Here is the question: I really need to return several bits of information of the same record (not only action_date, but also action_office, action_value, etc.)-a join of the tables will work not here, as I will explain below. I can, of course, perform a function separate for each statement, but this is obviously inefficient. Within the select statement of the view, however, I don't know how each of the values that I need to get back.
For example, right now, I have:
Table 1:
sequence_number NUMBER (10),
name varchar (30),
...
Table2:
Table1_seq NUMBER (10),
action_seq NUMBER (10),
action_date DATE,
action_office VARCHAR (3),
action_value VARCHAR (60),
...
I can't just simply join Table1 and Table2 because I have to perform processing in order to determine the rows returned matching, I really need to select. If the package opens a cursor and treats each line until it finds the one I need.
The following works but is ineffective since all calls to the package returns the columns of the same record. I don't know how to put all the values that I need in the SELECT statement.
CREATE VIEW all_this_stuff AS
SELECT sequence_number, name,
RP.action_date (sequence_number) action_date,
RP.action_office (sequence_number) action_office,
RP.action_value (sequence_number) action_value
FROM table1
Is there a way to return multiple values in my SELECT statement or I'm going about this all wrong?
Any suggestions?
Thank you very much!
Hello
What you want is a Query of Top - N , what you can do using the ROW_NUMBER analytic function in a subquery, like this:
WITH got_rnum AS
(
SELECT action_seq, action_dt, action_office, action_type, action_value
, ROW_NUMBER () OVER ( ORDER BY action_date
, action_seq
, action_serial
) AS rnum
FROM table2
WHERE action_code = 'AB'
AND action_office LIKE 'E' -- Is this right?
)
SELECT action_seq, action_dt, action_office, action_type, action_value
FROM got_rnum
WHERE rnum = 1
;
As written, this returns a single line (at most).
I suspect you'll actually get a rank for each group , where a group is defined by a value in a table in which you join.
In this case, add a PARTITION BY clause to the ROW_NUMBER function.
If post you a small example of data (CREATE TABLE and INSERT statements), I could show you exactly how.
As I don't have your tables, I'll show you the use of the tables in the scott schema.
This is a view containing data in the scott.dept table and also to scott.emp, but only for the highest employee in each Department (in other words, the employee whose oldest hire date). If there be a tie for the first hire date, while the candidate with the lowest empno is selected.
CREATE OR REPLACE VIEW senior_emp
AS
WITH got_rnum AS
(
SELECT d.deptno
, d.dname
, e.empno
, e.ename
, e.hiredate
, ROW_NUMBER () OVER ( PARTITION BY d.deptno
ORDER BY e.hiredate
, e.empno
) AS rnum
FROM scott.dept d
JOIN scott.emp e ON d.deptno = e.deptno
)
SELECT deptno
, dname
, empno
, ename
, hiredate
FROM got_rnum
WHERE rnum = 1
;
SELECT *
FROM senior_emp
;
Output:
. DEPTNO DNAME EMPNO ENAME HIREDATE
---------- -------------- ---------- ---------- ---------
10 ACCOUNTING 7782 CLARK 09-JUN-81
20 RESEARCH 7369 SMITH 17-DEC-80
30 SALES 7499 ALLEN 20-FEB-81
Moreover, one of the conditions to the query you posted has been
action_office LIKE 'E'
which equals
action_office = 'E'
(AS is always equivalent to = if the string that follows AS does not contain the winning cards.)
Did you mean say that or did you mean something like this:
action_office LIKE 'E%'
Instead?
Tags: Database
Similar Questions
-
passing multiple values from the query in the select statement of the procedure
I am collecting IDS to select education agreement from the date of rental values and then I would pass the result of the query in the statement select to get the result.
data type for the contract id is of type varchar and date of rental there is more than one id to contract for most of the time. Help, please. Thank you
CREATE OR REPLACE PROCEDURE abstract_menu (p_letting IN VARCHAR2, p_results OUT SYS_REFCURSOR) IS v_contractId VARCHAR2 (40); SELECT lcontid INTO v_contractId FROM letprop WHERE datestat IS NOT NULL AND letting = p_letting AND letstat <> 'R'; BEGIN OPEN p_results FOR SELECT DISTINCT SUBSTR (l.letting, 3, 2) || '-' || SUBSTR (l.letting, 5, 2) || '-' || SUBSTR (l.letting, 1, 2) lettingdate, l.lcontid contractid, SUBSTR (q.cprojnum, 1, 10) projectnumber, DECODE (TRIM (MIN (j.route)), NULL, 'N/A', TRIM (MIN (j.route))) routenumber, L.DATESTAT statusdate, L.LETSTAT lettingstatus, (q.cdescr) jobdescription, INITCAP (q.clocat1 || q.clocat2) LOCATION FROM vendor v, vendaddr r, letprop l, planhold p, proposal q, project j, propproj k, bidlet bd WHERE v.vendor = r.vendor AND k.contid = q.contid AND k.pcn = j.pcn AND l.lcontid = k.contid AND p.vendor = v.vendor AND l.letting = p.letting AND l.lcontid IN v_contactid "**************This is where I would like to pass the contract id from the above select statement***********'" AND l.CALL = p.CALL AND r.addrnum = p.billto AND bd.letting = l.letting GROUP BY q.cdescr, q.clocat1, q.clocat2, bd.letting, l.letting, l.lcontid, q.cprojnum, L.LETSTAT, L.DATESTAT ORDER BY lettingdate; end;
user9196150 wrote:
AND l.lcontid IN v_contactid ' * this is where I would like to pass the id of the select statement above contract * ""»CREATE OR REPLACE PROCEDURE abstract_menu (p_letting IN VARCHAR2, p_results OUT SYS_REFCURSOR) IS BEGIN OPEN p_results FOR SELECT DISTINCT SUBSTR (l.letting, 3, 2) || '-' || SUBSTR (l.letting, 5, 2) || '-' || SUBSTR (l.letting, 1, 2) lettingdate, l.lcontid contractid, SUBSTR (q.cprojnum, 1, 10) projectnumber, DECODE (TRIM (MIN (j.route)), NULL, 'N/A', TRIM (MIN (j.route))) routenumber, L.DATESTAT statusdate, L.LETSTAT lettingstatus, (q.cdescr) jobdescription, INITCAP (q.clocat1 || q.clocat2) LOCATION FROM vendor v, vendaddr r, letprop l, planhold p, proposal q, project j, propproj k, bidlet bd WHERE v.vendor = r.vendor AND k.contid = q.contid AND k.pcn = j.pcn AND l.lcontid = k.contid AND p.vendor = v.vendor AND l.letting = p.letting AND l.lcontid IN ( SELECT ll.lcontid FROM letprop ll WHERE ll.datestat IS NOT NULL AND ll.letting = p_letting AND ll.letstat 'R' ) AND l.CALL = p.CALL AND r.addrnum = p.billto AND bd.letting = l.letting GROUP BY q.cdescr, q.clocat1, q.clocat2, bd.letting, l.letting, l.lcontid, q.cprojnum, L.LETSTAT, L.DATESTAT ORDER BY lettingdate; end; /
SY.
-
Return multiple values from a table
Hi there I'm working on a bit of sql that returns values when they exist in a table.
The code I have returns the correct value when there is an entry in the tbl_studentmodules table, as soon as there is more than one entry in this table it does display no line at all: (.)
Can someone tell how to return multiple values?
Thanks in advance!select modulename from tbl_modulefeedback where 1 = (select count(*) from tbl_studentmodules where upper(:APP_USER) = upper(student_id)) and 1 = (select count(*) from tbl_modulefeedback, tbl_studentmodules where tbl_modulefeedback.modulecode = tbl_studentmodules.modulecode)
AshleighTry this:
select modulename from tbl_modulefeedback where 1 <= (select count(*) from tbl_studentmodules where upper(:APP_USER) = upper(student_id)) and 1 <= (select count(*) from tbl_modulefeedback, tbl_studentmodules where tbl_modulefeedback.modulecode = tbl_studentmodules.modulecode)
When you ask questions, please enter CREATE TABLE and INSERT some commands to make it easier to help you.
-
How to return multiple variables from a function
Hello
As seen below, I tried more then return a variable to a function, but I think I have a syntax problem...
function hesapla3 (parameter1, parameter 2): (number & & Boolean) / / PROBLEM IS HERE, I have NEED of TWO VARIABLES to RETURN
{
var yenideger2:Number = new Number();
var bol2:Boolean = new Boolean();
yenideger2 = parameter1 + parameter 2;
if(yenideger2>80) {bol2 = false ;}
Return yenideger2;
return bol2;
trace (yenideger2);
trace (bol2);
}You can do it this way.
You can combine the 2 to 1 as a var
var tekdeger:String=yenideger2.toString()+"-"+bol2.toString();
Return tekdeger;
and then you can split and mount the value returned into variables that you want...
-
I need to return multiple values in function
create or replace function f (p) in varchar2) return varchar2
is
a number (10);
Start
for loop 1 in 1.10
Select instr('yyyyyyyyynnnnnyynny','y',1,i) in the doubles.
end loop;
return a;
end;
my function return a value, but I need to return multiple values
Thanks in advanceI don't know if that's what you want (you should give expected results also)
SQL> create or replace type mytabletype as table of number; 2 / Type created. SQL> create or replace function f (p in varchar2) return myTableType pipelined is 2 a number(10); 3 begin 4 for i in 1..10 loop 5 select instr('yyyyyyyyynnnnnyynny','y',1,i) into a from dual; 6 pipe row (a); 7 end loop; 8 end; 9 / Function created. SQL> select * from table(f('1')); COLUMN_VALUE ------------ 1 2 3 4 5 6 7 8 9 15 10 rows selected. SQL>
Published by: Leo Mannhart on March 22, 2011 11:01
could even be compressed into
create or replace function f (p in varchar2) return myTableType pipelined is begin for i in 1..10 loop pipe row (instr('yyyyyyyyynnnnnyynny','y',1,i)); end loop; end;
-
What subprogramme should we prefer to return multiple values with OUT parameter?
Hello
I worked on the procedures and functions, and aware of their differences. Today I have an interview question that - suppose I don't have return multiple values through a subroutine and I want nor run any what DML within this subprogramme and I want to use this subprogramme in the SQL query. What subprogramme I would prefer - "Operating Mode" or "Function" and why?
In my view, both can be used in this case, but want to see your views and opinions.
Kind regards
Sachin jerbi
In terms of software engineering, if you claim something that you expect to "return" something (a value or values) then you use a function. If you call something to "do" something, and then you use a procedure.
THE settings are not good practices in many cases and should not strictly considered "return" of values. Instead, they are assigned values in the code and generally act as pointers to the original variable/structure that was passed as a parameter (the actual internals of Oracle don't quite do it, but in principle this is what they do).
A return value (or structure) of a function is basically push in the battery to the point that the RETURN statement is issued, and then the code calling the stack appears to assign it to a variable or placeholder, it should go in.
If it seems a little difference between procedures and functions to some people, it is recommended to use functions for obtaining values and procedures to do things, in most cases. It is not just "syntactic sugar", as mentioned above.
-
Return multiple values at once of the listOfValues region
Friends,
I created a (listOfValues) region that has 4 fields/columns
ResponsibilityName, ResponsibilityId, ApplicationName, ApplicationId
I assigned a responsibility field ResponsibilityName of the listOfValues in a page.
It works fine when I select a value in this LOV. It returns a value to the field of 'Responsibility' of the page.
Is it possible to return the other 3 fields so while selecting ResponsibilityName in LOV. for example while you select a value for the field 'Responsibility', can the LOV return ResponsibilityId LOV to the 'ResponsibilityId' field in the page, at the same time?
1 selection can return values 1 to the fields?You can return multiple values in the LOV. Simply create a result to the mapping of the array element Lov to the basic element of page. Once you select a value from the LOV, the element of basic page could get filled in with the value on the table of lov whereby it has been mapped.
Concerning
Sumit -
How to return two values of a function?
Hi all
I have a function which returns a varchar2 value. This value is used in the SQL of a ref cursor
Now I want this function returns two values. I tried to use varray, but could not accomplish the task.
Can you tell me how can I find the two values from a function? and how this function can be called in a SQL query?
Thank you very muchThe query should look like:
SELECT v.col.name AS name , v.col.flag AS flag FROM ( SELECT kk_test_kk(t.somecolumn,t.someothercolumn,42) AS col FROM sometable t ) v;
Note that you need to prefix with the name of view inline ('v' in the example) and the expression of the function ('col' in the example).
-
Plugin to point that returns multiple values
When you create an APEX element that can return multiple values, such as a checkbox, APEX uses the settings of varchar2 p_v01 - p_v200 instead of the p_t01 table settings - p_t200.
I would like to create a plugin item type that can return multiple values in the same way, so that the session recorded for the item state contains a delimited string of values. I realize, I could achieve with Javascript and a hidden item, but I really want to a non-Javascript solution. Is this possible? If this isn't the case, it would be a nice improvement!
Hi Tony,.
Take a look at the level of the parameter p_is_multi_value of the APEX_PLUGIN. GET_INPUT_NAME_FOR_PAGE_ITEM api (http://docs.oracle.com/cd/E37097_01/doc.42/e35127/apex_plugin.htm#AEAPI1254). If you set it to TRUE, you will get one of the p_v01 - p_v200 table settings. It's the same what our box element type calls.
Concerning
Patrick
Member of the APEX development team
My Blog: http://www.inside-oracle-apex.com
APEX Plug-Ins: http://apex.oracle.com/plugins
Twitter: http://www.twitter.com/patrickwolf
-
Impossible to select the value from the search screen (quick select, then) on OFA
Hello
We have a problem with a user who are not able to select any value from the LOV on any page of the Oracle.
1. click the button of LOV
2. search + go
3. Select or quick selection nothing happens (error on the Page appears in the left corner of the homepage)
4. only cancel works.
Any suggestion would be appreciated.
I saw a similar question posted Impossible to select the value from the search screen (quick select, then) on OFA
But unfortunately this is not the answer.
Thank you
Sam
Display of the solution to help others. I tried to connect a SR and suddenly a pop Note ups that helped.
The problem is related to the profile "Self Service Accessibility Features" customer has defined for the user concerned.
Follow these steps:
1 change the value of 'None' profile at the user level.
2 disconnect / connect to the application and testing.
See you soon.
-
Update using a function in the select statement
Hello
Is it possible to do the follwing.
I tabele and a custom function, the custome service will update the column (amount) of the table based on the value of going through the function of the select statement.
SELECT id, stock, where an id = get_update (id);
Now
Get_update function will return the same id I'm passing and update the column amount to a value in the
table a.
When I run the satament selection I have the updated data in the column amount in my first executtion, but if execute the same statement, again, I see the changes reflected. is it possible to get data updates to the first performance himself.
the function is of type PRAGMA AUTONOMOUS_TRANSACTION
ConcerningIndeed a strange requirement and probably not the way to go in a production environment. But anyway is a way to achieve your goal
SQL> create table a as select 12 id, 500 amount from dual union all select 13 id, 600 amount from dual / Table created. SQL> create or replace type a_typ as object (id number, amount number) / Type created. SQL> create or replace function get_id (pid number) return a_typ is pragma autonomous_transaction; l_a_typ a_typ := a_typ (null, null); begin update a set amount = 800 where id = pid returning id, amount into l_a_typ.id, l_a_typ.amount; commit; return l_a_typ; end get_id; / Function created. SQL> select id, d.a_typ.amount amount from a, (select get_id (13) a_typ from dual) d where id = d.a_typ.id / ID AMOUNT ---------- ---------- 13 800 1 row selected.
-
Another REGEXP question: how to extract multiple values from a string
Hi, how can I retrieve multiple values to a string with RegExp with Oracle SQL constructs?
Have looked at the various examples, I cannot understand this.
the following Sql code
Returns CN = A_role.select regexp_substr(sessie."rollen", 'CN=A_role') "A_role" from ( select ' CN=A_role,OU=a_org_unit,OU=another_org_unit,DC=bz,DC=ad,DC=min,DC=local CN=Another_role,OU=some_org_unit,DC=bz,DC=ad,DC=min,DC=local CN=Users,OU=Dep,DC=bz,DC=ad,DC=min,DC=local ' "rollen" from dual ) sessie;
I want him back all of the CN = concatenated values with a semicolon and stripped of the CN = bit.
So: A_role; Another_role; Users .
Any help would be much appreciated. Here I use database 11g 11.2.0.2.0.
Does anyone have an idea how to do this in SQL?
best regards MikeHello
If using Oracle 11 gwith tst as (select 'CN=A_role,OU=a_org_unit,OU=another_org_unit,DC=bz,DC=ad,DC=min,DC=local,' ||'CN=Another_role,OU=some_org_unit,DC=bz,DC=ad,DC=min,DC=local,' ||'CN=Users,OU=Dep,DC=bz,DC=ad,DC=min,DC=local' as rollen from dual ) select listagg(sub, ',') within group (order by rn) as res from (select regexp_substr(rollen, '(CN=)([[:alnum:]_]+)',1,rownum,'i',2) sub, rownum rn from tst connect by level <=length(rollen) ) where sub is not null
-
I called the procedure as belowcreate procedure emp_p ( v_r OUT emp%rowtype ) AS Begin select * into v_r from emp; end;
Errordeclare v_x emp%rowtype; Begin emp_p(v_x); end;
declare
*
ERROR on line 1:
ORA-01422: exact fetch returns more than number of lines
ORA-06512: at "SYS." EMP_P', line 4
ORA-06512: at line 4 level
Thank you
REDAinfant_raj wrote:
create procedure emp_p ( v_r OUT emp%rowtype ) AS Begin select * into v_r from emp; end;
OUT parametere v_r could contain data for one line only, so if your query
Select * into v_r from emp
returns multiple rows, you're bound to get "ORA-01422: exact fetch returns more than number of lines.
You can use SYS_REFCURSOR or Collections instead.
Vivek L
-
How to pass parameter in the function using the select statement?
Hello
I had a problem. I can't pass as parameter to the function by using the select statement. But it can pass as a parameter using the "code". How can I solve this problem?
For example,.
Select * from table (SplitFunction ('HS750020, HS750021')) < < < this work.
but
Select * from table (SplitFunction (select LOT_NO in the TRACER_SEARCH_SCHEDULE where JOB_ID = '36')) < < < do not work.
Thank you for trying to help him. Thank you.Select * from table (SplitFunction (select LOT_NO in the TRACER_SEARCH_SCHEDULE where JOB_ID = '36'))< do="" not="">
Try like this
select * from table(select splitfunction(lot_no) from tracer_search_schedule where job_id='36')
Just make sure that your subquery returns only 1 row.
-
can we write the function in a select statement?
I have function and there Out parameter, so in this case I can write a select statement to my function to retrieve the value?user11195165 wrote:
I have function and there Out parameter, so in this case I can write a select statement to my function to retrieve the value?Never mind that it's bad! for a function to have an OUT parameter. That's not how the functions should be designed and written.
It's like with a knife like a spoon to eat porridge. Of course, it can work in a way. But why? A knife was never designed to be a spoon - and while it may work of porridge to eat, you'll have a hell of a time trying to use it to eat the soup. So, use the right tool.
In other words, a function is the wrong tool for the job. Use a procedure.
Maybe you are looking for
-
Quickly see that only photos not albums?
I noticed that as soon as I put a picture in an album, looking at all the photos he doesn't let me move them to other albums. However, I don't see, to show only the photos that are not yet in the albums in order to select them and move them into alb
-
Satellite Pro A300 - 13 X: Question on the product recovery disks
I recently bought a laptop Satellite Pro A300 - 13 X with Vista Business installed.It came with two product recovery discs to install XP Professional. Given that I decided just to use XP, I followed the procedure for recovery of product and I am now
-
force the scroll bar vertical table
I have a control which is an array of enums. I show only a single element. LabVIEW will allow me to display a horizontal scroll bar. I need the scroll bar to be vertical to match the other controls and LEDs on the front panel. LabVIEW seems hardcoded
-
This should be easy. I would get a timestamp in the simple long format. The timestamp in seconds gives me this in a weird form that I can't get a long. How can I convert to this basic format?
-
Error code 80070BC9 on the installation of KB979916, KB982526, KB971033
, I have this error these three updates of the installation. Tried the steps on this page: http://social.answers.microsoft.com/Forums/en-US/vistawu/thread/09c8d14b-c092-4e53-807e-497990fcd56d/ with the firewall & anti virus disabled, but still the