这里有一个例子:
http://expert.csdn.net/Expert/topic/2323/2323708.xml?temp=.2692224
我用sql server数据库,库中有如下两表。
表t1保存不同类型人员在消费时的折扣计算公式及奖励积分计算公式,如下:
类型          折扣价                         积分
一般会员    本次消费额* 0.95          积分+round(本次消费额/1000)
白银会员    本次消费额* 0.9            积分+round(本次消费额/1000)
黄金会员    本次消费额* 0.85          积分+round(本次消费额/1000)
钻石会员    本次消费额* 0.8            积分+round(本次消费额/1000)表t2中保存人员的消费情况,并要根据t1中各对应字段公式计算其结果,如下:
会员编号   类型             本次消费额        折扣价          积分
002         白银会员           1520.00              0               0
005         黄金会员           3300.00              0               0
007         一般会员             350.00              0               0
034         钻石会员             520.00              0               0
002         白银会员             780.00              0               0如上两表,在sql server查询分析器里我想用t1表中的公式直接用update命令计算出t2表中对应字段的值。命令如下:update t2 set 折扣价=t1.折扣价,积分=t1.积分 from t1,t2 where t1.类型=t2.类型可是执行时却提示说数据类型转换出错,因为t1表中的折扣价字段和积分字段为char型,保存的计算公式,可在t2表中的折扣份和积分字段却为数值型,所以出错。本人要如何才能实现如上算法。请高人们帮我看看我应如何做!查了一夜sql书也找不到办法,只能求教各位了。
*******************
create table #t1(类型 varchar(8),折扣价 varchar(100),积分 varchar(100))
insert #t1 select 
'一般会员','本次消费额*0.95','积分+round(本次消费额/1000,0)'
Union all select 
'白银会员','本次消费额*0.9','积分+round(本次消费额/1000,0)'
Union all select 
'黄金会员','本次消费额*0.85','积分+round(本次消费额/1000,0)'
Union all select 
'钻石会员','本次消费额*0.8','积分+round(本次消费额/1000,0)'create table #t2(会员编号 char(3),类型 varchar(8),本次消费额 numeric(10,2),折扣价 numeric(10,2),积分 int)
insert #t2 select
'002','白银会员',1520.00,     0,      0
union all select 
'005','黄金会员',3300.00,     0,      0
union all select 
'007','一般会员',350.00,     0 ,     0
union all select 
'034','钻石会员',520.00,     0 ,     0
union all select 
'002','白银会员',780.00,     0 ,     0
declare @a char(100),@b varchar(100),@c varchar(100)declare cur cursor for select * from #t1
open curfetch next from cur into @a,@b,@cwhile @@fetch_status = 0
begin
  exec('update #t2 set 折扣价='+@b+',积分='+@c+' where 类型='''+@a+'''')
fetch next from cur into @a,@b,@c
end
close cur
deallocate curSelect * from #t2
go
drop table #t1,#t2

解决方案 »

  1.   

    这里也有一个:
    http://expert.csdn.net/Expert/topic/2421/2421402.xml?temp=.9925653
    我想在一个表中的某一字段村一个计算公式.在另一个表中存的要计算的实际项目(项目是可变的),请问各位高手,我该如何实现呀?
    例如:我保存的公式是:((A+B)*C)/D;
    而我存的数据是:1  A   20
                   2  B   10
                   3  C   5
                   4  D   15
    我想要的结果是((20+10)*5)/15=10
    类似财务软件中的计算工资的方法,非常急,那位高手请指点一下。我先谢谢啦.呵呵这个简单,做个过程就可以了。举例:declare @公式 varchar(100)
    set @公式='((A+B)*C)/D'create table 你的表(id int,name varchar(10),value int)
    insert 你的表 values(1,  'A',   20)
    insert 你的表 values(2,  'B' ,  10)
    insert 你的表 values(3,  'C'  , 5)
    insert 你的表 values(4,  'D'   ,15)select @公式=replace(@公式,name,value) from 你的表exec('select '+@公式+' 结果')go
    drop table 你的表
      

  2.   


    EXEC('...')
    或:
    sp_executesql
      

  3.   

    SET QUOTED_IDENTIFIER OFF 
    GO
    SET ANSI_NULLS OFF 
    GO/******  希望能够给你有所参考。******/
    --select * from fyd('2002-10-12' , '2002-12-12' ,  '01'  , 10)     vThreadNo
    ALTER     function dbo.fyReturn
    (@dat varchar(22),@dat1 varchar(22), @s varchar(55)/*, @i int*/)
    returns @table table
    (
    序号 int identity(1,1),
    购贷单位 varchar(111),
    品名 varchar(50),
    规格 varchar(50),
    -- 数量 numeric(27,2),
    -- 出厂单价 numeric(27,2),
    结算价 numeric(27,2),
    实际单价 numeric(27,2),
    金额 numeric(27,2),
    发出时间 datetime,
    回款时间 datetime,
    回款期限 int,
    返点比例 numeric(27,2),
    返点金额 numeric(27,2),
    备注 varchar(111),
    地区编号 varchar(50),
    负责人编号 varchar(50)--,
    -- 发贷类型 varchar(50)
    )
    as
    begin
    declare @str1 varchar(50), @str2 varchar(50), @str3 varchar(50), @mon1 numeric(27, 2), @mon2 numeric(27, 2)
       , @int int, @int1 int
       set @str1 = '' set @str2 = '' set @str3 = '' set @mon1 = 0 set @mon2 = 0 set @int =0 set @int1 = 0
    declare cursor3 cursor 
    for select DISTINCT yb.vPersonNo
           from yb 
           where /*yb.mRealPrice / yb.mTradePrice > 0.75 and   and yb.dConsignmentDate between cast ('' + @dat + '' as DateTime) and cast ('' + @dat1 + '' as DateTime)
     and yb.vRegionNo = '' + @s  + ''
           --回款时间减去发贷时间即回款周期小于最大的“返点天数”。不回款不返点,有回款必定有回款时间!!!
    open cursor3
    FETCH NEXT FROM cursor3 INTO @str1
    while (@@fetch_status =0)
    begin   
       set @int = @int + 1
       select @str3 = vPersonName from YSalesDelegatePersonBase where vPersonNo = '' + @str1 + ''
       insert into @table (
    购贷单位, 品名/*, 数量*/
    , 实际单价, 返点比例, 金额
    , 发出时间, 回款时间, 回款期限
    , 负责人编号 /*发贷类型*/)
       select yb.vReturnFundClientName, yb.vProductName/* cast(yb.iReturnFundAmount as numeric(27, 2))*/
    , cast(yb.mRealPrice as numeric(27, 2)), cast(iScale as numeric(27,2)), cast(yb.mReturnFundSum as numeric(27, 2))
    , yb.dConsignmentDate, yb.dReturnFundDate, yb.intervalDate
    , yb.vPersonNo/*, substring(yb.vConsignmentNumber, 7, 2)*/
       from yb 
       where /*yb.mRealPrice / yb.mTradePrice > 0.75 and */
    (cast((yb.dReturnFundDate - yb.dConsignmentDate) as int)
       < yb.iReturnFundTerm)/*(select max(iReturnFundTerm) from YReturnFundScale))*/
    and yb.dConsignmentDate between cast ('' + @dat + '' as DateTime) 
    and cast ('' + @dat1 + '' as DateTime)
    and yb.vPersonNo = '' + @str1  + ''   insert into @table (购贷单位, 负责人编号) 
    values('' + @str3 + '合计', cast(@int as varchar(50)))
    fetch next from cursor3 into @str1
    end
    CLOSE cursor3
    DEALLOCATE cursor3 update @table set 返点金额 = 金额 * 返点比例
    declare cursor1 cursor 
    for SELECT distinct 负责人编号
    FROM @table
    open cursor1
    FETCH NEXT FROM cursor1 INTO @str1
    set @int = 0 
    while (@@fetch_status =0)
    begin   
    set @int = @int + 1
    -- 因为不同的负责人编号与需要合计的负责人编号相等!所以求出不同负责人的返点金额合计。
    select @mon1 = sum(返点金额) from @table where 负责人编号 = @str1
    -- 更新合计
    update @table set 返点金额 = @mon1 where 负责人编号 = cast(@int as varchar(50))
    fetch next from cursor1 into @str1
    --特别注意:循环次数等于记录数!当@str1等记录尾值时,离记录数还差1,还会循环,@str1仍将等于记录尾值!!!

    end
    CLOSE cursor1
    DEALLOCATE cursor1
    return
    end
    /*
    select * 
       from ya
    select * from YReturnFundScale
    select cast(cast('2003-03-07' as datetime) - cast('2003-01-07' as datetime) as int)
    select * from fya('2000-02-02','2003-12-12','01', 10)
    */GO
    SET QUOTED_IDENTIFIER OFF 
    GO
    SET ANSI_NULLS ON 
    GO