要实现一个单号分发的功能。
例子:小王手中有1万条表单号00001-10000存在表tb_code中,现在小王可以将说中单号分发给其他部门
分发的功能输入如:
分发起始单号:_______ 分发数量_________    
正常情况下10000条数据是连续的,但是会出现断号的情况,需求要求用户可以看到断号。 比如00001-10000中数据有 00009,00010,01091,08951,08999是不存在于这00001-10000中的 用户要能看到要是用循环 那速度太慢啦。
用什么方法可以将 你录入的 单号与数据的单号进行比对抽出不在表中的数据-----------------------declare @str nvarchar(200)
set @str=""
while @beginCode<=@endCode          --输入的起始单号@beginCode,截止单号@endCode
begin
  if((select count(*) from tb_code where code=@beginCode)=0) --循环查询表tb_code中不存在的表单
   begin
       set @str=@str+@beginCode
   end
end
print @str
实在没办法 这样数据太慢!
有人能帮忙出出好注意吗?
分用完了,也是想混口饭吃。大家帮下

解决方案 »

  1.   

    declare @TmpTable table (id int)
    declare @str nvarchar(200)
    set @str=""
    while @beginCode<=@endCode          --输入的起始单号@beginCode,截止单号@endCode
    begin
    --此处好像有个批量一次插入的方法,想不起来了
       insert into @TmpTable(id) select convert(int,@beginCode)
    endselect code from tb_code where convert(int,code)>=@beginCode and convert(int,code)<=@endCode and convert(int,code) not in (select id from @TmpTable)
      

  2.   

    你的意思是弄个临时表来存储tb_code 然后3表操作吗?
      

  3.   

    去SQL Server论坛去问吧。告诉你一个基本判断标准:只要用一条select语句就可以打印出所有断号区间,只需要扫猫一次,而且仅仅是一条select语句!
      

  4.   

    google的结果.
      SQL顺序列找出断号 收藏
    select id from infoid
    -----------
    1
    2
    3
    5
    6
    7
    8
    10
    11
    12
    15(11 行受影响)方法一:select (select max(id)+1 from Info where id<a.id) as beginId,(id-1) as endId
    from Info a 
    where 
    a.id>(select max(id)+1 from Info where id<a.id)beginId     endId
    ----------- -----------
    4           4
    9           9
    13         14(3 行受影响)方法二:select beginId,(select min(id)-1 from info where id > beginId) as endId 
    from (  
    select id+1 as beginId from info where id+1 not in (select id from info) and id < (select max(id) from info)  
    ) as tbeginId     endId
    ----------- -----------
    4           4
    9           9
    13          14(3 行受影响)说明:1、查找结果的两列是断号的区间,如果beginId=endId,则表示缺少该号码,否则表示缺少beginId ~ endId;2、如果号码1不存在,区间1 ~ select min(id)-1 from info 将无法找出