address  
-----------------------  
01      四川省  
02      北京市  
03      天津市  
……  
-----------------------  
subject  
-----------------------  
01          施工员  
02          预算员  
03          资料员  
……  
-----------------------  
kd
-----------------------
id   kd
11   1号考场
12   2号考场
13   3号考场
14   4号考场
15   5号考场
16   6号考场
-----------------------
enter  
-----------------------  
name         subject  address   kd    examnumber  
王1          施工员    天津市   11    03110100001  
王2          预算员    北京市   12    02120200001  
王3          预算员    北京市   12    02120200002  
王4          施工员    四川省   13    02130100001  
王5          施工员    四川省   13    02130100002  
王6          资料员    天津市   14    03140300001  
王7          资料员    天津市   14    03140300002  
王8          预算员    天津市   15    03150200001  
王9          预算员    天津市   15    03150200002  
王10         预算员    四川省   16    02160200001
------------------------  
现在表里面的examnumber  全是空的。  
原来是没有考点,只是: 地址+科目+5位排序号 
代码如下:
update e
set  examnumber =a.addressid+s.subjectid+right(100000+(select count(*) from enter where address=e.address and subject=e.subject and name<=e.name),5)
from     enter e,gw_subject s,gw_address a
where    e.subject= s.subject and e.address=a.addressname但是现在加入了考场2位数字。
求高手帮忙修改一下。
应该怎样来修改这条语句才能正确的显示。
达到要求:地址+考场+科目+5位排序号。判断级别是从左--右。

解决方案 »

  1.   

    update e
    set  examnumber =a.addressid+e.kd+s.subjectid+right(100000+(select count(*) from enter where address=e.address and subject=e.subject and kd=e.kd and name<=e.name),5)
    from     enter e,gw_subject s,gw_address a
    where    e.subject= s.subject and e.address=a.addressname
      

  2.   

    update e
    set  examnumber =a.addressid+cast(e.kd as varchar)+s.subjectid+right(100000+(select count(*) from enter where address=e.address and subject=e.subject and kd=e.kd and name<=e.name),5)
    from     enter e,gw_subject s,gw_address a
    where    e.subject= s.subject and e.address=a.addressname
      

  3.   

    查看下examnumber子段定义的长度,可能不足11位
      

  4.   

    而且我在设置考点数据的时候设置成varchar。也合不起来。
      

  5.   

    ---- 如果你的kd是Int类型的+起来会出问题,需要转换成字符型才可以相加
    ----Try:
    update e
    set  examnumber =a.addressid+Cast(e.kd As Varchar)+s.subjectid+right(100000+(select count(*) from enter where address=e.address and subject=e.subject and kd=e.kd and name<=e.name),5)
    from  enter e,gw_subject s,gw_address a
    where e.subject= s.subject and e.address=a.addressname
      

  6.   

    where 后面要加一句
    and kd=e.kd 才能开始排序。不然后面全是5个0。
    还有这么写以后似乎不按照期望的顺序排列。
    地址+考场+科目+5位排序号。判断级别是从左--右
      

  7.   

    update e
    set  examnumber =a.addressid+e.kd+s.subjectid+right(100000+(select count(*) from enter where address=e.address and subject=e.subject and name<=e.name),5)
    from     enter e,gw_subject s,gw_address a
    where    e.subject= s.subject and e.address=a.addressname
      

  8.   

    似乎该加的都加了排序可以在select的时候order by examnumber