Select from a collection of collections SELECT - CAST - MULTISET - TABLE
Does someone have a suggestion for the SELECT statement which is performing a CAST?
I am on Oracle 10.2.0.1.0. The goal is to take a collection of a nested table and order it by color, then descr, then grown_by, saving it into an ordered collection (v_List2). Am getting error with ORA-22907 Invalid cast to a type that is not a nested table.
CREATE OR REPLACE TYPE ot_fruit
AS OBJECT
descr VARCHAR2(100)
,color VARCHAR2(50)
,grown_by VARCHAR2(50)
CREATE OR REPLACE TYPE tab_fruitList AS TABLE OF ot_fruit;
CREATE OR REPLACE TYPE ot_tab_fruit
AS OBJECT
fruit tab_fruitList
DECLARE
v_List ot_tab_fruit := ot_tab_fruit(tab_fruitList(ot_fruit('apple','red','tree'),
ot_fruit('blueberry','blue','bush')
v_List2 ot_tab_fruit := ot_tab_fruit(tab_fruitList());
BEGIN
SELECT CAST ( MULTISET ( SELECT * FROM TABLE(v_List)
ORDER BY 2, 3, 1
<b> -- This compiles with ORA-22907 error
AS ot_tab_fruit</b>
) INTO v_List2
FROM DUAL;
FOR i IN v_List2.FIRST..v_List2.LAST
LOOP
DBMS_OUTPUT.PUT_LINE('i='||i||' descr='||v_List2(i).fruit.descr ||' color='||
v_List2(i).fruit.color||' grown_by='||v_List2(i).fruit.grown_by);
END LOOP;
END;Thanks, Kate
Message was edited by:
johnsok
This solution, which works perfectly by the way, came from Avi Abrami. I've highlighted the necessary changes to make the SELECT of a collection of objects work properly.
create or replace type OT_FRUIT as object (
DESCR varchar2(100)
,COLOR varchar2(50)
,GROWN_BY varchar2(50)
create or replace type TAB_FRUITLIST as table of OT_FRUIT;
create or replace type OT_TAB_FRUIT as object (
FRUIT TAB_FRUITLIST
DECLARE
v_List ot_tab_fruit := ot_tab_fruit(tab_fruitList(ot_fruit('apple','red','tree'),
ot_fruit('blueberry','blue','bush')
v_List2 ot_tab_fruit := ot_tab_fruit(tab_fruitList());
BEGIN
SELECT CAST ( MULTISET ( SELECT * FROM TABLE(v_List.fruit)
ORDER BY 2, 3, 1
AS tab_fruitlist
) INTO v_List2.fruit
FROM DUAL;
FOR i IN v_List2.fruit.FIRST..v_List2.fruit.LAST
LOOP
DBMS_OUTPUT.PUT_LINE('i='||i||' descr='||v_List2.fruit(i).descr ||' color='||
v_List2.fruit(i).color||' grown_by='||v_List2.fruit(i).grown_by);
END LOOP;
END;
/
Similar Messages
-
Select from tabA & tabB then insert into tabC (these tables ok for form?)
hi everyone,
i have a question for any of you who have a few mins to spare
i have a bunch of tables that i will eventually create a bunch of corrisponding forms for... now i want one of the forms to allow the end user to search for records from two tables,,, select some or all of the rows and then use them in either a delete or update statement....
my table set up is shown below:
SQL> desc BOOK_OUT_INFO
Name Null? Type
BOOK_OUT_ID NUMBER(10)
BOOK_CN VARCHAR2(9)
BOOK_CN_NUM NUMBER(3)
BOOK_ISBN VARCHAR2(9)
BOOK_TITLE VARCHAR2(100)
BOOK_AUTHOR_NAME VARCHAR2(30)
BOOK_STATUS VARCHAR2(4)
EMPLOYEE_ID NUMBER(5)
EMPLOYEE_NAME VARCHAR2(30)
EMPLOYEE_ADDRESS VARCHAR2(100)
EMPLOYEE_PHONE_NUMBER NUMBER(7)
EMPLOYEE_DEPT VARCHAR2(15)
EMPLOYEE_SID NUMBER(5)
SQL> desc BOOK_IN_INFO
Name Null? Type
BOOK_CN VARCHAR2(9)
BOOK_CN_NUM NUMBER(3)
BOOK_ISBN VARCHAR2(9)
BOOK_TITLE VARCHAR2(100)
BOOK_AUTHOR_NAME VARCHAR2(30)
BOOK_STATUS VARCHAR2(4)
SQL> desc SEARCH_INFO
Name Null? Type
BOOK_ISBN NUMBER(9)
BOOK_TITLE VARCHbefAR2(100)
BOOK_AUTHOR_NAME VARCHAR2(30)
ok so now.... i need to allow the user to search both BOOK_IN_INFO and BOOK_OUT_INFO, by BOOK_ISBN then select the records they require for use later and have them inserted into SEARCH_INFO....
before i start making the forms is there anything i need to change in the tables?
thanks all
MUCH APPRECIATED
RMMOI'll give you a hint on how you could solve it.
Let's take BOOK_OUT_INFO table and try to insert some records from this table to SEARCH_INFO table.
Create a button in BOOK_OUT_INFO block, lets call the block BLOCK1. The button would appear for all rows. Write this code in W-B-P trigger of the button.
INSERT INTO TABLE SEARCH_INFO (BOOK_ISBN,BOOK_TITLE,BOOK_AUTHOR_NAME) VALUES (:BLOCK1.BOOK_ISBN, :BLOCK1.BOOK_TITLE, :BLOCK1.AUTHOR_NAME);
COMMIT;
MESSAGE('1 row inserted');MESSAGE('1 row inserted'); -
Error in cast multiset in collections
DECLARE
TYPE emp_dept_rec IS RECORD (
v_sal emp.sal%TYPE,
v_name emp.ename%TYPE,
v_deptname dept.DEPTNAME%type
TYPE emp_dept_tab_type IS TABLE OF emp_dept_rec;
l_emp_dept_tab emp_dept_tab_type;
type emp_tab is table of emp%rowtype;
type l_emp_tab is table of emp%rowtype;
type dept_tab is table of dept%rowtype;
type l_dept_tab is table of dept%rowtype;
cursor e1 is
select * from emp;
cursor d1 is select * from dept;
begin
OPEN e1;
FETCH e1
BULK COLLECT INTO l_emp_tab;
open d1;
FETCH d1
BULK COLLECT INTO l_dept_tab;
select cast(multiset (select em.sal,em,ename ,dep.DEPTNAME
from table(l_emp_tab) em,table(l_dept_tab) dep
where em.deptno=dep.deptno)
as emp_dept_tab_type)
into l_emp_dept_tab ;
end;
it is giving error as
ORA-06550: line 43, column 25:
PL/SQL: ORA-00923: FROM keyword not found where expected
ORA-06550: line 39, column 5:
PL/SQL: SQL Statement ignoredHere is an example.
SQL> CREATE OR REPLACE TYPE emp_rec IS OBJECT (
2 v_sal NUMBER(7,2),
3 v_name VARCHAR2(35),
4 v_empno NUMBER(4),
5 v_deptno NUMBER(2)
6 )
7 ;
8 /
Type created.
SQL> CREATE OR REPLACE TYPE emp_tab IS TABLE OF emp_rec;
2 /
Type created.
SQL> CREATE OR REPLACE TYPE dept_rec IS OBJECT (
2 v_deptno NUMBER,
3 v_dname VARCHAR2(50)
4 )
5 ;
6 /
Type created.
SQL> CREATE OR REPLACE TYPE dept_tab IS TABLE OF dept_rec;
2 /
Type created.
SQL> CREATE OR REPLACE TYPE emp_dept_rec IS
2 OBJECT (
3 v_sal NUMBER,
4 v_name VARCHAR2(35),
5 v_deptname VARCHAR2(30)
6 );
7 /
Type created.
SQL> CREATE OR REPLACE TYPE emp_dept_tab_type IS TABLE OF emp_dept_rec;
2 /
Type created.
SQL> set serverout on
SQL> DECLARE
2 l_emp_dept_tab emp_dept_tab_type; --emp_dept_tab_type declared in database
3 l_emp_tab emp_tab; --emp_tab declared in database
4 l_dept_tab dept_tab; --dept_tab declared in database
5
6 CURSOR e1 IS
7 SELECT emp_rec(sal, ename, empno, deptno) FROM emp; --Note the type casting here
8
9 CURSOR d1 IS
10 SELECT dept_rec(deptno, dname) FROM dept; --Note the type casting here
11 BEGIN
12 OPEN e1;
13
14 FETCH e1 BULK COLLECT
15 INTO l_emp_tab;
16
17 OPEN d1;
18
19 FETCH d1 BULK COLLECT
20 INTO l_dept_tab;
21
22 SELECT CAST(MULTISET (SELECT em.v_sal, em.v_name, dep.v_dname
23 FROM TABLE(l_emp_tab) em, TABLE(l_dept_tab) dep
24 WHERE em.v_deptno = dep.v_deptno) AS emp_dept_tab_type)
25 INTO l_emp_dept_tab
26 FROM DUAL;
27 FOR i IN 1 .. l_emp_dept_tab.COUNT LOOP
28 dbms_output.put_line(l_emp_dept_tab(i)
29 .v_sal || '--' || l_emp_dept_tab(i)
30 .v_name || '--' || l_emp_dept_tab(i).v_deptname);
31 END LOOP;
32
33 END;
34 /
1300--MILLER--ACCOUNTING
5000--KING--ACCOUNTING
2450--CLARK--ACCOUNTING
3000--FORD--RESEARCH
1100--ADAMS--RESEARCH
3000--SCOTT--RESEARCH
2975--JONES--RESEARCH
800--SMITH--RESEARCH
950--JAMES--SALES
1500--TURNER--SALES
2850--BLAKE--SALES
1250--MARTIN--SALES
1250--WARD--SALES
1600--ALLEN--SALES
PL/SQL procedure successfully completed.It is just for educational purpose, because the thing you have achieved by so much programming can be done easily by simple join in the table itself.
user10447332 Newbie
Handle: user10447332
Status Level: Newbie
Registered: Oct 20, 2008
Total Posts: 227
Total Questions: 153 (152 unresolved) >
What a record! and most of the time you don't care to follow/revisit the thread also!. -
Hi, I have a quick question I hope someone can help me with. I'm a student and what I'm looking for should be easy but I can't find any information on it.
Here's my code, it's probably self-explanatory but to clarify I'm trying to get a list of "Captains" in the order of who has the most wins.
The problem is that the database tables have thousands of "Captains" and I'm only supposed to look at 200 specific "Captains" which have their ssn in a specific arraylist and then return the top 80 "Captains" from that selection.
Something like this...
SELECT first 80 E.name, L.ssn, COUNT(L.wins) as Wins
FROM log L, employees E
where type matches "[Captain]"
and E.ssn = L.ssn
and L.ssn IN (...arraylist...) // How do I loop through the arraylist but still return a list of the top 80 winners?
group by E.name, L.ssn
order by Wins desc;
Should I start by taking the list of social security numbers from the arraylist and insert them into a temporary table and then use that temporary table to base my selection on?
For example:
int rows = 0;
PreparedStatement ps = conn.prepareStatement("INSERT INTO TEMP captainsTemp (ssn) VALUES(?)");
Iterator i = myArrayList.iterator();
while (i.hasNext())
// Set the variables
for (int pos = 1; pos <= 63; pos++)
String s = (String)i.next();
ps.setString(pos,s);
// insert a row
rows += ps.execute();
...and then below that I could use
"SELECT * FROM captains
WHERE ssn IN (SELECT * FROM captainTemp)..."
This looks like an ugly solution and I'm not sure if it works, sessionwise..
Everything's written in Java and SQL.
If you have any thoughts on this I would be most grateful.
I should add that this is NOT a school assignment but something I'm trying to figure out for my work...
Many thanks,
sincerely,
JT.hi,
Ignore my previous response. Try out this one. It should help you.
Lets take the example of EMP table (in Oracle's SCOTT Schema). The following is the description of the table.
SQL> desc emp
Name Null? Type
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
SQL> select ename from emp;
ENAME
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLER
Say, the ArrayList contains 3 names CLARK,KING & MILLER. You want to loop through the ArrayList for the ENAME values.
First construct a string like below from the ArrayList.
"ename in 'CLARK' OR ename in 'KING' or ename in 'MILLER'";
Append this string to the normal SELECT Query
Query :
select ename from emp where ename in 'CLARK' OR ename in 'KING' or ename in 'MILLER'
Here you get the desired output & thats pretty fast because you just do it once !!!
SQL> select ename from emp where ename in 'CLARK' OR ename in 'KING' or ename in 'MILLER';
ENAME
CLARK
KING
MILLER
you can extend this to your Query also.
thanks,
Sriram -
hi,
I would like to request your help.
I want to select some data from one table .but the table is dynamic.
It is continuously changing.So what i thought is accept the tablename as
parameter and then do the select from the parameter but the select
statement is not working .
Please advise whether i can select from parameter any data or is there
any other way to do that.
Thanks in advance.
aasr.Post your code. Are you looking for a dynamic where clause?
Here is a short sample of a dynamic select statement.
Enter your table name in the parameter on selection screen. Enter you where clause in the select-option on selection screen.
I used.....
MARA
MTART = 'HALB'
report zrich_0004 .
data: xwhere(30) type c.
data: iwhere(30) type c occurs 0.
data:itab(1000) type c occurs 0 with header line.
parameters: p_table(30) type c.
select-options: s_where for xwhere.
loop at s_where.
clear iwhere.
xwhere = s_where-low.
append xwhere to iwhere.
endloop.
select * up to 100 rows into table itab
from (p_table)
where (iwhere).
loop at itab.
write:/ itab.
endloop.
Regards,
Rich Heilman
Message was edited by: Rich Heilman -
Create Table As Select From Select
Table_2
CREATE TABLE TABLE_2
"ID" VARCHAR2(10),
"EXCL" VARCHAR2(10),
"SEQ" NUMBER(10)
Inserts
INSERT INTO TABLE_2 (ID ,EXCL ,SEQ ) VALUES ('123456' ,'' ,1 );
INSERT INTO TABLE_2 (ID ,EXCL ,SEQ ) VALUES ('123456' ,'EX' ,2 );
INSERT INTO TABLE_2 (ID ,EXCL ,SEQ ) VALUES ('123456' ,'' ,3 );
INSERT INTO TABLE_2 (ID ,EXCL ,SEQ ) VALUES ('321654' ,'' ,4 );
INSERT INTO TABLE_2 (ID ,EXCL ,SEQ ) VALUES ('123798' ,'EX' ,5 );
INSERT INTO TABLE_2 (ID ,EXCL ,SEQ ) VALUES ('123785' ,'' ,6 );
INSERT INTO TABLE_2 (ID ,EXCL ,SEQ ) VALUES ('654787' ,'' ,7 );
INSERT INTO TABLE_2 (ID ,EXCL ,SEQ ) VALUES ('654787' ,'' ,8 );
INSERT INTO TABLE_2 (ID ,EXCL ,SEQ ) VALUES ('654787' ,'' ,9 );
INSERT INTO TABLE_2 (ID ,EXCL ,SEQ ) VALUES ('985217' ,'EX' ,10 );
INSERT INTO TABLE_2 (ID ,EXCL ,SEQ ) VALUES ('985217' ,'' ,11 );
INSERT INTO TABLE_2 (ID ,EXCL ,SEQ ) VALUES ('937158' ,'' ,12 );
INSERT INTO TABLE_2 (ID ,EXCL ,SEQ ) VALUES ('937158' ,'' ,13 );
Select *
ID EXCL SEQ
123456 1
123456 EX 2
123456 3
321654 4
123798 EX 5
123785 6
654787 7
654787 8
654787 9
985217 EX 10
985217 11
937158 12
937158 13
I need to create a table based on for all Records which are EXC not null - but I need to bring through all the records which have the same ID as EXC not null field.
Desired output: -
ID EXCL SEQ
123456 1
123456 EX 2
123456 3
123798 EX 5
985217 EX 10
985217 11
Any ideas folks?Hi,
Here's one way:
CREATE TABLE table_x
AS
SELECT *
FROM table_2
WHERE id IN (
SELECT id
FROM table_2
WHERE excl IS NOT NULL
;By the way, whenever you post formatted text (such as query results) on this site, type these 6 characters:
\(small letters only, inside curly brackets) before and after each section of formatted text, to preserve spacing.
You'll get better answers quicker if people can read:ID EXCL SEQ
123456 1
123456 EX 2
123456 3
123798 EX 5
985217 EX 10
985217 11
(for example) rather than
Deeds_2001 wrote:
ID EXCL SEQ
123456 1
123456 EX 2
123456 3
123798 EX 5
985217 EX 10
985217 11 -
Pipelined function, select from table t1 or t2 depending on user's choise
Hi all,
My need is to select data from table t1 or t2 depending on user's choise using pipelined function. You can find my first guess below. Maybe someone can help me to save some code, I mean getting the same result without writing "PIPE ROW" twice.
FUNCTION fn_indicators (p_datbeg date, p_datend date, p_indicatorid number) return cl_risk_act pipelined IS
v_obj cl_user_type;
BEGIN
case when p_indicatorid = 1 then
FOR e IN (
select trunc(sysdate-1) as adate, 0 as cid, 0 as indicatorid, '0' as code, '0' as indicatorname, 0 as value, 0 as cnt, '0' as cname from dual
LOOP
v_obj.adate := e.adate;
v_obj.cid := e.cid;
v_obj.indicatorid := e.indicatorid;
v_obj.code := e.code;
v_obj.indicatorname := e.indicatorname;
v_obj.value := e.value;
v_obj.cnt := e.cnt;
v_obj.cname := e.cname;
PIPE ROW (v_obj);
END LOOP;
when p_indicatorid = 2 then
FOR e IN (
select trunc(sysdate-2) as adate, 1 as cid, 1 as indicatorid, '1' as code, '1' as indicatorname, 1 as value, 1 as cnt, '1' as cname from dual
LOOP
v_obj.adate := e.adate;
v_obj.cid := e.cid;
v_obj.indicatorid := e.indicatorid;
v_obj.code := e.code;
v_obj.indicatorname := e.indicatorname;
v_obj.value := e.value;
v_obj.cnt := e.cnt;
v_obj.cname := e.cname;
PIPE ROW (v_obj);
END LOOP;
end case;
RETURN;
end;marco wrote:
Justin,
In my real code table real_t1 or pipelinedfn_t2 is joined to other tables, so surely real code is bigger than sample one. It is not wise to keep code twise with only one table name changed. I think about dynamic sql to choose from two tables:
with t1 as
(select * from real_t1
t2 as
(select * from pipelinedfn_t2
t3 as (
[dynamic selection of t1 or t2 depending on user's choise]
select * from t3, t... where...I don't know correct syntax.
Any ideas?
>Justin,
In my real code table real_t1 or pipelinedfn_t2 is joined to other tables, so surely real code is bigger than sample one. It is not wise to keep code twise with only one table name changed. I think about dynamic sql to choose from two tables:
with t1 as
(select * from real_t1
t2 as
(select * from pipelinedfn_t2
t3 as (
[dynamic selection of t1 or t2 depending on user's choise]
select * from t3, t... where...I don't know correct syntax.
Any ideas?
Dynamic code does NOT scale.
Making the tradeoff of smaller code size for reduced performance is not one that I would make. -
Selecting from a view when tables are in more than one schema problem
I give up where am I missing it. Why is the ORA-01031 error being generated on the view u1.bv1. In testing even if I give object access to u3 I still get the same errors:
SQL>
SQL> drop user u1 cascade;
drop user u1 cascade
ERROR at line 1:
ORA-01918: user 'U1' does not exist
SQL> drop user u2 cascade;
drop user u2 cascade
ERROR at line 1:
ORA-01918: user 'U2' does not exist
SQL> drop user u3 cascade;
drop user u3 cascade
ERROR at line 1:
ORA-01918: user 'U3' does not exist
SQL> drop role aRole;
drop role aRole
ERROR at line 1:
ORA-01919: role 'AROLE' does not exist
SQL>
SQL>
SQL> select user from dual;
USER
SYS
1 row selected.
SQL>
SQL> select * from v$version;
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bi
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for Solaris: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
5 rows selected.
SQL>
SQL> create user u1 identified by u1 default tablespace users
2 quota unlimited on users;
User created.
SQL>
SQL> create user u2 identified by u2 default tablespace users
2 quota unlimited on users;
User created.
SQL>
SQL> create role aRole;
Role created.
SQL>
SQL> create user u3 identified by u3 default tablespace users;
User created.
SQL>
SQL> grant aRole to u3;
Grant succeeded.
SQL>
SQL> alter user u3 default role all;
User altered.
SQL>
SQL> grant create session to u3;
Grant succeeded.
SQL>
SQL> create table u1.t1(c1 number);
Table created.
SQL>
SQL> create table u2.t1(c1 number);
Table created.
SQL>
SQL> grant select on u2.t1 to u1;
Grant succeeded.
SQL>
SQL> create or replace view u1.bv1 as
2 select u1.t1.c1 as c1 ,u2.t1.c1 as c2
3 from u1.t1, u2.t1 where u1.t1.c1 = u2.t1.c1;
View created.
SQL>
SQL> grant select on u1.bv1 to aRole;
Grant succeeded.
SQL>
SQL> create or replace view u1.gv1 as select * from u1.t1;
View created.
SQL>
SQL> grant select on u1.gv1 to aRole;
Grant succeeded.
SQL>
SQL> connect u3/u3
Connected.
SQL>
SQL> select * from u1.bv1;
select * from u1.bv1
ERROR at line 1:
ORA-01031: insufficient privileges
SQL>
SQL> select * from u1.gv1;
no rows selected
SQL>
SQL> spool offUser u1 cannot do a grant on a view to other user if the view uses tables from other schemas unless you do a "with grant option" grant.
You need to do the following to make it work:
SQL> grant select on u2.t1 to u1 with grant option;
Grant succeeded.
SQL> -
ORA-00903 when selecting from a view
Hi,
I'm migrating our existing database on 8.1.7 to 9.2 and I keep getting an ORA-00903 error (on Oracle 9.2.0.1.0) when I try to select from a view with nested selects. Here's a very simple example - no rows are returned when I issue the query (its a dummy query, but it illustrates the problem). However, as soon as I turn it into a view, and do a select from the view, I get an Invalid Table Name error. Can anyone suggest why this is happening?
So... This works on its own
SELECT ID
FROM (
SELECT /*+ ORDERED */
t.id
FROM tblmsg t,
tblevent e,
e_workflowstate s,
(SELECT e_taskstatus_id
FROM e_taskstatus s,
e_workflowplntasks ws
WHERE s.e_taskstatus_id = ws.taskid
UNION ALL
SELECT e_taskstatus_id
FROM e_taskstatus s,
e_workflowmantasks ws
WHERE s.e_taskstatus_id = ws.taskid
UNION ALL
SELECT e_taskstatus_id
FROM e_taskstatus s,
e_workflowtrgtasks ws
WHERE s.e_taskstatus_id = ws.taskid) V
WHERE t.id = e.id
AND t.id = v.e_taskstatus_id
AND V.e_taskstatus_id = e.id)
But this doesn't
CREATE OR REPLACE VIEW TESTVIEW AS
SELECT ID
FROM (
SELECT /*+ ORDERED */
t.id
FROM tblmsg t,
tblevent e,
e_workflowstate s,
(SELECT e_taskstatus_id
FROM e_taskstatus s,
e_workflowplntasks ws
WHERE s.e_taskstatus_id = ws.taskid
UNION ALL
SELECT e_taskstatus_id
FROM e_taskstatus s,
e_workflowmantasks ws
WHERE s.e_taskstatus_id = ws.taskid
UNION ALL
SELECT e_taskstatus_id
FROM e_taskstatus s,
e_workflowtrgtasks ws
WHERE s.e_taskstatus_id = ws.taskid) V
WHERE t.id = e.id
AND t.id = v.e_taskstatus_id
AND V.e_taskstatus_id = e.id);
View created
SELECT * FROM TESTVIEW;
ERROR at line 1:
ORA-00903: invalid table name
This works fine on 8.1.7. I haven't tested it on 9.1 though.
Any ideas?
ThanksOne thing to try would be to use SCHEMA.TABLENAME rather than just TABLENAME to refer to the tables.
Its just a shot in the dark, but hopefully it may help.
Martin. -
Hi all!
I am facing a prob while designing a query:
I have a table named ind_mtr with column named "seq_no"....seq_no has a unique index.
I want to arrange the data by seq_no in desc order and from arranged data i want to select first 500 records. For this purpose i supplied the following query:
select seq_no from (select seq_no from ind_mtr where rownum<501)
order by seq_no desc;
But it is giving me wrong results...it selects first 500 records in database and then arranges them in desc order with respect to seq_no.
Can anybody tell me how can i solve this...
RiazSince the 1st 500 rows in descending order on seq_id represents the 500 max values of seq_id, you really want something like the following: SELECT * FROM <tablename> where seq_id BETWEEN ( SELECT MAX( seq_id ) - 500 FROM <tablename> ) AND (SELECT MAX(seq_id) FROM <tablename> ); Or you could put into 2 queries in anonymous block. There is probably a better way. You should look into SQL For Smarties and SQL Puzzles and Answers by Joe Celko. Hope this helps.
-
How do I select from multiple schema's
How do I select from multiple schema's
Tried:
SELECT * FROM schema1.table1, schema2.table2
WHERE schema1.table1.column1 = schema2.table2.column2;
Errored......Thanks
We finally got in touch with our DBA and he said the same thing.
SELECT t1.*,
t2.*
FROM schema1.table1 t1,
schema2.table2 t2
WHERE t1.column1 = t2.column2;
I'll try it afther I finish a task my boss just gave me.
BRB then. -
Select From Multiple CTE?
I was wondering if it is possible to create 2 - 3 CTE statements, and then run ONE select statement that would return the results from all 3 statements? Something like (I am just throwing this together for example, please excuse any syntax issue)
;With CTE AS (Select Count(numSold) As TotalSold, 'Total Sold' As Description
FROM tblSaleInfo
Where sold Is not null)
Select * from cte
;With CTE1 As (Select Count(employeeName) As TotalEmployee, 'Total Employee' As Description
From tblEmployeeInfo
Where Active is not null)
Select * from cte1
--Could you possibly select * from both CTE statements to return one dataset, maybe something like
Select * from cte
UNION ALL
Select * from cte1Yes, but the correct way to do it is
;With CTE AS (Select Count(numSold) As TotalSold, 'Total Sold' As Description
FROM tblSaleInfo
Where sold Is not null),
CTE1 As (Select Count(employeeName) As TotalEmployee, 'Total Employee' As Description
From tblEmployeeInfo
Where Active is not null)
Select * from cte
UNION ALL
Select * from cte1
You want to declare all the CTE's (separated by commas), followed by the query that uses the CTEs.
Tom
Thank you, I was having a brainstorming moment and not in front of SQL Server at the moment so I couldn't test my syntax. Thank you! -
I CAN'T SELECT FROM USERTABLES ON LINUX
IS IT ONE OF THOSE DAYS OR WE NEED TO REPLACE
OUR COFFEE MAKER
S.M.S
nullUSE: SELECT * FROM USER_TABLES;
It is a view, not a table, as all DBA_, ALL_ and USER_ views, created at the end of the installation, so your installation must have completed ok. -
DB Trigger: select from and into current table
I'm running 8.0.5 and facing a table looking like MYTAB(RECID,RECLABEL,..., PARENTRECID, PARENTRECLABEL) where:
. RECID = current record UID (say: employee number)
. RECLABEL = current record label (say: employee name)
. PARENTRECID = RECID of another record in table MYTAB (say: employee ID of the manager of the current employee)
. PARENTRECLABEL = must be made to hold automatically the contents of RECLABEL in the parent record (say: the name of the manager of the current employee)
I know an easy way to get this info would be to use a view like CREATE VIEW MYVIEW AS SELECT A.RECID, A.RECLABEL, A.PARENTRECID, B.RECLABEL "PARENTRECLABEL" FROM MYTAB A, MYTAB B WHERE A.PARENTRECID=B.RECID but for various reasons I would really love to denormalize that info into column PARENTRECLABEL using a database trigger.
Obviously, table MYTAB itself cannot be selected from during the execution of the dB trigger: mutating table error !
I tried:
. to select from a view defined as "SELECT * FROM MYTAB" as suggested in another thread, but this trick didn't seem to be sufficient to lure Oracle (it still says I'm trying to select from a mutating table !)
. to use a package within which a PL/SQL table variable is filled with the list of affected RECID on a FOR EACH ROW basis, and to issue updates in a after statement (thus: not AFTER EACH ROW) AFTER UPDATE trigger - but it's too late already and those updates are apparently not taken into account by the database.
Any other suggestions?
Thx - DidierHere's the code, if the formatting in unpreserved please do let me I'll mail the same.
The Package
CREATE OR REPLACE PACKAGE save_table_package
AS
TYPE saved_test2_type
IS TABLE OF test2%ROWTYPE
INDEX BY BINARY_INTEGER;
saved_test2 saved_test2_type;
saved_test2_num NUMBER:= 1;
PROCEDURE save_test2_row(i_id NUMBER
,i_ename VARCHAR2
,i_mgr_id NUMBER
,i_mgr_ename VARCHAR2
PROCEDURE clear_rows;
END save_table_package;
show err
CREATE OR REPLACE PACKAGE BODY save_table_package AS
PROCEDURE save_test2_row(i_id NUMBER
,i_ename VARCHAR2
,i_mgr_id NUMBER
,i_mgr_ename VARCHAR2
IS
BEGIN
DBMS_OUTPUT.PUT_LINE('Enter pkg.save_test2_row:'| |TO_CHAR(save_table_package.saved_test2_num,'099999'));
save_table_package.saved_test2(saved_test2_num).id := i_id;
save_table_package.saved_test2(saved_test2_num).ename := i_ename;
save_table_package.saved_test2(saved_test2_num).mgr_id := i_mgr_id;
save_table_package.saved_test2(saved_test2_num).mgr_ename := i_mgr_ename;
DBMS_OUTPUT.PUT_LINE('Saved Details Of Row ###:' | |
TO_CHAR(save_table_package.saved_test2_num,'099999') | |'('| |
TO_CHAR(save_table_package.saved_test2(saved_test2_num).id,'099') | |','| |
save_table_package.saved_test2(saved_test2_num).ename | |','| |
save_table_package.saved_test2(saved_test2_num).mgr_id | |','| |
save_table_package.saved_test2(saved_test2_num).mgr_ename | |')'
save_table_package.saved_test2_num := save_table_package.saved_test2_num + 1;
DBMS_OUTPUT.PUT_LINE('Leave pkg.save_test2_row:'| |TO_CHAR(save_table_package.saved_test2_num,'099999'));
END save_test2_row;
PROCEDURE clear_rows IS
BEGIN
DBMS_OUTPUT.PUT_LINE('Enter pkg.clear_rows :'| |TO_CHAR(save_table_package.saved_test2_num,'099999'));
save_table_package.saved_test2_num := 1;
DBMS_OUTPUT.PUT_LINE(' Leave pkg.clear_rows :'| |TO_CHAR(save_table_package.saved_test2_num,'099999'));
END clear_rows;
END save_table_package;
show err
The Triggers
CREATE OR REPLACE TRIGGER trig_bfr_iup_test2_fer
BEFORE INSERT OR UPDATE OF mgr_id ON test2 FOR EACH ROW
DECLARE
BEGIN
DBMS_OUTPUT.PUT_LINE('Enter before insupd:'| |TO_CHAR(save_table_package.saved_test2_num,'099999'));
save_table_package.save_test2_row(:new.id
,:new.ename
,:new.mgr_id
,:new.mgr_ename);
DBMS_OUTPUT.PUT_LINE('Leave before insupd:'| |TO_CHAR(save_table_package.saved_test2_num,'099999'));
END;
show err
CREATE OR REPLACE TRIGGER trig_aft_iup_test2_stm
AFTER INSERT OR UPDATE OF mgr_id ON test2
DECLARE
BEGIN
DBMS_OUTPUT.PUT_LINE('Enter after insupd:'| |TO_CHAR(save_table_package.saved_test2_num,'099999'));
FOR i IN 1 .. save_table_package.saved_test2_num - 1
LOOP
UPDATE test2 eme
SET mgr_ename = (SELECT mgr.ename
FROM test2 mgr
WHERE mgr.id = eme.mgr_id)
WHERE eme.id = save_table_package.saved_test2(i).id
DBMS_OUTPUT.PUT_LINE('MGR_ENAME Set Row#:'| |TO_CHAR(i,'099999'));
END LOOP;
save_table_package.clear_rows;
DBMS_OUTPUT.PUT_LINE('Leave after insupd:'| |TO_CHAR(save_table_package.saved_test2_num,'099999'));
END;
show err
null -
CAST MULTISET...is it supported by BI
In a precedent POst i explained the problems we had with multiple queries to display data as a hierarchical manner, with more than 2 levels
We'd like to explore the way with CAST MULTISET...but before to spend time, we'd like to assure that it is supported by BI Publisher.
As an example ( with one level):
We prepared the Object Types as is :
create or replace TYPE ETAG_T AS OBJECT("@S_NUME_OBJE" NUMBER,
S_NUME_ETAG NUMBER,
C_TYPE_ETAG VARCHAR2(4 CHAR),
L_DESC_ETAG VARCHAR2(50 CHAR),
N_NOMB_PIEC NUMBER,
N_SURF_TOTA NUMBER,
D_MODI DATE,
R_FONC CHAR(5),
R_UTIL CHAR(20),
C_CODE_LANG VARCHAR2(4 CHAR)
create or replace TYPE etaglist_t AS TABLE OF etag_t
create or replace TYPE obje_t as Object (
"@S_NUME_OBJE" NUMBER,
N_NUME_IMME VARCHAR2(5) ,
N_NUME_OBJE VARCHAR2(5) ,
etag_list etaglist_t
Then the data query is (Statement is named Q1):
SELECT
obje_t(s_nume_obje, n_nume_obje, n_nume_imme,
CAST (MULTISET (SELECT S_NUME_OBJE,
S_NUME_ETAG,
C_TYPE_ETAG,
L_DESC_ETAG,
N_NOMB_PIEC,
N_SURF_TOTA,
D_MODI,
R_FONC,
R_UTIL,
C_CODE_LANG
FROM imm.b12_0 eta
WHERE eta.s_nume_obje = obj.s_nume_obje)
AS etaglist_t))
AS "obj"
FROM pointi.b02_v obj
WHERE obj.s_nume_obje= :sNumeObje
As a result , we get only the <obj> element like this :
<LIST_Q1>
<Q1>
<obj/>
</Q1>
</LIST_Q1>
We tried too :
SELECT XMLElement("Object",
obje_t(s_nume_obje, n_nume_obje, n_nume_imme,
CAST (MULTISET (SELECT S_NUME_OBJE,
S_NUME_ETAG,
C_TYPE_ETAG,
L_DESC_ETAG,
N_NOMB_PIEC,
N_SURF_TOTA,
D_MODI,
R_FONC,
R_UTIL,
C_CODE_LANG
FROM imm.b12_0 eta
WHERE eta.s_nume_obje = obj.s_nume_obje)
AS etaglist_t))
AS "obj" ) as "objXML"
FROM pointi.b02_v obj
WHERE obj.s_nume_obje= :sNumeObje
And we get :
<Q1>
<objXML/>
</Q1>
Seems to be not supported isn't it...
LaurentOk thanks,
I already checked your blog before to speak about pl/sql tables ;-)
I think , for maintenance, it will better too , than having a single query with many cast...Multiset , which could have more than 500 lines :-(
But in our case, as we should have kind of hierarchies in the XML (with levels) and collections, i think we should have a mix :
store pl/sql tables in a package, that will be reused from the main query, and certainly continue to use cast multiset to manage theses nested collections.
As you said, i think we could remove the use of Object Types as we do today.
Maybe you are looking for
-
External screen doesn't go into standby when putting MacBook Pro into sleep
I'm using a new MacBook Pro with an external monitor (DELL P2411H) through a Mini Displayport to DVI adapter. When putting the Macbook into sleepmode, the external screen goes into standby but immediately wakes up, then goes back into standby and wak
-
I have a problem, I'd like to gather client info and present that info as my forms application loads but unfortunatley I can't seem to get it to works. I can create a button and place code behind it and gather information that way so I'm confident th
-
Hi, I am using the Watercolour these at the moment, but want to change the colours of the images - is there an easy way to do this? Where are the image files located? Thanks Rich
-
Hi there, Here it goes my first post. Today I have upgraded my windows 8.1. Now, it is asking for a windows key. There is no OEM sticker on the laptop, neither recovery DVD or recovery particion. If I want to reintall windows 8 on factory setting, wh
-
Generating deltas records after a work order status change
Hi We are currently reporting on Work Order status changes in BW. However, the process team has written an ABAP program that allows users to change a Work Order status or an Opeartion status. Any changes using this method do not trigger a delta recor