金额合计求值问题

news/2024/7/11 0:14:38 标签: Go, .net, XML, Blog, HTML
<iframe align="top" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog01.html" frameborder="0" width="728" scrolling="no" height="90"></iframe>

原帖地址:

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 bb=@num
if @@rowcount>0 goto lb_exit

set @l=0
insert @t select ','+aa+',',aa,bb,@l from test where bbwhile @@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 b.level=@l
and b.aa1<a.aa>and <a href="mailto:a.bb=@num-b.bb">a.bb=@num-b.bb</a><br>)b where a.aa=b.aa or charindex(','+a.aa+',',b.aa1)&gt;0<br>if @@rowcount&gt;0 goto lb_exit</a.aa>

set @l=@l+1
insert @t select b.aa+a.aa+',',a.aa,a.bb+b.bb,@l
from test a,@t b
where b.level=@l-1
and b.aa1<a.aa>and a.bbend</a.aa>

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



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=67518



http://www.niftyadmin.cn/n/593561.html

相关文章

数据库建表原则

关键字: 数据库建表原则 1. 原始单据与实体之间的关系 可以是一对一、一对多、多对多的关系。在一般情况下&#xff0c;它们是一对一的关系&#xff1a;即一张原始单据对应且只对应一个实体。在特殊情况下&#xff0c;它们可能是一对多或多对一的关系&#xff0c;即一张原始单证…

从PRISM开始学WPF(番外)共享上下文 RegionContext?

原文:从PRISM开始学WPF&#xff08;番外&#xff09;共享上下文 RegionContext&#xff1f;RegionContext共享上下文 There are a lot of scenarios where you might want to share contextual information between the view that is hosting a region and a view that is insi…

数据编码 PHP,Php socket数据编码

bytes.php 字节编码类/** * byte数组与字符串转化类 * author * created on 2011-7-15 */class bytes { /** * 转换一个string字符串为byte数组 * param $str 需要转换的字符串 * param $bytes 目标byte数组 * author zikie */ public static function getbytes($str) { $len …

html触发条件,如何触发css3动画

如何触发css3动画1、可以选择利用css的状态&#xff1a;hover&#xff0c;focus&#xff0c;active 来触发&#xff0c;也可以一开始就触发。#aaa {width: 100px;height: 100px;background: blue;transition: width 2s;-moz-transition: width 2s; /* Firefox 4 */-webkit-tran…

字段里字符串的处理问题

<iframe align"top" marginwidth"0" marginheight"0" src"http://www.zealware.com/csdnblog01.html" frameborder"0" width"728" scrolling"no" height"90"></iframe>原帖地址:…

virtualProtect函数

原文链接&#xff1a;https://blog.csdn.net/zacklin/article/details/7478118 结合逆向课件11转载于:https://www.cnblogs.com/qy-blogs/p/9134114.html

SQL数据库建表前期优化

关于数据库优化方面的文章很多&#xff0c;但是有的写的似是而非&#xff0c;有的不切实际&#xff0c;对一个数据库来说&#xff0c;只能做到更优&#xff0c;不可能最优&#xff0c;并且由于实际需求不同&#xff0c;优化方案还是有所差异&#xff0c;根据实际需要关心的方面…

angular5 httpclient的示例实战

摘要: 从angular 4.3.0 以后的版本开始使用httpclient&#xff0c;替换了之前的http&#xff0c;引用的包路径已经变为了angular/common/http了一个基础的 httpclient 样例 import { Injectable } from angular/core; import { HttpClient, HttpResponse } from angular/common…