国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

金額合計求值問題

2024-07-21 02:07:54
字體:
來源:轉載
供稿:網友


原帖地址:

http://community.csdn.net/expert/topic/3190/3190686.xml?temp=.6296961

表test中記錄:
  aa     bb 
  001   50.5
  002   60
  003   15.4
  004   25
  005   48
  ...

輸入任一金額,然后在表中查找是否有該金額或幾條記錄的合計等于該金額.
如:輸入25,則要找出004,輸入85,則要找出002與004,依次類推。
------------------------------------------------------------------------------------


--測試數據
create table test(aa varchar(10),bb numeric(10,2))
insert test select '001',50.5
union  all  select '002',60
union  all  select '003',15.4
union  all  select '004',25
union  all  select '005',48
union  all  select '006',37
go

--查詢函數
create function fn_search(@num numeric(10,2))
returns @r table (aa varchar(10),bb numeric(10,2))
as
begin
 declare @t table (aa varchar(8000),aa1 varchar(10),bb numeric(10,2),level int)
 declare @l int

 insert @r select aa,bb from test where [email protected]
 if @@rowcount>0 goto lb_exit

 set @l=0
 insert @t select ','+aa+',',aa,bb,@l from test where bb<@num
 while @@rowcount>0
 begin
  insert @r select distinct a.aa,a.bb
  from test a,(
   select a.aa,a.bb,aa1=b.aa from test a,@t b
   where [email protected]
    and b.aa1<a.aa
    and [email protected]
  )b where a.aa=b.aa or charindex(','+a.aa+',',b.aa1)>0
  if @@rowcount>0 goto lb_exit

  set @[email protected]+1
  insert @t select b.aa+a.aa+',',a.aa,a.bb+b.bb,@l
  from test a,@t b
  where [email protected]
   and b.aa1<a.aa
   and a.bb<@num-b.bb
 end

lb_exit:
 return
end
go

--調用測試1
select * from dbo.fn_search(25)

/*--結果

aa         bb          
---------- ------------
004        25.00

(所影響的行數為 1 行)
--*/

--調用測試2
select * from dbo.fn_search(135.5)

/*--結果

aa         bb          
---------- ------------
001        50.50
002        60.00
004        25.00
005        48.00
006        37.00

(所影響的行數為 5 行)
--*/

--調用測試3(找不到的,無返回值)
select * from dbo.fn_search(135.7)

/*--結果
aa         bb          
---------- ------------

(所影響的行數為 0 行)
--*/
go

drop table test
drop function fn_search
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 葫芦岛市| 定陶县| 射洪县| 延吉市| 桂东县| 德清县| 含山县| 林芝县| 赤壁市| 耿马| 都安| 静海县| 澄迈县| 龙川县| 鄱阳县| 介休市| 福泉市| 西乌珠穆沁旗| 新丰县| 桐庐县| 中江县| 永顺县| 黔西县| 兴安县| 芦溪县| 静安区| 新平| 黄大仙区| 繁峙县| 青浦区| 册亨县| 抚远县| 丰台区| 焦作市| 双城市| 东台市| 沙洋县| 吴忠市| 淅川县| 三都| 西充县|