日期:2014-05-18 浏览次数:20395 次
create table tb(rank int identity(1,1),baseIncome int,overmin int,overmax int,taxs varchar(10)) insert into tb select 2000,0,1000,'5%' insert into tb select 2000,1000,3000,'10%' insert into tb select 2000,3000,6000,'15%' insert into tb select 2000,6000,10000,'20%' insert into tb select 2000,10000,15000,'25%' create proc wsp @Income money as select 所要交的税=(@Income-baseIncome)*cast(replace(taxs,'%','') as numeric(5,2))/100 from tb where @Income-baseIncome between overmin and overmax-1 exec wsp 5000 --结果:450
------解决方案--------------------
create table cTaxRate ( minV decimal(18,2), maxV decimal(18,2), TaxRate decimal(18,2), --税率 Allowance decimal(18,2) --速算扣除数 ) create function cFunctionTax(@Amount decimal(18,2)) returns decimal(18,2) as Begin declare @taxV decimal(18,2), @taxBase decimal(18,2) select @taxBase=2000 -- select @taxBase=字段 from 表 select @taxV=(@Amount-@taxBase)*TaxRate-Allowance from cTaxRate where (@Amount-@TaxBase)>=minV and (@Amount-@TaxBase)<maxV return @TaxV End
------解决方案--------------------
create table 税率表 ( rank int , baseIncome numeric(18,2) , overmin numeric(18,2), overmax numeric(18,2) , taxs numeric(18,6) ) insert 税率表 select 1, 2000, 0 , 1000 , 0.05 union all select 2, 2000, 1000 , 3000 , 0.1 union all select 3, 2000, 3000 , 6000 , 0.15 union all select 4, 2000, 6000 , 10000 , 0.2 union all select 5, 2000, 10000, 15000 , 0.25 go create proc pr_计算税金 @income numeric(18,2) as select sum(case when @income>baseIncome+overmax then overmax-overmin else @income-baseIncome-overmin end *taxs) as 税金 from 税率表 where baseIncome+overmin<=@income go exec pr_计算税金 2500 --25 exec pr_计算税金 3500 --100 exec pr_计算税金 9500 --1000 exec pr_计算税金 12000 --1500
------解决方案--------------------
create table #tab (id int identity(1,1),baseincome int,overmin int,overmax int,taxs float) insert into #tab values(2000,0,1000,0.05) insert into #tab values(2000,1000,3000,0.1) insert into #tab values(2000,3000,6000,0.15) insert into #tab values(2000,6000,10000,0.2) insert into #tab values(2000,10000,15000,0.25) create proc #tab_p (@money float) as begin select sum(case when @money-(baseincome+overmin)>overmax then overmax when @money-(baseincome+overmin)<0 then 0 else @money-(baseincome+overmin) end*taxs)from #tab end exec #tab_p 4500 200