home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a061 / 1.img / CLIENT1 / ACT10B.QRY < prev    next >
Encoding:
Text File  |  1992-02-21  |  3.4 KB  |  79 lines

  1. /*  The procedure created by this query will "explode" a Bill of Materials
  2. to an arbitrary depth. It uses a "stack" in the form of a temporary table,
  3. similar to stacks in other programming languages.  The current parent is
  4. placed on the stack, the children of that parent are found and added to the
  5. stack, and the parent is deleted. The procedure then tries to find the next
  6. level of children down for every entry in the stack. This continues until
  7. there are no more lower levels of children.
  8.  
  9. The output of this procedure is simply a printout of the parents followed
  10. by all of their children, with each level of child indented. */
  11.  
  12.  
  13.  
  14. create procedure BOM (@current char(40)) 
  15. as
  16. set nocount on                     /* Turn off printout of rows processed */
  17.  
  18. declare @level int,                /* Create two temporary variables */
  19.         @msg   char(40)
  20.  
  21. create table #stack (child char(20), level int)   /* Create the "stack" */
  22.  
  23. insert into #stack values (@current, 1)           /* The first entry in the
  24.                                                   stack is the parameter
  25.                                                   passed when procedure is
  26.                                                   called. This is level 1. */
  27.  
  28. select @level = 1                  /* Select is used to set the initial value
  29.                                    of a variable */
  30.  
  31. while @level > 0                   /* Stop the process once there are
  32.                                    no more entries on the stack to find
  33.                                    children for. */
  34. begin
  35.  
  36.      if exists (select * from #stack
  37.           where level = @level)         /*  Are there any more parents on
  38.                                         the stack at the current level?
  39.                                         If not, jump to the ELSE statement */
  40.  
  41.      begin
  42.  
  43.           select @current = MIN(child)
  44.           from #stack
  45.                where level = @level     /*  Find the one lowest parent on
  46.                                         the stack at the current level.  */
  47.  
  48.           select @msg = SPACE(@level * 4)
  49.                + @current               /* Fill the msg string
  50.                                         with parent name, indented */
  51.           print @msg
  52.  
  53.           delete from #stack
  54.                where level = @level
  55.                and child = @current     /* Remove the parent just printed
  56.                                         from the stack */
  57.  
  58.           insert #stack
  59.                select child, @level + 1
  60.                from hierarchy
  61.                where parent = @current  /* Insert new entries on the stack
  62.                                         which are the children of the parent
  63.                                         just printed. These become the new
  64.                                         parents. */
  65.  
  66.           if @@rowcount > 0
  67.                select @level=@level + 1   /* Were there any children selected
  68.                                           by the last statement? If so,
  69.                                           increment the @level variable
  70.                                           by 1. */
  71.  
  72.      end
  73.  
  74.      else select @level = @level - 1     /* When there are no more parents
  75.                                          on the stack at the current level,
  76.                                          look back up one level. */
  77.  
  78. end
  79.