哦,上面的有地方写错了,重贴:数据表中有两个字段invest_number,invest_secondinvest_number=,1,4,8,12,15,18,22,25,32,39,  invest_second=,1.02,2.55,1.36,2.55,1.00,2.03,2.56,8.05,30.66,22.03,invest_number中的数字和invest_second中的数字是对应的现在另一个数据表(kk)中有六个字段,如果在六个字段中出现了A则要把invest_number,invest_second做相应的处理,
如果六个字段中没有出现字母A则不需做任何处理例如:表kk:字段一     字段二       字段三          字段四           字段五             字段六A            B            G              R              S                   EA出现在字段一的位置则要把invest_number中<8的数字替换成0,invest_second中相对应于invest_number位置的数字替换成1结果:invest_number=,0,0,8,12,15,18,22,25,32,39,  invest_second=,1,1,1.36,2.55,1.00,2.03,2.56,8.05,30.66,22.03,如果出现在字段二的位置则要把invest_number中<15的数字替换成0,invest_second中相对应于invest_number位置的数字替换成1结果为:invest_number=,1,4,0,0,15,18,22,25,32,39,  invest_second=,1.02,2.55,1,1,1.00,2.03,2.56,8.05,30.66,22.03,
依此类推,出现在第三就把invest_number<22的数字替换成0,invest_second中相对应于invest_number位置的数字替换成1第四就所invest_number<29的数字替换成0,invest_second中相对应于invest_number位置的数字替换成1
请问这个要怎么实现呢,

解决方案 »

  1.   

    看起来比较烦,还好逻辑比较清楚if exists(select * from kk where 字段一='A')
       处理语句
       returnif exists(select * from kk where 字段二='A')
       处理语句
       ruturn
    if ……是这样子吗?
      

  2.   

    更新下:
    数据表中有两个字段invest_number,invest_secondinvest_number                        invest_second,1,4,8,12,15,18,22,25,32,39,        ,1.02,2.55,1.36,2.55,1.00,2.03,2.56,8.05,30.66,22.03,这些值是一条记录不是多条记录的。
      

  3.   

    有几个问题你没有说清楚A出现在字段二的位置则要把invest_number中<15的数字替换成0,invest_number=,1,4,0,0,15,18,22,25,32,39,  
    1和4的位置为什么不变呢
    你说的规律是什么,如果字段六为A,那应该是什么样子的?
      

  4.   

    我是想用存储过程来实现,不想用asp因为数据多的时候会比较慢,用存储过程不会了,所以到数据库区来提问的。
      

  5.   

    /*是出现在字段一时数据中小于8的替换,
    出现在字段二时数据中大于7小于15的替换
    三的时候是大于14小于22
    四的时候大于21小于29
    五的时候大于28小于36
    六为A时就要把字符串中大于35小于43*/
    -- 创建测试
    create table mm(invest_number bigint,invest_second numeric(10,2))
    create table kk(col1 varchar(100),col2 varchar(100),col3 varchar(100),col4 varchar(100),col5 varchar(100),col6 varchar(100))
    -- 插入记录
    insert into mm
    select 1,1.02
    union select 4,2.55
    union select 8,1.36
    union select 12,2.55
    union select 15,1.00
    union select 18,2.03
    union select 22,2.56
    union select 25,8.05
    union select 32,30.66
    union select 39,22.03insert into kk
    select 'A','B','G','R','S','E'
    union select 'B','F','G','R','S','E'
    union select 'C','B','A','R','S','E'
    union select 'G','B','G','F','S','E'
    union select 'E','B','G','R','A','E'
    union select 'F','B','G','R','S','C'
    select * from mm
    select * from kk
    -- 操作
    alter procedure up_replace 
    as
    if exists(select top 1 * from kk where col1 like '%A%')
    begin
    update mm set invest_number = 0,invest_second = 1 where invest_number < 8
    end
    if exists(select top 1 * from kk where col2 like '%A%')
    begin
    update mm set invest_number = 0,invest_second = 1 where 7<invest_number and invest_number <15
    end
    if exists(select top 1 * from kk where col3 like '%A%')
    begin
    update mm set invest_number = 0,invest_second = 1 where 14<invest_number and invest_number<22
    end
    if exists(select top 1 * from kk where col4 like '%A%')
    begin
    update mm set invest_number = 0,invest_second = 1 where 21<invest_number and invest_number<29
    end
    if exists(select top 1 * from kk where col5 like '%A%')
    begin
    update mm set invest_number = 0,invest_second = 1 where 28<invest_number and invest_number<36
    end
    if exists(select top 1 * from kk where col6 like '%A%')
    begin
    update mm set invest_number = 0,invest_second = 1 where 35<invest_number and invest_number<43
    end
    go
    exec up_replace
    --删除测试
    drop table mm,kk
      

  6.   

    to 楼主:
      没注意“表KK 中六个字段的值都是唯一的”。。
      你再改改吧:)
      

  7.   

    to: feeling_68(随风) 
    可能是我没表达清楚吧,那上面的invest_number,invest_second的值是一条记录的:
    数据表中有两个字段invest_number,invest_secondid   invest_number                        invest_second 1   ,1,4,8,12,15,18,22,25,32,39,        ,1.02,2.55,1.36,2.55,1.00,2.03,.....
     2    ,3,6,23,40,.....                     ..................invest_number字段中每条记录的数字最大为42,最多不会超过18个这些值是一条记录不是多条记录的。
      

  8.   

    declare @invest_number varchar(1000)
    declare @invest_second varchar(1000)
    declare @pos_A int   
    declare @len int
    declare @pos intselect @invest_number=invest_number,@invest_second=invest_second from 表 
    select @pos_A=charindex('A',字段1+字段2+字段3+字段4+字段5+字段6) from kk
    set @len=@pos_A*7+1IF @len>1 
    begin
        while @len>0
        begin
            set @pos=charindex(','+cast(@len as varchar(100))+',',invest_number)
            if @pos>0 then
                begin
                   set @invest_number=stuff(@invest_number
                                            ,@pos
                                            ,len(cast(@len as varchar(100)+2),',0,')
                   set @invest_second=stuff(@invest_second
                                            ,@pos
                                            ,len(cast(@len as varchar(100)+2),',1,')
               end 
            set @len=@len-1
        end
    end
    update 表 set  invest_number=@invest_number,invest_second=@invest_second
      

  9.   

    To: lsxaa(小李铅笔刀)
    替换@invest_second的逻辑错了
      

  10.   

    谢谢icknay(icknay) 提醒,我还以为是楼主呢  替换@invest_second 那我想不出更好的办法了,这样吧
    declare @i int,@temp_str varchar(1000)
    set @i=1
    while @i<=len(@Invest_second)
    begin
        if substring(@Invest_second,@i,1)=',' 
        set @pos=@pos-1
        
        if @pos>0 or @pos<-1 
           set @temp_str=@temp_str+substring(@Invest_second,@i,1)
        else
           begin 
             if @post=-1
             set @temp_str=@temp_str+'1'+substring(@Invest_second,@i,1)
           end 
    end
    set @invest_second=@temp_str  谁有更好的办法,可以贴出来  大家互相交流,我只能想到这儿了
      

  11.   

    说实话,我也想不出更好的方法来替换@invest_second,因为SQL不支持Array,自己可以写一个,但是要用到临时表,这样会增加sql的资源开销。其实用asp实现,在速度不见得比sql执行要慢,而且还要简单很多啊。
      

  12.   

    嗯,既然你们说用asp速度不会慢的话,那还是用asp来做好了,毕竟对它熟悉点,以后改动也不会太麻烦再此谢过楼上的各位兄弟了,结贴了!!