遍历BOM表的SQL函数

数据库 SQL Server
SQL函数可以实现诸多的功能,下文就介绍了一个用于实现遍历BOM表的方法,如果您对此方面感兴趣的话,不妨一看。

SQL函数的种类很多,实现的功能也不太一样。下面为您介绍的是用于遍历BOM表的SQL函数,希望可以让您对SQL函数有更多的了解。

表结构如下:
ptype subptype amount
a  a.120
a  a.2 15
a  a.3 10
a. 1 a.1.1 20
a.1a.1.2  15
a.1 a.1.330
a.2 a.2.110
a.2 a.2.2 20
a.1.1 a.1.1.1 45
a.1.1 a.1.1.2 15
a.2.1 a.2.1.1 20
a.2.2 a.2.2.1 13

  1. create table matgroup(parentgroup varchar(50),childgroup varchar(50), mount float)  
  2.  
  3. insert into matgroup   
  4. select 'a',  'a.1',20  
  5. union select 'a',  'a.2', 15  
  6. union select 'a',  'a.3', 10  
  7. union select 'a.1', 'a.1.1', 20  
  8. union select 'a.1','a.1.2',  15  
  9. union select 'a.1', 'a.1.3',30  
  10. union select 'a.2', 'a.2.1',10  
  11. union select 'a.2', 'a.2.2', 20  
  12. union select 'a.1.1', 'a.1.1.1', 45  
  13. union select 'a.1.1', 'a.1.1.2', 15  
  14. union select 'a.2.1' ,'a.2.1.1', 20  
  15. union select 'a.2.2', 'a.2.2.1', 13  

函数如下:

  1. create FUNCTION fn_aaa (@matgroup varchar(50),@mount int )  
  2. RETURNS @retPLExpand TABLE (parentgroup varchar(50),childgroup varchar(50), mount float)  
  3.  
  4. AS  
  5. BEGIN  
  6. DECLARE @RowsAdded int  
  7. declare @PLExpand Table (parentgroup varchar(50),childgroup varchar(50), mount float,processed tinyint default(0))  
  8.  
  9. INSERT @PLExpand  
  10.  SELECT b.parentgroup,b.childgroup, @mount*b.mount, 0  
  11.  FROM matgroup b   
  12.  WHERE b.parentgroup=@matgroup  
  13. SET @RowsAdded = @@rowcount  
  14.  
  15. -- While new employees were added in the previous iteration  
  16.  
  17. WHILE @RowsAdded > 0  
  18.  
  19. BEGIN  
  20. /*Mark all employee records whose direct reports are going to be   
  21. found in this iteration with processed=1.*/  
  22. UPDATE @PLExpand  
  23. SET processed = 1 
  24. WHERE processed = 0 
  25.  
  26. -- Insert employees who report to employees marked 1.  
  27. INSERT @PLExpand  
  28. SELECT a.parentgroup,a.childgroup,a.mount*b.mount , 0  
  29. FROM matgroup a inner join @PLExpand b on a.parentgroup=b.childgroup  
  30.   where b.processed = 1 
  31.  
  32. SET @RowsAdded = @@rowcount  
  33. /*Mark all employee records whose direct reports have been found  
  34. in this iteration.*/  
  35.  
  36. UPDATE @PLExpand  
  37. SET processed = 2 
  38. WHERE processed = 1 
  39. END  
  40.  
  41. -- copy to the result of the function the required columns  
  42. INSERT @retPLExpand  
  43. SELECT parentgroup,childgroup,mount  
  44. FROM @PLExpand  
  45. RETURN  
  46. END  

调用方法如下:
select * from fn_aaa('a.1')
意思是找出a.1下的所有儿子及孙子.
 
 

 

 

【编辑推荐】

动态sql中使用临时表的实例

Oracle存储过程使用动态SQL

SQL Server删除视图的两种方法

SQL Server视图的使用

sql server表格变量的用法

责任编辑:段燃 来源: 互联网
相关推荐

2010-09-09 13:32:14

SQL函数遍历

2010-11-12 14:10:15

SQL遍历父子关系表

2010-11-11 10:41:03

sql server遍

2010-09-06 16:52:17

SQL函数

2010-11-11 10:53:22

SQL Server遍

2010-11-11 11:00:06

sql server遍

2010-09-16 14:38:55

Sql server表

2010-09-14 15:51:15

sql遍历

2010-09-16 09:15:59

SQL函数

2010-11-24 13:11:06

MySQL遍历数据表

2010-09-09 11:23:17

SQL函数格式

2010-09-09 16:40:58

SQL循环游标

2010-07-02 09:00:57

jQuery

2010-09-17 16:03:17

锁定SQL表

2010-09-16 14:13:11

SQL Server系

2010-09-10 15:37:44

SQL函数

2010-09-09 13:26:52

SQL函数判断

2010-09-10 15:51:51

SQL分析函数

2010-09-06 14:17:04

SQL函数

2010-09-24 19:28:12

SQL CHARIND
点赞
收藏

51CTO技术栈公众号