How to write a recursive select procedure to read hierarchial data?
Hi!
I have a database where two tables are of interest to me.
TABLE1
ITEM_ID
TABLE2
PARENT_ID
CHILD_ID
There is a constrant between the tables so parent_id and child_id must exist in TABLE1 (they are actually same objects as those defined in TABLE1 using PK ITEM_ID)
This table design is used to create tree structures. But I ahve hit a bump in the road on how to create a procedure that give me the entire structure as a recordset.
Basically I just want to execute the procedure and I should be able to see the hierarchy, pergaps by appending pipe characters... like
|Node1
||Node11
|||Node111
||Node12
|||Node121
|Node2
Later in my web app I will create a word like document using this information. But first I just need to list the structure from a specific parent.
Do you perhaps have an idea how to create such a procedure? It should have at least 1 input parameter and that is the start parent. Then it should perhaps work in a recursive manner.
Im very sure this has been done before, but I have not found a good example using the table design as stated above.
kind regards
Henrik
Please read about [url http://download-uk.oracle.com/docs/cd/B10501_01/server.920/a96540/queries4a.htm#2053937]Hierarchical Queries in the manual.
Similar Messages
-
How to write the following SELECT
CARDS Commission
0-20 0
21-25 500
26-31 650
32-36 850
37> 1050
If an PERSON sells 28 cards in a month, commission would be computed as below.
25*500 = 12500
3*650 = 1950
Total commission 14450New solution:
SQL > var cards_sold NUMBER;
SQL > exec :cards_sold := 28;
PL/SQL procedure successfully completed.
SQL >
SQL > WITH COMM_TABLE AS
2 (
3 SELECT 0 AS MIN_CARD, 20 AS MAX_CARD, 0 AS COMMISSION FROM DUAL UNION ALL
4 SELECT 21 AS MIN_CARD, 25 AS MAX_CARD, 500 AS COMMISSION FROM DUAL UNION ALL
5 SELECT 26 AS MIN_CARD, 31 AS MAX_CARD, 650 AS COMMISSION FROM DUAL UNION ALL
6 SELECT 32 AS MIN_CARD, 36 AS MAX_CARD, 850 AS COMMISSION FROM DUAL UNION ALL
7 SELECT 37 AS MIN_CARD, NULL AS MAX_CARD, 1000 AS COMMISSION FROM DUAL
8 )
9 SELECT LEAD(PART_COMMISSION,1) OVER (ORDER BY MAX_CARD DESC)
10 + (:cards_sold-LEAD(MAX_CARD,1) OVER (ORDER BY MAX_CARD DESC))*COMMISSION AS COMMISSION
11 FROM
12 (
13 SELECT COMM_TABLE.*
14 , COMM_TABLE.COMMISSION*COMM_TABLE.MAX_CARD AS PART_COMMISSION
15 FROM COMM_TABLE
16 WHERE MIN_CARD <= :cards_sold
17 ORDER BY MIN_CARD DESC
18 )
19 WHERE ROWNUM <= 2;
COMMISSION
14450Explanation:
/* According to your requirements commission is calculated in two parts.
* The first part is equal to the COMMISSION value for the RANGE that the :cards_sold exceeds (21-25 in this case)
* multiplied by the MAX_CARDs value in that range. This is indicated by (A) below.
* The second part is the COMMISSION in the range that the :cards_sold is included in multipled by the
* difference of the :cards_sold and the MAX_CARD values for the RANGE that the :cards_sold exceeds (21-25 in this case).
* This is indicated by (B) below.
SELECT LEAD(PART_COMMISSION,1) OVER (ORDER BY MAX_CARD DESC) /* (A) */
+ (:cards_sold-LEAD(MAX_CARD,1) OVER (ORDER BY MAX_CARD DESC))*COMMISSION /* (B) */ AS COMMISSION
FROM
SELECT COMM_TABLE.*
, COMM_TABLE.COMMISSION*COMM_TABLE.MAX_CARD AS PART_COMMISSION /* (A) */
FROM COMM_TABLE
WHERE MIN_CARD <= :cards_sold /* We are only interested in the entries where the # of cards sold is greater than the minimum */
ORDER BY MIN_CARD DESC
WHERE ROWNUM <= 2; /* Only need one commission value */ -
How to write a routine in infopack to restrict a date range !!!!
Hi
I want to load the data for a speicific date range ,i.e , i want to load the data between created on some date range which is not in between the same range for changed on...so i want to restrict that date range for changed on....Please le tme know the routine logic to restrict a date range....
regards...How to ... Write Infopackage selection Routine
1.Create an Infopackage 2. Go to selections tab and choose Type: 6 u2013 ABAP Routine.You can see following available options(F4 Help).
3. Give disruption, and hit enter, now you will move to following screen. 4. Write Code between begin of Routine and End of Routine.
5. See below sample code to select date range from Previous 6 days to Current date.
6. L_T_Range table is of Type structure RSSDLRANGE.
a. RSSDLRANGE contains SIGN, OPTION, LOW, HIGH
We need to populate these fields to pass range dynamically.
Sample Code:
***$$ begin of routine - insert your code only below this line -
Data: l_idx like sy-tabix.
Data: date_low like sy-datum.
Date_low = sy-datum u2013 6.u201D(To get 6 days back).
read table l_t_range with key
fieldname = 'CRDAT'.
l_idx = sy-tabix.
Pass Range values to L_T_Range Table.
Move date_low to L_T_Range -Low.
Move sy-datum to L_T_Range -High.
L_T_Range -Sign = u2018Iu2019. *****(Here: I u2013 Include, E u2013 Exclude)
L_T_Range -Option = u2018BTu2019.****( Here: BT u2013 Between )
modify l_t_range index l_idx.
p_subrc = 0.
**$$ end of routine - insert your code only before this line -
7. Syntax check and Save.
Hope it Helps
Srini -
How to call a PL/SQL procedure from a xml Data Template
We have a requirement in which we need to call a pl/sql package.(dot)procedure from a Data Template of XML Publisher.
we have registered a Data Template & a RTF Template in the XML Publisher Responsibility in the Oracle 11.5.10 instance(Front End).
In the Data Query part of the Data Template , we have to get the data from a Custom View.
This view needs to be populated by a PL/SQL procedure.And this procedure needs to be called from this Data Template only.
Can anybody suggest the solution.
Thanks,
SachinCall the procecure in the After Parameter Form trigger, which can be scripted in the Data Template.
BTW, there is a specialized XML Publisher forum:
BI Publisher -
How to let the user select from more then 1 data templet file to fillin a field
help i have a form the has 5 free form text fields that i need to select from a list of texts to fill it in and then allow the user to add to the data.
thanksthanks here are 2 strings
COPD continue O2 by NC/Albuterol MDI/ Atrovent MDI/ Duonebs/ Albuterol MDI/ Prednisone taper/ IV Solumedrol/ Spiva/ Advair diskus/ oximetry prn/ PT/ OT/ Coreg
CAD/ANGINA cont. O2 by NC?baby ASA/ ASA/ Lopressor/Atenolol/ Lisinopril/ Low fat diet/ PT/OT/ sublingual nitroglycerin prn/ Lasix/ Lipitor/ Zocor/ Mevacor
i would like the user to be able to select from inside of the field when it is blank (fisrt time entering data)
thanks again -
Newbie: how to create a dynamic select list of the current date + 5 years
Hi - I'm a JSP newbie (coming from a PHP background), and am trying to create a select list that contains the current year, plus 5 years. So, the list when generated (today), would display:
2007
2008
2009
2010
2011
2012
Obviously, the code would need to generate inside the option tags, as well as in the value property of the option tag.function addit(){
var val = 2012; //you can take this from js date object and strip it off
for (var i=2007; i <= val;++i){
addOption(document.getElementById("mycombo"), i, i);
function addOption(selectbox,text,value)
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
selectbox.options.add(optn);
}without hardcoding:
function addit(){
var dd = new Date();
var yearnow = dd.getFullYear();
for (var i=yearnow; i <= yearnow + 5 ;++i){
addOption(document.getElementById("mycombo"), i, i);
function addOption(selectbox,text,value)
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
selectbox.options.add(optn);
in your jsp:
<body onload="addit();">
<form>
<select name="mycombo">
<option> Select year</option>
</select>
</form>
</body>
Message was edited by:
skp71
Message was edited by:
skp71 -
How to write select query for this
Hi,
I had a html form and in the for i had drop down box and it needs to select multiple values from the drop down box. When i select multiple values then i have to write the SQL select statement query .
When i try to write the select statement and trying to run i am getting error.
select * from Table
where emo_no = '1,2,3'
this is how i write query please suggest me how to write query for selecting multiple values from the drop down box.
Thanksselect * from Table
where emo_no in ( 1,2,3)
for integer values
select * from Table
where emo_no in ('1','2','3')
for characters
If we talk about large scale applications that may have millions of records, I would suggest this.
declare @t table (v int)
insert into t (v) values (1)
insert into t (v) valves (2)
insert into t (v) values (3)
select *
from table
inner join @t t on table.emo_no = t.v
Using "in" for a where clause is not so bad for filtering on a few values, but if you are filtering a lot of rows and a lot of values (emo_no) the performance degrades quickly for some reasons beyond the scope of this.
This is just one solution, I'll through this out as well, instead of an in memory (@t) table, doing a disk based temp table (#t) and creating an index on the column "v".
create table #t (v int)
insert into #t (v) values (1)
insert into #t (v) valves (2)
insert into #t (v) values (3)
create index ix_t on #t (v)
select *
from table
inner join #t t on table.emo_no = t.v
drop table #t
Pardon any syntax errors and careful using a drop statement.
Sometimes in memory tables work better than disk temp tables, it takes some testing and trial and error depending on your datasets to determine the best solution.
Probably too much info ;-)
Byron Mann
[email protected]
[email protected]
Software Architect
hosting.com | hostmysite.com
http://www.hostmysite.com/?utm_source=bb -
How to make select list as read only
Hi All.
when i m loading a page ...how to make value of select list as read only ..
i tried doing this ....
i added readonly attribute to the select list's element attibute ...it is showing as read only field by darkening the select list field but ... the select list is editable even now... its allowing to choose from drag and drop ...
Thanks & Regard ,
NandiniI have the same requirment..
I have a select list on a editable form and i want to make that one read only.
When I use read only property it doent do anything. (But this is working for text items well).
When I use "disabled' option it works fine. But then that value does not post when use POST the form. So making select list disabled is not a solution.
Any Idea..
Thanks
mc -
How to write this java code in jsp using jstl tags?
Can anybody help me on this?
I dont know how to check the containsKey using jstl tags?
<%
LinkedHashMap yearMap = (LinkedHashMap)request.getAttribute("yearMap");
TreeSet nocSet = (TreeSet)request.getAttribute("nocSet");
Iterator yearMapIt = yearMap.keySet().iterator();
while(yearMapIt.hasNext())
int yearValue = (Integer)yearMapIt.next();
%>
<tr>
<td><%=yearValue%></td>
<%
LinkedHashMap monthMap = (LinkedHashMap)yearMap.get(yearValue);
Iterator nocSetIt = nocSet.iterator();
while(nocSetIt.hasNext())
String nCase=(String)nocSetIt.next();
if(monthMap.containsKey(nCase))
String count = (String)monthMap.get(nCase);
%>
<td> <%= count %> </td>
<% }
else
%>
<td> 0 </td>
<%
%>
</tr>
<% } %>Edited by: avn_venki on Feb 18, 2008 11:54 PM<c:forEach var="yearMap" items="${requestScope.yearMap}">
<th> <c:out value="${yearMap.key}"/> </th>
<bean:define id="monthMap" value="${yearMap.value}"/>
<c:forEach var="nocSet" items="${nocSet}">
then how to write containsKey using tags?? -
How to write and read Xml file from database if possible?
Hi all,
I need to read the .Xml file when receives from Source systems and write the data into the table as well as write in the.xml file thru reading the data from table as per the client needs. I am stranger to this area. Since, please provide some examples how to approach the same.
Thanks in advance !!
Regards.
Vissu.....The XML DB forum is better suited to your question.
It also has a FAQ which details how to read and shred XML into tables as well as other common XML based questions...
XML DB FAQ -
How do I change the default from"Mark message as read by date" to "Mark message as read"?
When I migrated from Windows XP to Windows 7 I downloaded & installed a new version of Thunderbird (v4.10.2) and retrieved my Mail files to the new profile. Everything seems to be working properly except that whenever I close an open message I get the "Mark messages as read by date" popup which has the current date but wants me to specify a starting date. This didn't happen in my previous installations (I've used Thunderbird for years). Options / Advanced / Reading & Display has "Automatically mark messages as read" checked and "Immediately on display" set, but I haven't found anything that lets me choose "Mark as read" rather than "Mark as read by date". How do I turn the "by date" off?
Thanks --'Mark Message as read by Date'
This can be invoked by right click on email in list and select 'Mark' > 'As read by date' to invoke the 'Mark Message as read by Date' pop up window.
If you are reading an email in either the Message Pane or in a new tab and press the 'C' key, this will open the 'Mark Message as read by Date' pop up window.
So can you tell us exactly how you are ''closing'' a previously unread message.
Do you select an email in the list so that it displays in the Message Pane? Then you do not need to close it because this will auto happen when you select another email in the list.
OR do you double click to open an email in a tab. Then how do you close it? There are some options on how to do this:
# Do you click on the small 'x' located in the tab?
# do you use 'Ctrl'+'W' ?
# Do you right click on tab to show drop down options and then press 'C' key?
I notice that there is a similar action - pressing of 'C' key.
If you do not right click to get drop down, but instead just use 'C' key, you will invoke the 'Mark Message as read by Date' window.
Can you check what it is you are doing -
Hi,
Iam new to the xml,
can u please anyone help me how to write procedure to load the data into a table using xml as input parameter to a procedure and xml file is as shown below which is input to me.
<?xml version="1.0"?>
<DiseaseCodes>
<Entity><dcode>0</dcode><ddesc>(I87)Other disorders of veins - postphlebitic syndrome</ddesc><claimid>34543></claimid><reauthflag>0</reauthflag></Entity>
<Entity><dcode>0</dcode><ddesc>(J04)Acute laryngitis and tracheitis</ddesc><claimid>34543></claimid><reauthflag>0</reauthflag></Entity>
<Entity><dcode>0</dcode><ddesc>(J17*)Pneumonia in other diseases - whooping cough</ddesc><claimid>34543></claimid><reauthflag>0</reauthflag></Entity>
</DiseaseCodes>.
Regards,
vikram.here is the your XML parse in 11g :
select *
from xmltable('//Entity' passing xmltype
'<?xml version="1.0"?>
<DiseaseCodes>
<Entity><dcode>0</dcode><ddesc>(I87)Other disorders of veins - postphlebitic syndrome</ddesc><claimid>34543></claimid><reauthflag>0</reauthflag></Entity>
<Entity><dcode>0</dcode><ddesc>(J04)Acute laryngitis and tracheitis</ddesc><claimid>34543></claimid><reauthflag>0</reauthflag></Entity>
<Entity><dcode>0</dcode><ddesc>(J17*)Pneumonia in other diseases - whooping cough</ddesc><claimid>34543></claimid><reauthflag>0</reauthflag></Entity>
</DiseaseCodes>
') columns
"dcode" varchar2(4000) path '/Entity/dcode',
"ddesc" varchar2(4000) path '/Entity/ddesc',
"reauthflag" varchar2(4000) path '/Entity/reauthflag'
dcode ddesc reauthflag
0 (I87)Other disorders of veins - postphlebitic syndrome 0
0 (J04)Acute laryngitis and tracheitis 0
0 (J17*)Pneumonia in other diseases - whooping cough 0
SQL>
Using this parser you can create procedure as
SQL> create or replace procedure myXMLParse(x clob) as
2 begin
3 insert into MyXmlTable
4 select *
5 from xmltable('//Entity' passing xmltype(x) columns "dcode"
6 varchar2(4000) path '/Entity/dcode',
7 "ddesc" varchar2(4000) path '/Entity/ddesc',
8 "reauthflag" varchar2(4000) path '/Entity/reauthflag');
9 commit;
10 end;
11
12 /
Procedure created
SQL>
SQL>
SQL> exec myXMLParse('<?xml version="1.0"?><DiseaseCodes><Entity><dcode>0</dcode><ddesc>(I87)Other disorders of veins - postphlebitic syndrome</ddesc><claimid>34543></claimid><reauthflag>0</reauthflag></Entity><Entity><dcode>0</dcode><ddesc>(J04)Acute laryngitis and tracheitis</ddesc><claimid>34543></claimid><reauthflag>0</reauthflag></Entity><Entity><dcode>0</dcode><ddesc>(J17*)Pneumonia in other diseases - whooping cough</ddesc><claimid>34543></claimid><reauthflag>0</reauthflag></Entity></DiseaseCodes>');
PL/SQL procedure successfully completed
SQL> select * from MYXMLTABLE;
dcode ddesc reauthflag
0 (I87)Other disorders of veins - postphlebitic syndrome 0
0 (J04)Acute laryngitis and tracheitis 0
0 (J17*)Pneumonia in other diseases - whooping cough 0
SQL>
SQL>
Ramin Hashimzade -
How to write a procedure for update the table
Hi all
can any body please tell me how to write a procedure to update the table......
I have a table with about 10,000 records...........Now I have add a new column and I want to add values for that like
registration Code Creidits
13213 BBA
1232 MCS
I had add the creidit now i want to update the table.........the new value want to get by SQL like
Creidit = select creidit from othere_table...........
Hope u can understand my problem
Thanks in advance
Regards
Shayan
[email protected]Please try the following --
update Program_reg a
set TotalCreidit = ( select tot_cr <Accroding to your logic>
from Program_reg b
where a.Registration = b.Registration
and a.Enrollment = b.Enrollment
and a.code = b.code
where a.Registration in ( select distinct Registration
from Program_reg );
N.B.: Not Tested....
Regards.
Satyaki De. -
How do write a stored Procedure in PL/SQL
Hi,
I am new to this forum. I am creating a webpage using Gridview in Visual Studio 2005.
How to write a Stored procedure in Oracle using PL/SQL?
I want to write a SP for the following query :
"Select S_No, Task_ID,Last_Act_Dt,Last_Act_Tm,Status from E002 Order by S_No"
Please help.In Oracle, you normally wouldn't create a stored procedure that just does a query. Depending on what you're attempting to do on the client side, and the client libraries involved, you may be able to write a stored procedure that returns a REF CURSOR and call that. This will put some limitations on what the client can do with the results, for example. The data would be read-only and scrolling back and forth in the result set would potentially rather slow and rather expensive in terms of client memory. That makes this generally a somewhat unattractive option.
You could create a view in Oracle and just query the view in your application code. If you needed to change the query, you could then just change the view.
Justin -
How to write a procedure to run the call the custom package from backend
Hi All
Oracle 10g
Oracle Apps R12
I am working with oracle order management here we have a customize Package called (Pick Release).Due to some problem we have running this concurrent program by manually giving Route_id as parameter. The route_id is taken from the route Table. By using this query
select distinct route_id from route@DB_LINK_APPS_TO_ROADSHOW
where trunc(route_date) = trunc (sysdate+2).
so daily we have nearly 42 routes and we are running this concurrent program manually nearly times.
so now how to write a procedure for this
Step 1 Getting the route from route table.( By cursor we can get the route_id Accordingly)
Step 2 How to trigger the custom package from back end and execute accordingly to that output of the cursor(route_id)
If the cursor get 40 routes is it then the concurrent program runs 40 times according to that route_id.
can some could provide the steps to do this
Thanks & Regards
Srikkanth.MThis is about 4 or 5 lines of PL/SQL and the name of the custom package is not provided.
If you request someone in this forum to do your work for free -because obviously you didn't even try to write it, which must be considered abusing this forum- you must at least provide sufficient info so someone can do it.
And no, I won't do it for you.
Sybrand Bakker
Senior Oracle DBA
Maybe you are looking for
-
I loved Safari and was using it getting to know it...then messed it up now the only thing that shows when I open it is some bookmarks.. I cannot acccess the safari icon logo...or drop down boxes to change anything...and I cannot open more than one pa
-
Page Layout mode: canvas colour
Is it possible to change the canvas colour in Page Layout mode? Andy
-
WLC 5508 AP SSO FUS Upgrade with different Versions
Hi everybody, I've got two 5508 configured as AP SSO and I want to upgrade the FUS to 1.9.0.0 The Image running is 7.6.110.0 On the active controller the FUS Version is 1.3 On the HA-SKU controller the FUS Version is 1.7 Now, can I upgrade the FUS to
-
Hi Experts, For the requirements gather workshops for in a full cycle project, what does the workshops covers. Is it just the As-Process discussed or as a consultants, what is that we need to do or prepare for the workshops? Appreciate if anbody can
-
After I updated my iPhone4 into iOS6, all my applications crash on launch. I tried restoring it but same thing happened