规则:先比较表A中productcode的前5位是否与表B中的subcode相同,如果不同,再比较前6位是否与表B中的subcode相同,如果还是不同,则该记录对应的catecode,catename为空,如表A:
orderno,lineno,productcode
1111     1        667CG016
1111     2        667CG010     
2222     1        62AL5142
2222     2        62AL5111
3333     1        6343L031
表B:
catecode,catename,subcode
001        aaa     667CG  
002        bbb     62AL50
003        ccc     62AL51结果:
orderno,lineno,productcode,catecode,catename
1111     1        667CG016    001      aaa
1111     2        667CG010    001      aaa
2222     1        62AL5142    003      ccc
2222     2        62AL5111    003      ccc
3333     1        6343L031    空        空因为可能的数据量比较大,所以在存储过程中能否不使用游标就能实现呢,谢谢

解决方案 »

  1.   

    select * from t1 where left(noid,5) in (select subcode from 表B) or left(noid,6) in (select subcode from 表B)
      

  2.   

    select * from 表A where left(productcode,5) in (select subcode from 表B) or left(productcode,5) in (select subcode from 表B)
      

  3.   

    select *,(select catecode from 表B) catecode,(select catename from 表B) catename
    from 表A where left(productcode,5) in (select subcode from 表B) or left(productcode,5) in (select subcode from 表B)
      

  4.   

    create table ta
    (orderno int,[lineno] int,productcode varchar(10))
    create table tb
    (catecode varchar(3),catename varchar(10),subcode varchar(10))insert into ta 
    select 1111,     1,        '667CG016'
    union all
    select 1111,     2,        '667CG010'     
    union all
    select 2222,     1,        '62AL5142'
    union all
    select 2222,     2,        '62AL5111'
    union all
    select 3333,     1,        '6343L031'insert into tb
    select '001',        'aaa',     '667CG'  
    union all
    select '002',        'bbb',     '62AL50'
    union all
    select '003',        'ccc',     '62AL51'select a.*,b.catecode,b.catename
    from tA a 
    left join tB b
    on substring(a.productcode,1,5)=substring(b.subcode,1,5)
      

  5.   

    orderno     lineno      productcode catecode catename
    ----------- ----------- ----------- -------- ----------
    1111        1           667CG016    001      aaa
    1111        2           667CG010    001      aaa
    2222        1           62AL5142    002      bbb
    2222        1           62AL5142    003      ccc
    2222        2           62AL5111    002      bbb
    2222        2           62AL5111    003      ccc
    3333        1           6343L031    NULL     NULL
      

  6.   

    前5位已经与表B中的subcode不相同了,为什么还比较前6位是否与表B中的subcode相同??
      

  7.   

    哇,一会儿功夫就这么人回答,太谢谢了
    to:fuxia(双子星)
      是这样吗:
      select * from a,b where substring(a.productcode,1,5)=b.subcode or substring (a.productcode,1,6)=b.subcode to:no1francis(天使赶不走)
       请问如果数据量大的话,用in会不会降低效率to:duoshanx(合作双赢)
      前5位已经与表B中的subcode不相同了,为什么还比较前6位是否与表B中的subcode相同??  因为表B中的subcode有可能是5位,也有可能是6位以上,谢谢各位
      

  8.   

    肯定的  因为相当于分了好几次组  性质就跟你用的交叉联接一样   
    推荐
    select * from a join b on substring(a.productcode,1,5)=b.subcode or substring (a.productcode,1,6)=b.subcode 
    便可