return used with recursive with type object tree

BANNER
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
PL/SQL Release 11.2.0.1.0 - Production
CORE     11.2.0.1.0     Production
TNS for 32-bit Windows: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
There was just a post called employee return tree with the object type, and there were several responses.
I tried to wrap my brain around the recursive with and I was wondering how to implement it for this problem

Basically, the post was something like that.
CREATE TABLE employees
AS
   SELECT 1 mgr_id, 2 emp_id FROM DUAL
   UNION
   SELECT 2, 3 FROM DUAL
   UNION 
   SELECT 3, 4 FROM DUAL
   UNION
   SELECT 4, 5 FROM DUAL;
and he wanted to
MGR_ID     GET_EMPS(MGR_ID)
1     2,3,4,5
2     3,4,5
3     4,5
4     5
as mentioned, it has several solutions were however I was and wrote a recursive function
to get the answer. that seems to work
CREATE OR REPLACE FUNCTION get_emps (p_mgr employees.mgr_id%TYPE)
   RETURN VARCHAR2
IS
   v_mgr    employees.mgr_id%TYPE;
   v_emp    employees.emp_id%type;
   v_emps   VARCHAR2 (200);
BEGIN
   v_mgr := p_mgr;
   LOOP
      SELECT emp_id
        INTO  v_emp
        FROM employees
       WHERE mgr_id = v_mgr;

      v_emps := v_emps||','||v_emp;
      v_mgr := v_emp;
   END LOOP;
  exception when no_data_found then
  return  substr(v_emps,2);
    END;
now, I would like to convert a recursive with my function, but I can't quite understand.

Here's what I have so far but I'm still not close.
WITH emp1 
 (mgr_id, emp_id, emp_ids)
 AS  
 ( SELECT mgr_id  , to_char( emp_id), to_char(emp_id)  from employees  WHERE mgr_id = 1
 UNION /* my internet filter will not alllow me to post this statement */ALL
   SELECT emp2.mgr_id,  to_char(emp2.emp_id), to_char(emp1.emp_id)||','||to_char(emp2.emp_id)
   FROM employees emp2 JOIN emp1 ON (emp2.mgr_id = emp1.emp_id)
 )
 select * from emp1
Sorry for the comment here we have a weird filter on our internet that does not allow me to view the word a union |

Hello

You're so close! You just need a character more:

WITH emp1
 (mgr_id, emp_id, emp_ids)
 AS
 (
     SELECT  mgr_id
     ,     TO_CHAR (emp_id)
     ,     TO_CHAR (emp_id)
     FROM     employees
     WHERE     mgr_id        = 1
    UNION ALL
         SELECT  emp2.mgr_id
     ,     TO_CHAR (emp2.emp_id)
     ,     TO_CHAR (emp1.emp_ids) || ',' || TO_CHAR (emp2.emp_id)
--                        ^
--                        s added above
     FROM     employees emp2
     JOIN             emp1 ON (emp2.mgr_id = emp1.emp_id)
 )
SELECT  *
FROM      emp1
;

Output:

    MGR_ID EMP_ID     EMP_IDS
---------- ---------- --------------------
         1 2          2
         2 3          2,3
         3 4          2,3,4
         4 5          2,3,4,5

Tags: Database

Similar Questions

  • Function returning the tree used with the object type

    Hi guys!

    I'm fighting for some time now of a function that should return the tree structure of the employees. Let me clarify...
    I have a table say Manager and employees (two columns for an example)
    MANAGER_ID
    EMPLOYEE_ID

    Example:

    MAN_ID - 1; EMPLOYE_ID - 2;
    MAN_ID - 2; EMPLOYE_ID - 3;
    MAN_ID - 2; EMPLOYE_ID - 4;
    MAN_ID - 4; EMPLOYE_ID - 5

    The purpose of my function is back for a specified MAN_ID everyone in the tree...

    Example:

    RETURN_TREE (1) return {2,3,4,5};
    RETURN_TREE (2) returns {3,4,5};
    RETURN_TREE (4) returns {5};

    How to get there?

    With respect,

    PsmakR

    And use BULK COLLECT or the COLLECT function to build the collection:

    create type emp_table as table of number(6);
    /
    
    DECLARE
     v_emp_list emp_table;
    BEGIN
     select cast(collect(employee_id) as emp_table)
     into v_emp_list
     from employees
     connect by prior employee_id = manager_id
     start with manager_id = :p_man_id
    
     -- or
     /*
     select employee_id
     bulk collect into v_emp_list
     from employees
     connect by prior employee_id = manager_id
     start with manager_id = :p_man_id
     */
    END;
    /
    
  • 'Function SQL Query return' using with Flash graphics

    I created a pl/sql function that returns a SQL query in a varchar2 of this form:

    Select the null link
    -value > < x value
    < value y of Series1 > series 1 label
    < value Series2 y > 2 series label
    < value of y Series3 > series 3 Label
    ...
    ...
    from tablea a
    Join tableb b
    On a.col = b.col
    order of < x value >

    If I call the function of a graphic Flash box series SQL with the Type of Source query "SQL query" value that returns the function like this:

    return functionname (to_date ('30-sep-2010, ' mon-dd-yyyy ""))

    It parses correctly and the page is saved; However, when I run the page I get no output - error messages or another indication of a problem.

    Now, if I call the function in a SQL client, capture the result of the query SQL using the dbms_output and paste that into the box graphic Flash series SQL - change the Source Type of SQL query query - and save the page it works fine when I run it and returns a chart series flash.

    Can anyone suggest either;

    1. what I might have missed or done wrong?
    2. a way to effectively diagnose the problem...

    I tried to use the debugger Apex - what is very nice, indeed - but it provides no information on what might be my problem. I tried to write my own my function debug messages using the apex_debug_message package - nothing...

    Thank you
    Eric

    Hi Eric,.

    Try to pull the source like this:

    begin
       return functionname(to_date('30-sep-2010', 'dd-mon-yyyy'));
    end;
    

    This works very well for me, and if I take the begin-end and the final semicolon from the return statement I get the same behavior as you.

    He doesn't mention in aid of the source (only during the wizard if) this source type must be expressed in this way, but I agree, it would be useful that the tool would validate to this format meets it "Query SQL that returns the function" or give some sort of indication of the sentence. In any case, this should help you will again.

    Hope this helps,
    John

    If you find this information useful, please do not forget to mark the 'useful' or 'correct' post so that others benefit as well.

  • I need to create a curved with type text box that heal and return to the next line in the form of curved text. How do I do that?

    I need to create a curved with type text box that heal and return to the next line in the form of curved text. How do I do that?

    1. Draw a set of concentric half circles also spaced, stacked so that the outermost is the most remote.
    2. Use the text tool to add text to the outermost path, thus creating a PathType object.
    3. Select all the paths. Type > threaded text > create.
    4. Text tool: click at the end of the text in the object of PathType. Continue typing.

    > If I do that it will not type more.

    A Warp effect is a direct effect. When it is applied to the type of objects, they are still editable text. The chain will deform, however, the text also, not only the paths to which it is attached. Sometimes you want that, sometimes you don't have.

    JET

  • Choose a view with nested object types

    I use 11.2 DB. I created 2 types of objects. The contact_t has been integrated into the student_t object and I created a view with the nested object.

    How do you choose the values of the object contact_t type in the view SQL? I want to see the values of the contact_t object in speerate areas (ie. contact_name, city, Department...)

    Thank you

    CREATE or REPLACE TYPE contact_t () AS OBJECT

    DCID NUMBER (10),

    Contact_Name VARCHAR2 (50).

    City VARCHAR2 (50).

    State VARCHAR2 (10),

    zip VARCHAR2 (10),

    e-mail VARCHAR2 (100))

    CREATE or REPLACE TYPE students_t () AS OBJECT

    DCID NUMBER (10),

    ID NUMBER (10),

    last_name VARCHAR2 (50).

    first name VARCHAR2 (50).

    middle_name VARCHAR2 (50).

    contact contact_t)


    VIEW to CREATE or REPLACE students_t students_view

    WITH the OBJECT IDENTIFIER (dcid) AS

    Select decided,

    ID,

    last_name,

    first name,

    middle_name,

    contact_t (dcid, contact_name, city, state, zip, email)

    FROM MyTable

    What exactly is the problem? Are you an error? An ORA-01730 by chance?

    Use the default STUDENTS_T constructor in the query from the view:

    VIEW to CREATE or REPLACE students_t students_view

    WITH the OBJECT IDENTIFIER (dcid) AS

    Select (students_t)

    DECIDED

    id

    last_name

    first name

    middle_name

    , contact_t (dcid, contact_name, city, state, zip, email)

    )

    FROM MyTable;

    Then, for example:

    SQL > select t.id

    2, t.contact.contact_name

    3, t.contact.city

    4 t students_view;

    ID CONTACT. CONTACT CONTACT_NAME. CITY

    ----------- -------------------------------------------------- --------------------------------------------------

    1 TEST                                               LA

  • Hi, can someone give me some ideas on what type/brand of usb cameras are not suitable for labview? I need to use with labview and IMAQ Cheers acquisition image vision module

    Hi, can someone give me some ideas on what type/brand of usb cameras are not suitable for labview? I need to use with labview and image acquisition IMAQCheers vision module

    Hi, I use a 1.4MP USB camera with LabView. The brand is ID - a German company.

  • When I burn CD for my use on the computer, sometimes the order of the songs are returned, starting with the last song first.

    * Original title: Windows Media player

    When I burn CD for my use on the computer, sometimes the order of the songs are returned, starting with the last song first.  This is the CD, or I do something wrong?  This isn't an ongoing problem... most of the time it's ok.  Can I change the order manually. If Yes, where and how.

    Thank you

    Lee

    Lee - there are a number of possibilities. First of all, it depends whether you burn audio disks or data - if they are audio discs, you can check and rearrange the order of songs in the burn list - if necessary - before you start burning. If you drag multiple files to the burn list quite often the last track you clicked on will go upward, so I wonder if it is what can happen. Back with a little more detail if I not quite correctly interpreted the problem. Best regards, Ric.

  • All trying to deploy Linux VM model using PowerCLI, during customization in error that "do not provide dns when you create NIC mapping for customization with type 'Linux" ".

    All trying to deploy Linux VM model using PowerCLI, during customization in error that "do not provide dns when you create NIC mapping for customization with type 'Linux" ".

    See my response on the other thread where you posted the same question.

  • Windows XP Home edition has crashed - how to return WXP and use with the product key?

    Hello. I bought an Equium L20 last year and after 1 year my windows crashed due to a virus. I want to know how to get a real windows xp OS because the product under my laptop key is always saved for me and is a legitimate product but I can't get windows because it was obviously preinstalled and no CD was given to me with the package.

    How can I get back Windows to use with my product key on my WIN sticker below my laptop.

    Hello

    First of all, it is very strange that recovery CD is not delivered with your laptop. It's hard to believe, but anything is possible. As you know after buying a new product, it is very important to check all orders sent with it and if something is missing, you must call the local as soon as possible and not to wait a year.

    Sorry but this key code belongs to original recovery image and you can't use it with every installation of Windows XP Home edition CD. In your case, there are two options:
    -You can buy the CD of facilities Microsoft original WXP (with the key correctly code) or
    -Contact the partner of service allowed in your country and order the new CD from recovery for your laptop model.

    All that s!

    Bye and good luck!

  • What type of portable music player I have to use with media player, Pocket size.

    I don't want to use itune!

    Hey! :), you don't have to use itunes. Any type of device you have, such as an MP3 player or ipod. And you don't need what whether media player... you just need to have music... files like from a CD or something. :) hope this helps

  • Tables created in a stored procedure cannot be used with dynamic SQL? The impact?

    There is a thread on the forum which explains how to create tables within a stored procedure (How to create a table in a stored procedure , however, it does create a table as such, but not how to use it (insert, select, update, etc.) the table in the stored procedure.) Looking around and in the light of the tests, it seems that you need to use dynamic SQL statements to execute ddl in a stored procedure in Oracle DB. In addition, it also seems that you cannot use dynamic SQL statements for reuse (insert, select, update, etc.) the table that was created in the stored procedure? Is this really the case?

    If this is the case, I am afraid that if tables cannot be 'created and used"in a stored procedure using the dynamic SQL, as is the case with most of the servers of DB dynamic SQL is not a part of the implementation plan and, therefore, is quite expensive (slow). This is the case with Oracle, and if yes what is the performance impact? (Apparently, with Informix, yield loss is about 3 - 4 times, MS SQL - 4 - 5 times and so on).

    In summary, tables created within a stored procedure cannot be 'used' with dynamic SQL, and if so, what is the impact of performance as such?

    Thank you and best regards,
    Amedeo.

    Published by: AGF on March 17, 2009 10:51

    AGF says:
    Hi, Frank.

    Thank you for your response. I understand that the dynamic SQL is required in this context.

    Unfortunately, I am yet to discover "that seeks to" using temporary tables inside stored procedures. I'm helping a migration from MySQL to Oracle DB, and this was one of the dilemmas encountered. I'll post what is the attempt, when more.

    In Oracle, we use [global temporary Tables | http://www.psoug.org/reference/OLD/gtt.html?PHPSESSID=67b3adaeaf970906c5e037b23ed380c2] aka TWG these tables need only be created once everything like a normal table, but they act differently when they are used. The data inserted in TWG will be visible at the session that inserted data, allowing you to use the table for their own temporary needs while not collide with them of all sessions. The data of the TWG will be automatically deleted (if not deleted programmatically) when a) a commit is issued or b) the session ends according to the parameter that is used during the creation of the TWG. There is no real need in Oracle to create tables dynamically in code.

    I noticed that many people say that the "Creation of the tables within a stored procedure" is not a good idea, but nobody seems necessarily explain why? Think you could elaborate a little bit? Would be appreciated.

    The main reason is that when you come to compile PL/SQL code on the database, all explicit references to tables in the code must correspond to an existing table, otherwise a djab error will occur. This is necessary so that Oracle can validate the columns that are referenced, the data types of those columns etc.. These compilation controls are an important element to ensure that the compiled code is as error free as possible (there is no accounting for the logic of programmers though ;)).

    If you start to create tables dynamically in your PL/SQL code, so any time you want to reference this table you must ensure that you write your SQL queries dynamically too. Once you start doing this, then Oracle will not be able to validate your SQL syntax, check the types of data or SQL logic. This makes your code more difficult to write and harder to debug, because inevitably it contains errors. It also means that for example if you want to write a simple query to get that one out in a variable value (which would take a single line of SQL with static tables), you end up writing a dynamic slider all for her. Very heavy and very messy. You also get the situation in which, if you create tables dynamically in the code, you are also likely to drop tables dynamically in code. If it is a fixed table name, then in an environment multi-user, you get in a mess well when different user sessions are trying to determine if the table exists already or is the last one to use so they can drop etc. What headache! If you create tables with table names, then variable Dynamics not only make you a lot end up creating (and falling) of objects on the database, which can cause an overload on the update of the data dictionary, but how can ensure you that you clean the tables, if your code has an exception any. Indeed, you'll find yourself with redundant tables lying around on your database, may contain sensitive data that should be removed.

    With the TWG, you have none of these issues.

    Also, what is the impact on the performance of the dynamic SQL statements in Oracle? I read some contrasting opinions, some indicating that it is not a lot of difference between static SQL and SQL dynamic in more recent versions of Oracle DB (Re: why dynamic sql is slower than static sql is this true?)

    When the query runs on the database, there will be no difference in performance because it is just a request for enforcement in the SQL engine. Performance problems may occur if your dynamic query is not binding variable in the query correctly (because this would cause difficult analysis of the query rather than sweet), and also the extra time, to dynamically write the query running.

    Another risk of dynamic query is SQL injection which may result in a security risk on the database.

    Good programming will have little need for the tables of dynamically created dynamically or SQL.

  • Can what kind of mouse I use with my Macbook Pro?

    I intend to buy a mouse for my Macbook Pro 13 "retina. Do I have that right to say that a powered bluetooth mouse can be used with my macbook? In addition, what other mouse types can I use with my macbook?

    Kind regards

    Rayan

    Pretty much any USB or BT mouse, trackball or trackpad should work just fine.

  • This email address is already in use with another Apple ID

    Hi, I had a problem with the evolution of my apple ID, that I had two Apple id and I wanted to use the Email address I have objective for labor and official use, the other is that my backup and everything in it, was that I created in my teens, if the Email is very unprofessional I removed my official email address of this apple ID and when I try to add in the apple ID , I use currently it is said that "' this Email address is already in use with another Apple ID" when it's actually longer, how can I swtich my official Email deal with my Apple ID? " Please help and thank you,

    I'm not sure to understand how you use your Apple ID... it seems that you want to have your communication and purchases on that 'professional' - work and personal communications, games, music, etc. on another.

    Both should be saved.

    All the data associated with an identifier Apple is always linked to this identifier to Apple.

    If you have changed your e-mail address to manage your Apple ID , you may need to Contact Apple for the Apple ID account security support

  • HP Operating System CD sold and used with the computer of the other company.

    I bought this CD from:

    AClass products

    32 c Westfield, Trading Estate

    Westfield Trading Estate

    Midsomer Norton, Somerset

    BA3 - 4BH

    United Kingdom

    I found on this CD - he wrote "For Distribution only with new" or HP Compaq PC

    Commercial sale claim, is that you can use it on any Acer, Dell, Fujitsu, Toshiba and other computer laptop or desktop - if they are, things can go wrong.

    Its great to buy a HP CD to go with the HP for laptop or desktop, even if they think that using it on other computer companies may not cause problems. If I used it on a Dell, Toshiba or another computer, I know he can or can not install, but if this is the case, it may hang after that I have try or warn just not. That is why, I know things happen. For example you get HP Logo to appear in a Dell computer, or the Dell Logo appears in an HP computer - if the computer can still run the software, the software of the BIOS has instructions Stop and Go, just like WIFI and brand cards labeled software.

    My problem or the question is, if someone claims its possible to use the CD of the HP operating system on other computers from computer companies, as if the problems never happen, its apples mixture similar to oranges unless you like a fruit salad, its best to put things in their place. I would like to know the truth of HP or an expert, some people say its strange I would have problems, they think it's harmless - there must be a reason for the problem when it is used on different computers. It would be good to know that I'm not the only one who knows how this happened, to confirm what I discovered and read, or if asked that its harmless to use with other computers, or simply absurd, and why.

    Hi Kabigan

    I even asked Microsoft and the answer they gave me, it's illegal. Society, I bought an OEM CD (Got back CD instead) and license, even to send the label with the serial number where his tell clear the Furijitsu, my old laptop computer is Hp. normally in my area where I worked as HE consultantant license belong to the preinstalled computer, it was purchased with.

    My experience it won't work, because a recovery cd is check the bios and see that this isn't the computer that made the recovery cd. I have not yet opened the package, because I consider return the cd and the company label.

    I may be wrong because I don't know the law, but normally software license is the same for the EMEA region for the whole country. It could be if she sold the Germany law differs "German Federal Court of Justice 06/07/2000 (find ZR 244/97) distribution of OEM and DSP licensing."

  • Any camera regardless of the interface is available for use with the LabView interface.

    Hello

    I intend to go for some CMOS camera,

    but I have a huge doubt before buying, the camera of menttioned above is not anywhere in this list. Nor can I see any type being supported USB device.

    The question is

    1. is a camera regardless of the interface is available for use with the LabView interface?
    2. Can I build a VI to communicate with any device image and recording of camera and take the data?

    Any kind of help or advice is greatly appreciated... I have to buy a CMOS camera and begin to run.

    Thank you...

    Hello Virginia,.

    I am pleased that this information has been useful, one thing I wanted to mention is that USB 3.0 has its own standard USB 3.0 Vision which is currently not supported. If this camera is also Direct Show compatible then you will be able to acquire an image using IMAQdx and manipulate all the attributes that are published to the API Live Show.

    I hope that USB 3.0 Vision will be supported in the near future, and we tentatively announced for this standard of communication for the August 2013 Vision Acquisition Softwareupdate.

    See you soon,.

    -Joel

Maybe you are looking for