id        gongshi                   emplid{000014}     25                   3456
{000007} {000006}+{000014}+{000003} 3456
{000003}    2500                   3456
{000006}    120                   3456
{000018}    20.00                   3456
请问各位高手,把gongshi 中的id替换成相应的数值最后计算出结果 ,请问用什么方法最好,怎么写?
在线等,马上给分

解决方案 »

  1.   

    --建立测试环境
    create table tab
    (
    id varchar(20),
    gongshi varchar(200),
    emplid int
    )insert into tab select '{000014}','25', 3456
    insert into tab select '{000007}','{000006}+{000014}+{000003}',3456
    insert into tab select '{000003}','2500',3456
    insert into tab select '{000006}','120',3456
    insert into tab select '{000018}','20.00',3456
    --语句
    update t3
    set gongshi = t4.gongshi
    from tab t3,
    (
    select t1.id,sum(cast(t2.gongshi as dec)) as gongshi
    from tab t1,tab t2
    where charindex(t2.id,t1.gongshi) > 0 and charindex('{',t1.gongshi) > 0 and isnumeric(t2.gongshi) = 1
    group by t1.id
    )t4 where t3.id = t4.id
    注:如果gongshi字段里面包含的ID所对应的公式也包含其它ID的话,这个语句不好使
      

  2.   

    --更新结果
    select * from tab{000014} 25 3456
    {000007} 2645 3456
    {000003} 2500 3456
    {000006} 120 3456
    {000018} 20.00 3456
      

  3.   

    id        gongshi                   emplid{000014}     25                   3456
    {000007} {000006}+{000014}+{000003} 3456
    {000003}    2500+{000006}                   3456
    {000006}    120                   3456
    {000018}    20.00                   3456
    请考虑公式套公式的情况