表tab有一字段a,不是自增字段,数据构成为:2009040001,前六位为日期年月,后4位为编号
插入数据时,先通过年月查找是否有该年月的编号,有则增加1,例如2009040002,没有则创建新的编号例如2009050001,请问如何避免多用户同时插入时值重复问题
数据库为sql server 2005

解决方案 »

  1.   


    我想写成这样不知道可不可以(伪代码)
    事务{
    lock(表)
    查询编号,并计算插入编号
    插入数据
    unlock(表)
    }在sqlserver2005不知道锁表怎么锁呢
      

  2.   

    你在插入时才去获取号码,这样不会重复,大致如下:
    下面代码是获取INT型,你需要前后截取字符串,然后比较.
    --如果想进行断号处理
    --以下三段,存储过程一样,只是显示不同数据时的情况.
    --这是没有数据时.
    create table tb(id int)
    gocreate proc my_proc @id int OUTPUT
    as
    begin
      if not exists (select 1 from tb)
         set @id = 1
      else
         begin
           select @id = min(m.px) + 1 from
           (select * , px = (select count(1) from tb where id < t.id) + 1 from tb t) m,
           (select * , px = (select count(1) from tb where id < t.id) + 1 from tb t) n
           where m.id <> n.id - 1 and m.px = n.px - 1
         if @id is null
            select @id = max(id) + 1 from tb
         end
       
    end
    godeclare @id as intexec my_proc @id outputselect @iddrop table tb
    drop proc my_proc/*
                
    ----------- 
    1(所影响的行数为 1 行)
    */--这是存在断号时.create table tb(id int)
    insert into tb values(1)
    insert into tb values(3)gocreate proc my_proc @id int OUTPUT
    as
    begin
      if not exists (select 1 from tb)
         set @id = 1
      else
         begin
           select @id = min(m.px) + 1 from
           (select * , px = (select count(1) from tb where id < t.id) + 1 from tb t) m,
           (select * , px = (select count(1) from tb where id < t.id) + 1 from tb t) n
           where m.id <> n.id - 1 and m.px = n.px - 1
         if @id is null
            select @id = max(id) + 1 from tb
         end
       
    end
    godeclare @id as intexec my_proc @id outputselect @iddrop table tb
    drop proc my_proc/*
                
    ----------- 
    2
    (所影响的行数为 1 行)
    */--这是不存在断号时.
    create table tb(id int)
    insert into tb values(1)
    insert into tb values(2)gocreate proc my_proc @id int OUTPUT
    as
    begin
      if not exists (select 1 from tb)
         set @id = 1
      else
         begin
           select @id = min(m.px) + 1 from
           (select * , px = (select count(1) from tb where id < t.id) + 1 from tb t) m,
           (select * , px = (select count(1) from tb where id < t.id) + 1 from tb t) n
           where m.id <> n.id - 1 and m.px = n.px - 1
         if @id is null
            select @id = max(id) + 1 from tb
         end
       
    end
    godeclare @id as intexec my_proc @id outputselect @iddrop table tb
    drop proc my_proc/*
                
    ----------- 
    3
    (所影响的行数为 1 行)
    */
      

  3.   

    贴一段老大的
    --以下代码生成的编号长度为12,前6位为日期信息,格式为YYMMDD,后6位为流水号。
    --创建得到当前日期的视图
    CREATE VIEW v_GetDate
    AS
    SELECT dt=CONVERT(CHAR(6),GETDATE(),12)
    GO--得到新编号的函数
    CREATE FUNCTION f_NextBH()
    RETURNS char(12)
    AS
    BEGIN
        DECLARE @dt CHAR(6)
        SELECT @dt=dt FROM v_GetDate
        RETURN(
            SELECT @dt+RIGHT(1000001+ISNULL(RIGHT(MAX(BH),6),0),6) 
            FROM tb WITH(XLOCK,PAGLOCK)
            WHERE BH like @dt+'%')
    END
    GO--在表中应用函数
    CREATE TABLE tb(
    BH char(12) PRIMARY KEY DEFAULT dbo.f_NextBH(),
    col int)--插入资料
    INSERT tb(col) VALUES(1)
    INSERT tb(col) VALUES(2)
    INSERT tb(col) VALUES(3)
    DELETE tb WHERE col=3
    INSERT tb(col) VALUES(4)
    INSERT tb(BH,col) VALUES(dbo.f_NextBH(),14)--显示结果
    SELECT * FROM tb
    /*--结果
    BH           col 
    ------------------- ----------- 
    050405000001  1
    050405000002  2
    050405000003  4
    050405000004  14
    --*/