--随机数生成有一个麻烦的地方就是 随机数不能在自定义方法里面使用。所以只能放到一个视图里面去。 create view view_rand as select rand() as Srand ; create function GenerateOrderID() returns varchar(50) as begin declare @OrderID varchar(50) declare @Mrand int select @Mrand=cast(Srand*10000 as int) from view_rand declare @Lastvarchar varchar(5) select @Lastvarchar= right('0000'+cast(@Mrand as varchar(4)),4) --select @Lastvarchar set @OrderID=replace(replace(replace(CONVERT(varchar, getdate(), 120 ),'-',''),' ',''),':','')+ @Lastvarchar return @OrderID end