自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

遍歷BOM表的SQL函數(shù)

數(shù)據(jù)庫(kù) SQL Server
SQL函數(shù)可以實(shí)現(xiàn)諸多的功能,下文就介紹了一個(gè)用于實(shí)現(xiàn)遍歷BOM表的方法,如果您對(duì)此方面感興趣的話,不妨一看。

SQL函數(shù)的種類(lèi)很多,實(shí)現(xiàn)的功能也不太一樣。下面為您介紹的是用于遍歷BOM表的SQL函數(shù),希望可以讓您對(duì)SQL函數(shù)有更多的了解。

表結(jié)構(gòu)如下:
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  

函數(shù)如下:

  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  

調(diào)用方法如下:
select * from fn_aaa('a.1')
意思是找出a.1下的所有兒子及孫子.
 
 

 

 

【編輯推薦】

動(dòng)態(tài)sql中使用臨時(shí)表的實(shí)例

Oracle存儲(chǔ)過(guò)程使用動(dòng)態(tài)SQL

SQL Server刪除視圖的兩種方法

SQL Server視圖的使用

sql server表格變量的用法

責(zé)任編輯:段燃 來(lái)源: 互聯(lián)網(wǎng)
相關(guān)推薦

2010-09-09 13:32:14

SQL函數(shù)遍歷

2010-11-12 14:10:15

SQL遍歷父子關(guān)系表

2010-11-11 10:41:03

sql server遍

2010-09-06 16:52:17

SQL函數(shù)

2010-11-11 10:53:22

SQL Server遍

2010-11-11 11:00:06

sql server遍

2010-09-16 14:38:55

Sql server表

2025-03-03 10:51:29

SQL數(shù)據(jù)庫(kù)MySQL

2010-09-14 15:51:15

sql遍歷

2010-09-16 09:15:59

SQL函數(shù)

2010-11-24 13:11:06

MySQL遍歷數(shù)據(jù)表

2010-09-09 11:23:17

SQL函數(shù)格式

2010-07-02 09:00:57

jQuery

2010-09-09 16:40:58

SQL循環(huán)游標(biāo)

2010-09-17 16:03:17

鎖定SQL表

2010-09-16 14:13:11

SQL Server系

2010-09-06 14:42:56

SQL函數(shù)

2010-09-06 14:17:04

SQL函數(shù)

2010-09-10 15:51:51

SQL分析函數(shù)

2010-09-24 19:28:12

SQL CHARIND
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)