Recursive Stored Procedure

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fabiomoggi
    New Member
    • Jul 2008
    • 3

    Recursive Stored Procedure

    Hello Guys,

    I am developing a web application to manage Active Directory resources, and one of my tasks is to map Organizational Units hierarchy into a SQL Server database. Let's suppose that I have the following OU hierarchy in my Active Direcoty:

    1. NewYork
    1.1 HR_Department
    1.1.1 Computers
    1.1.2 Users
    1.2 SALES_Departmen t
    1.1.1 Computers
    1.1.2 Users
    1.3 So on.....
    2. Chicago
    2.1 HR_Department
    1.1.1 Computers
    1.1.2 Users
    2.2 SALES_Departmen t
    2.1.1 Computers
    2.1.2 Users
    2.3 So on....

    I designed the following table into SQL Server in order to reach the AD hierarchy:

    OUId INT
    // Just an integer ID to identify the row.

    OUName VARCHAR(50)
    // Name of OU that comes from AD.

    ParentOUId INT
    // Id of the parent OU to create as many levels as necessary.

    The idea is to create a recursive table to store the hierarchy in a flexible way that there won't be any limitations in terms of creating as many sub OU as necessary. At this point goes my issue: I am having problems to create a store procedure to return whether a OU already exists or not.

    Unfortunately Active Directory does not provide me a property that indicates who is parent OU. I can only identify the object structure by the distinguishedNa me property, which acording to my example returns something like:

    OU=Computers,OU =HR_Deparment, OU=NewYork, DC=....

    From this string, I convert it to an array:

    [0] Computers
    [1] HR_Department
    [2] NewYork

    From now on, I am stuck. I don't know how to create a recursive procedure or function in the context to return and create the AD Hierarchy.


    Could anyone help with it?

    Thank you very much for you attention!
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    I'd say use XML


    -- CK

    Comment

    • fabiomoggi
      New Member
      • Jul 2008
      • 3

      #3
      Hi,

      Thank you for the answer!

      I don't know how to approach it using XML.

      Could you help me on how to start it?

      Comment

      • Delerna
        Recognized Expert Top Contributor
        • Jan 2008
        • 1134

        #4
        Here is a question similar to yours that you might find helpful.

        Comment

        Working...