专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > SQL

存储过程中以日期加流水号做自动id有关问题

发布时间:2010-05-20 14:01:29 文章来源:www.iduyao.cn 采编人员:星星草
存储过程中以日期加流水号做自动id问题
sql里面User表里面有个userid字段
我要userid是这种结构的
U+日期+流水号(U2011060316040001)
C# code

declare @userid varchar(50)
declare @year char(4) 
declare @mm char(2)
declare @hh char(2)
declare @timeStr char(10)
set @year=cast(year(getdate())as varchar(4))
set @mm=cast(month(getdate())as varchar(2))
set @hh=cast(day(getdate())as varchar(2))
if @mm<10
begin
set @mm='0'+@mm
end
set @timeStr=@year+@mm+@hh

    Declare @MaxId VarChar(50),

          @MaxNum int

  Select @MaxId=Max(eventID) From Users where Userid Like @userid +'%'

  If @MaxId Is null

     Set @MaxNum='0';

  Else

     Set @MaxNum=Cast(replace(@MaxId,@EventId,'') as int)

  Set @MaxNum=@MaxNum+1

  Set @MaxId=Cast(@MaxNum as VarChar(10))

  While(Len(@MaxId)<4)

      Set @MaxId='000'+@MaxId
      set @MaxId=@timeStr+@MaxId
      set @Userid = 'u'+@MaxId




这样写不行,有重复的,
还有getdate()只能得到2011-06-03而不能得到时分秒 这个要怎么处理呢
请各位高手帮忙看看。谢谢!
或者谁有这方面存储过程贴出来学习学习.

------解决方案--------------------
还有getdate()只能得到2011-06-03而不能得到时分秒 这个要怎么处理呢
SELECT GETUTCDATE()
------解决方案--------------------
怎么会得不到呢?什么数据库?
(dba)> select 'U' + dateformat(getdate(), 'yyyymmddHHNNSS001');
'U'+dateformat(getdate(),'yyyymmddHHNNSS001')
---------------------------------
U20110603170055001

------解决方案--------------------
getdate()可以得到日期、时间
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: