Query Tree structure

Results 1 to 2 of 2

Thread: Query Tree structure

  1. #1
    Join Date
    Dec 1969

    Default Query Tree structure

    I have a table with the following structure<BR><BR>StartID EndID<BR>100 102<BR>102 201 <BR>201 302<BR>302 -<BR><BR>The user will be supplying only the start ID(100), I need to retrieve all the IDs under that until the EndID is null (in this case 102,201,302..)<BR><BR>How can I acheive this using a single query

  2. #2
    Join Date
    Dec 1969

    Default RE: Query Tree structure

    create a stored procedure that has that supplied startid as a parameter:<BR><BR>CREATE PROCEDURE GetIds<BR>@startID int<BR>AS<BR><BR>Then declare a variable for the end id, and get it out of the table:<BR><BR>DECLARE @endID int<BR><BR>SELECT @endID = EndID<BR>FROM tablename<BR>WHERE StartID = @startID<BR><BR>Then get out all the ids between those two:<BR><BR>SELECT id<BR>FROM tablename<BR>WHERE id BETWEEN @startID AND @endID<BR><BR>That should do it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts