stored procedure / recursion / sql server 2005

Last post 09-29-2006 3:54 AM by Iori_Jay. 8 replies.

Sort Posts:

  • stored procedure / recursion / sql server 2005

    09-22-2006, 2:25 AM
    • Participant
      1,014 point Participant
    • SpongeBert
    • Member since 08-10-2006, 10:21 PM
    • Posts 1,213

    Hello my friends

    This is my sql table structure

    FK = ID int,
    Empnaam varchar(200),
    PK = EmpID int

    With this table, where i insert values, employees can hire other employees. Now i want to see with a function in visual studio who hired who. I get stuck when a person hired more than 1 person....

    This is my stored procedure :


    set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    GO
    ALTER PROC [dbo].[ShowHierarchy]
    (
    @Root int
    )
    AS
    BEGIN
    SET NOCOUNT ON
    DECLARE @EmpID int, @EmpNaam varchar(30)

    SET @EmpNaam = (SELECT EmpNaam FROM dbo.Emp WHERE EmpID = @Root)
    PRINT REPLICATE('-', @@NESTLEVEL * 4) + @EmpNaam

    SET @EmpID = (SELECT MIN(EmpID) FROM dbo.Emp WHERE ID = @Root)

    WHILE @EmpID IS NOT NULL BEGIN
    EXEC dbo.ShowHierarchy @EmpID
    SET @EmpID = (SELECT MIN(EmpID) FROM dbo.Emp WHERE ID = @Root AND EmpID > @EmpID)
    END
    END

    Thanks in advance!

    Grtz

    Please give us feedback no matter whether you get your answer.
    Please "mark as answer" if it's useful for you

    Sincerely,
    SpongeBert
  • Re: stored procedure / recursion / sql server 2005

    09-25-2006, 2:57 AM
    • Star
      12,930 point Star
    • Iori_Jay
    • Member since 04-04-2006, 6:21 AM
    • Posts 2,450

    Your stored procedure works fine on my machine, it prints a tree view of employees for a given employee. How do you insert records into the Emp table? Do you use the ID column as a pointer to the record's parent record (the one who employs it)? Here is the data I used in my testing:

    EmpIDEmpNaamID
    0<Root>0
    1Iori0
    2Kyo0
    3Mai1
    4Athena1
    5Yuri2
    6Andy3
    7Kenson4
    8Joe6

    Welcome to my SQL/ASPNET forum for Chinese
    http://51up.org/bbs/forumdisplay.php?fid=38
  • Re: stored procedure / recursion / sql server 2005

    09-25-2006, 1:53 PM
    • Participant
      1,014 point Participant
    • SpongeBert
    • Member since 08-10-2006, 10:21 PM
    • Posts 1,213

    hi Iori_Jay,

    thank you very much for testing my code!! Can you tell me how i call this function in visual studio c#? Let's say i want to know who has hired IORI (so result is: MAI en ATHENA)

    I would be so helpfull if you could help me out on this one. i don't see it!! im so close!!

     

    THanks man! Do you have a blog?

    Peace out!

    Please give us feedback no matter whether you get your answer.
    Please "mark as answer" if it's useful for you

    Sincerely,
    SpongeBert
  • Re: stored procedure / recursion / sql server 2005

    09-26-2006, 4:39 AM
    • Star
      12,930 point Star
    • Iori_Jay
    • Member since 04-04-2006, 6:21 AM
    • Posts 2,450
    SpongeBert:

    hi Iori_Jay,

    thank you very much for testing my code!! Can you tell me how i call this function in visual studio c#? Let's say i want to know who has hired IORI (so result is: MAI en ATHENA)

    I would be so helpfull if you could help me out on this one. i don't see it!! im so close!!

     

    THanks man! Do you have a blog?

    Peace out!

    Seems our comprehension on the hire relation is different. As I understand, the data used in my testing should indicate that IORI hired MAI and ATHENA, not the opposite, so that's what I mean use ID column as ParentIDSmile. You can call it as:

    EXEC dbo.[ShowHierarchy] 1

    As the stored procedure has no output nor returned result set (it just prints the employ tree), you can't get the tree by calling it from application.

    Sorry I don't have a blog yet.

    Welcome to my SQL/ASPNET forum for Chinese
    http://51up.org/bbs/forumdisplay.php?fid=38
  • Re: stored procedure / recursion / sql server 2005

    09-26-2006, 5:14 AM
    Answer
    • Star
      12,930 point Star
    • Iori_Jay
    • Member since 04-04-2006, 6:21 AM
    • Posts 2,450

    Another flaw of this stored procedure is that it uses a recursive algorism, and in SQL Server the max nest level of stored procedure is 32. So the sp can't deal with hierarchy which has more than 32 levels. It's better to implement the tree traversal in code, much easier and better performance.

    Welcome to my SQL/ASPNET forum for Chinese
    http://51up.org/bbs/forumdisplay.php?fid=38
  • Re: stored procedure / recursion / sql server 2005

    09-26-2006, 12:11 PM
    • Participant
      1,014 point Participant
    • SpongeBert
    • Member since 08-10-2006, 10:21 PM
    • Posts 1,213

    Hi Iori_Jay,

     Thanks again for your reply.

    You're right about the hire relation. My bad. I know there are better ways for solving this problem but i want first to try this way, afterwards i try to improve my code.

     Can you please tell me how i get the output from that stored procedure? so i want to see who IORI hired with result MAI and ATHENA.

     Thanks in advance,

    Let me know when you're blog hits the internet!

     

    Please give us feedback no matter whether you get your answer.
    Please "mark as answer" if it's useful for you

    Sincerely,
    SpongeBert
  • Re: stored procedure / recursion / sql server 2005

    09-27-2006, 4:35 AM
    • Star
      12,930 point Star
    • Iori_Jay
    • Member since 04-04-2006, 6:21 AM
    • Posts 2,450

    If you only want to get the employees directly employed by IORI (ignore persons employed by MAI or ATHENA), you can simply use a SELECT:

    SELECT EmpNaam FROM dbo.Emp WHERE ID = @Root

    Well, we can do a little modification to the stored procedure, but the returned result will still be table-formed result set, you will still face the problem how to get a direct-viewing employ tree from the result set. The only work around I can find is to use a OUTPUT parameter in the stored procedure to host the strings used to be printed in the original sp:

    ALTER PROC [dbo].[ShowHierarchy]  @Root int,@outstring VARCHAR(8000) OUTPUT
    AS
    BEGIN
    SET NOCOUNT ON
    DECLARE @EmpID int, @EmpNaam varchar(30)

    SET @EmpNaam = (SELECT EmpNaam FROM dbo.Emp WHERE EmpID = @Root)
    SET @outstring= @outstring+REPLICATE('-', @@NESTLEVEL * 4) + @EmpNaam +'
    '
    SET @EmpID = (SELECT MIN(EmpID) FROM dbo.Emp WHERE ID = @Root)

    WHILE @EmpID IS NOT NULL BEGIN
    EXEC dbo.ShowHierarchy @EmpID,@outstring OUTPUT
    SET @EmpID = (SELECT MIN(EmpID) FROM dbo.Emp WHERE ID = @Root AND EmpID > @EmpID)
    END
    END


    DECLARE @outstring varchar(8000)
    SET @outstring=''
    EXEC [ShowHierarchy] 1,@outstring output
    PRINT @outstring

    Then in your code you can use a SqlCommand with an OUTPUT parameter to execute the stored procedure and store the output string into a variable. You can refer to: Working with Command Parameters 

    Please do remember to initialize the output parameter to an empty string ('') before you use it in the sp.

    Welcome to my SQL/ASPNET forum for Chinese
    http://51up.org/bbs/forumdisplay.php?fid=38
  • Re: stored procedure / recursion / sql server 2005

    09-27-2006, 12:31 PM
    • Participant
      1,014 point Participant
    • SpongeBert
    • Member since 08-10-2006, 10:21 PM
    • Posts 1,213

     

    Iori_Jay, thank you very much. Great explanation!  Keep up the good work

     

    Please give us feedback no matter whether you get your answer.
    Please "mark as answer" if it's useful for you

    Sincerely,
    SpongeBert
  • Re: stored procedure / recursion / sql server 2005

    09-29-2006, 3:54 AM
    • Star
      12,930 point Star
    • Iori_Jay
    • Member since 04-04-2006, 6:21 AM
    • Posts 2,450
    Thanks for your encouragement! It's my pleasure, and hope you enjoy working with SQL ServerSmile
    Welcome to my SQL/ASPNET forum for Chinese
    http://51up.org/bbs/forumdisplay.php?fid=38
Page 1 of 1 (9 items)