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()可以得到日期、时间