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?
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)
Thanks in advance!Ashleigh
Try 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.
Tags: Database
Similar Questions
-
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?
-
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;
-
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 -
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
-
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.
-
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
-
Get more value from my table records
Hi Experts,
I need the unique output like VAL result with the biggest FLAGConnected to: Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - Production With the Partitioning, OLAP, Data Mining and Real Application Testing options SQL> WITH T AS 2 ( 3 SELECT 1 ID,'1OIKL' VAL,1 FLAG FROM DUAL UNION ALL 4 SELECT 2,'9IUY',0 FROM DUAL UNION ALL 5 SELECT 3,'9IUK',0 FROM DUAL UNION ALL 6 SELECT 4,'9IUS',0 FROM DUAL UNION ALL 7 SELECT 1,'1OIKL',0 FLAG FROM DUAL 8 )SELECT * FROM T; ID VAL FLAG ---------- ----- ---------- 1 1OIKL 1 2 9IUY 0 3 9IUK 0 4 9IUS 0 1 1OIKL 0 SQL>
Thank youID VAL FLAG ---------- ----- ---------- 1 1OIKL 1 2 9IUY 0 3 9IUK 0 4 9IUS 0 SQL>
Published by: DB-user11904889, March 24, 2010 15:51
Code field included in the values of the tableTry:
SQL> create table T AS 2 SELECT 1 PK, 1 ID,'1OIKL' VAL,1 FLAG FROM DUAL UNION ALL 3 SELECT 2,2,'9IUY',0 FROM DUAL UNION ALL 4 SELECT 3,3,'9IUK',0 FROM DUAL UNION ALL 5 SELECT 4,4,'9IUS',0 FROM DUAL UNION ALL 6 SELECT 5,1,'1OIKL',0 FLAG FROM DUAL; Table created. SQL> select * from t; PK ID VAL FLAG ---------- ---------- ----- ---------- 1 1 1OIKL 1 2 2 9IUY 0 3 3 9IUK 0 4 4 9IUS 0 5 1 1OIKL 0 5 rows selected. SQL> delete from t t1 2 where exists ( select null 3 from t t2 4 where t1.val = t2.val 5 and t1.flag < t2.flag 6 ); 1 row deleted. SQL> select * from t; PK ID VAL FLAG ---------- ---------- ----- ---------- 1 1 1OIKL 1 2 2 9IUY 0 3 3 9IUK 0 4 4 9IUS 0 4 rows selected.
-
How can I update this table with values from another table?
Hello
I have a table 'governed '. I want to replace the values in the column "regies.agent" by the value of the column "regies_personnes.id."
As you can see the tables have a column of common values. IE regies.agent = regies_personnes.nom
Table 'governed ':
Insert into 'authorities' (AGENT) values ('Humberdot Alain");
Insert into 'authorities' (AGENT) values ("Danard Patrick");
Table 'regies_personnes ':
Insert into REGIES_PERSONNES (NAME, ID) values ("Humberdot Alain", 1);
Insert into REGIES_PERSONNES (NAME, ID) values ("Danard Patrick", 2);
Before having it
SQL > select agent of authorities;
Humberdot Alain
Patrick Danard
After the update, the result should be
SQL > select agent of authorities;
1
2
Thank you for your kind reply.You can
update regies r set agent = (select id from regies_persones p where r.agent = p.nom) where exists (select id from regies_persones p where r.agent = p.nom)
-
return the minimum value from a table
Hi all, how can I output the lower value within an array of numbers?
I have this but it does not work, it is always the first number in the table.
function minF(a:Array):Number {var min:Number = [0]; for (var i: Number = 1; i >.) Length; i ++) {if (a [i] > min) {min = a [i];} } else {trace ("buahMin!"); return min}}; }
Thanks to you all.
This is because you have the sign on your conditional loop wrong.
You must make the comparison for all I less than your length of array.
function minF(a:Array):Number {}
var min:Number = [0];
for (var i: Number = 1; i
}
return min;
}
-
How can I search and return several items from the table quickly (like Matlab find)?
Hi, I'm a pretty experienced Labview programmer, and I've always wondered if there is a way to quickly search for pictures on several items that meet the selection criteria, equivalent to the Matlab find command. While my inability to do in Labview has always annoyed me, I now have an application that requires this capability and I need to find a solution.
Is the fastest way I've found to do this in labview to perform comparisons on the data vectors, then use the Boolean value that results from vector to analyze the table using a loop and shift registers. I enclose an extract vi a analysis comparative vi to one of the simple searches that I need to do. On my laptop, this research takes about 600 ms. In comparison, the equivalent in Matlab:
newArray=oldArray(find(oldArray(:,4)./oldArray(:,2)>1.5),;
runs in about 1.2ms. So, the way I am doing this in Labview is only 400 times slower
Needless to say, I need to make these types of research many times, and these additional 599 ms start to add up pretty quickly!
Thanks for your help,
Aaron
Hi Aaron,
the slow part is probably "build group" of the node in the loop.
Try this:
Define an array the same size as the input data (or simply uses a copy of it) and the wire that to the shift register. Keep a counter of lines found in the loop. Use IndexArray and ReplaceArraySubset for the loop to move rows found at the beginning of the table (overwrite the 'bad' lines). After the loop simply ReshapeArray the number of found lines...
Sorry, don't have LV (2009) on hand for editing your snippet.
-
Dear,
1. I have a table
2-build process named (ORDER_PROCESS)select "ROWID", "ORDER_ID", "LOCATION_ID", PLACE_FROM, PLACE_TO, DELIVERY_PRICE from "#OWNER#"."ORDER_LOCATION" WHERE ORDER_ID = :P3_ORDER_ID
3. in the header html page, I put this javascript code to get (place_to, place_from, delivery_price) when I choose selection list location_ifdeclare px01 varchar2(100) := dbms_assert.enquote_literal(apex_application.g_x01); V_PLACE_FROM varchar2(30); V_PLACE_TO varchar2(30); V_DELIVERY_PRICE NUMBER; v_sql varchar2(1000) := 'select get_state_name(place_to) place_to_state,get_state_name(place_from) place_from_state, place_to,DELIVERY_PRICE from LOCATION_SHEET WHERE LOCATION_ID = '; begin v_sql := v_sql || px01; execute immediate v_sql into V_PLACE_FROM,V_PLACE_TO,V_DELIVERY_PRICE; htp.prn(V_PLACE_FROM); htp.prn(V_PLACE_TO); htp.prn(V_DELIVERY_PRICE); exception when others then NULL; end;
4. in the column location_id, I put in the attributes of the element: onchange = "getlocation (this); »<script type="text/javascript"> function getlocation(pThis) { var tid = pThis.id.replace("f04","f05","f06","f07"); ajaxRequest = new htmldb_Get( null,$v('APP_ID'),'APPLICATION_PROCESS=ORDER_PROCESS',0); ajaxRequest.addParam('x01',$v(pThis)); ajaxResponse = ajaxRequest.get(); $s(tid,ajaxResponse ); } </script>
but when I choose to location_id (selection list) not take the values column (place_to, place_from, delivery_price), what is the problem please
AhmedNow check out your page.
Are there errors in your process of OnDemand. Don't know what it should be, I just got rid of the errors.
declare px01 varchar2(100) := dbms_assert.enquote_literal(apex_application.g_x01); V_PLACE_FROM varchar2(30); V_PLACE_TO varchar2(30); V_DELIVERY_PRICE NUMBER; /* v_sql varchar2(1000) := 'select get_state_name(place_to) place_to_state,get_state_name(place_from) place_from_state, place_to_id,DELIVERY_PRICE */ v_sql varchar2(1000) := 'select get_state_name(place_to_id) place_to_state,get_state_name(place_from) place_from_state, DELIVERY_PRICE from LOCATION_SHEET WHERE LOCATION_ID = '; begin v_sql := v_sql || px01; execute immediate v_sql into V_PLACE_FROM,V_PLACE_TO,V_DELIVERY_PRICE; htp.prn(V_PLACE_FROM||':'||V_PLACE_TO||':'||V_DELIVERY_PRICE); exception when others then htp.prn(sqlerrm); --NULL; end;
WHEN OTHER THEN NULL; is to be avoided at any time.
There is no place_to of the column in the LOCATION_SHEET table. Wouldn't be place_to_id? I made this assumption.See you soon,.
-
Insert values from another table
Hello I have a table:
T1
---
ID
user
and another table:
T2
---
intellectual property
Mac
So I modified T1:
ALTER table T1 add ip number (20);
ALTER table T1 add mac number (20);
So I have:
T1
---
ID
user
intellectual property
Mac
T1 and T2 are the same number of lines.
Now, I want to insert the values of IP and mac of T2 in the fields ip address and mac of T1.
IP and mac of T2 fields can be "not null".
How can I do?
Thanks in advanceand what do you think about something like this:
UPDATE t1 SET t1.mac = (SELECT t2.mac FROM t2 WHERE t2.id = t1.id), t1.ip = (SELECT t2.ip FROM t2 WHERE t2.id = t1.id)
Give it a shot... validate the results before stolen.
HTH,
Thierry -
Hello
I have a report that uses apex_item.checkbox (...) to generate the check box. This report correctly displays a check box for each line. The source code that is generated in the html page is:
< input type = "checkbox" name = "f01" value = "202" id = "P1_CHECKBOX" / >
...
...
< input type = "checkbox" name = "f01" value = "220" id = "P1_CHECKBOX" / >
...
...
< input type = "checkbox" name = "f01" value = "210" id = "P1_CHECKBOX" / >
...
...
I want to use the javascript function $v () to get the values of the enabled check box. I thought that the return of this function all the checked values separated by ':' but I noticed that my code alert ($v ('P1_CHECKBOX')); return whenever the value of the first checkbox if it is checked.
It returns '202' if the first box is checked, but nothing, if only the second checkbox is checked and '202' if the box of the first and the second is checked.Hello
first of all, $v, $x and $s are supposed to not work for items on the page, not the columns in a table or manually generated HTML elements.
Secondly, I think that your HTML code is not correct, because each of your boxes has the same ID. But the ID must be unique in the DOM of the browser tree. Thus the different box should actually named P1_CHECKBOX_1... P1_CHECKBOX_3. Just look at what is actually generated for a page element real checkbox. BTW, I think that you should not name the checkbox as part of page elements, because they are not actually page elements. I think that this could be confusing for other developers.
Hope that helps
Patrick
-----------
My Blog: http://www.inside-oracle-apex.com
APEX 4.0 Plug-Ins: http://apex.oracle.com/plugins
Twitter: http://www.twitter.com/patrickwolf
Maybe you are looking for
-
I tried to run mozilla firefox as always double click and it opens this time a pop came out asking me to select a user, I chose 1 on both I feel like I could have selected the wrong one miss me many of my bookmarks and things like that I don't know h
-
A gray transparent screen covers my open window Facebook keeps me to post, but I can still scroll vertically. Reload the page solves the problem for a few minutes before repeating the procedure. Problem with the Facebook Page right now.
-
Satellite M100-222: display panel dithering-> high compression like jpg
I bought Satellite M100-222 2 days. Soon, I saw that solid colors like windows default blue or gray /many of others too / appear strangely - it seems that dirty, grainy, or as strong jpg compression. It is not only my opinion. As much as I know that
-
Satellite P205D-S7438 - where to find FN button software for Win XP
I'm trying to get my keys FN working under XP on my Satellite P205D-S7438, but I can only find versions for other models and other operating systems. Is there an alternative or the version that exists for my model and XP?
-
I started to clear some files in the main C: files then the touchpad did not find the (ordrivers of controllers) and I used a usb mouse. But now this does not work since the only time where that I turned off the computer and then when I turned it on