CREATE PROCEDURE dbo.GetRepairBillID;1   
@id int output  
  
AS   
    declare @curtime int 
    BEGIN   
    select @curtime = CONVERT(int , GETDATE(), 12) 
    if exists(select * from RepairBill where ID > @curtime *100 and ID < (@curtime*100+99) ) 
        select @id = max(ID) + 1  from RepairBill where ID > @curtime *100 and ID < (@curtime*100+99)  
    else  
        select @id = @curtime*100 + 1  
    ENDSQL语句如上。大体流程是我想获得一个ID 这个ID是当天的日期加2为个数ID。比如2010082701就是2010年8月27日的第一个ID,如果程序调用GetRepairBillID则变成2010082702。现在不知道CONVERT函数可以吧日期变成int类型的吗?还有SQL中有long这个数据类型吗?我使用后报错。最后问下这种存储过程如何调试啊 又加不了断点。
(PS 编译环境为VC6.0 +SYBASE12.5.1)

解决方案 »

  1.   


    declare @s datetime 
    set @s = '2010-08-27'
    select cast(convert(varchar(10),@s,112) as int)+2结果
    20100829加2是为了看到效果。
      

  2.   

    CREATE PROCEDURE dbo.GetRepairBillID 
    @id int output   
    AS   
      BEGIN   
        set @id=
        isnull(
        (select max(id)+1 from RepairBill where datediff(dd,left(id,8),getdate())=0),
        cast(convert(varchar(8),getdate(),112) as int)*100+1
        )
      END
      

  3.   

    必须先转为字符串再转为int,
    long 对应bigint
    CREATE PROCEDURE dbo.GetRepairBillID;1   
    --declare 
    @id int output   
       
    AS     declare @curtime int  
      BEGIN   
      select @curtime = convert(int,CONVERT(varchar(6), GETDATE(), 12))
      if exists(select * from RepairBill where ID > @curtime *100 and ID < (@curtime*100+99) )  
      select @id = max(ID) + 1 from RepairBill where ID > @curtime *100 and ID < (@curtime*100+99)   
      else   
      select @id = @curtime*100 + 1   
      END