遍历BOM表的SQL函数
创始人
2024-07-17 16:01:09
0

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表格变量的用法

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...