Reporting a menu structure using a recursive UDF

  • Comments posted to this topic are about the content posted at

  • This seems needlessly complex - I just made a UDF that recursively calls itself with more than 1 parameter in my temp database and it works fine?

    Example (and it's a silly one but it does work - it's 1am here!)

    use TempDB
    create function recurse(
    @p1 int,
    @p2 varchar(10)
    returns varchar(8000)
    declare @result varchar(8000)
    if @p1 = 0
      set @result = @p2
      set @result = dbo.recurse(@p1 - 1, @p2 + '.' + cast(@p1 as char(1))) 
    return @result
    print dbo.recurse(5, 'X')


    I can see the benefit of the idea for some reporting purposes where you cannot have the tool work out how the hierarchy relates in client-side code, but if the entire hierarchy is being sent back to the client, you could also just send back the table and let the client sort out the display as well...  Depends on the circumstances I suppose.

    If someone is to implement in their DB/software, try rewriting without the messy string splitting for parameter passing - I don't see why it shouldn't work...


  • I'll have to tweek my own code as for some reason I was sure I could only have one parameter.  Thanks for this.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply