1.XH2015000001 XH2015000002。。。。。(字母+年份+自增长)
先定义标量值函数
create Function F_GetID()
returns nvarchar(50)AS
Begin
Declare @S nvarchar(50)
Select @S= 'XH'+CONVERT(varchar,YEAR(getdate()))+Right(1000000+IsNull(Right(Max(StudentGuID),6),0)+1,6) From StudentInfo
return @S
End
Go
再创建表:
CREATE table StudentInfo(
StudentGuID nvarchar(50) NOT NulL Default dbo.F_GetID(),
Studentname varchar(20) NOT NulL,
Constraint PK_Student_ID Primary key(StudentGuID)
)
GO
测试:
Insert StudentInfo(Studentname ) values('aa')
Insert StudentInfo(Studentname ) values( 'bb')
Select * From StudentInfo
.获得XH20150418000001 XH20150418000002。。。。。(字母+年份日月+自增长)
a.创建标量值函数
create Function F_GetCustomerGuID()
returns nvarchar(50)
AS
Begin
Declare @S nvarchar(50)
Select @S= 'XH'+CONVERT(varchar,getdate(),112)+Right(1000000+IsNull(Right(Max(CustomerID),6) From Customer
return @S
End
Go
b.创建表
CREATE table Customer(
CustomerID nvarchar(50) NOT NulL Default dbo.F_GetCustomerGuID(),
Customername varchar(20) NOT NulL,
Constraint PK_Custr_ID Primary key(CustomerID)
)
GO
c.测试:
Insert Customer(Customername ) values('aa')
Insert Customer(Customername ) values( 'bb')
Insert Customer(Customername ) values( 'cc' )
Select * From Customer
下附带一些sqlServer时间格式:
Select CONVERT(varchar(100),GETDATE(),23): 2006-05-16 Select CONVERT(varchar(100),0): 05 16 2006 10:57AM Select CONVERT(varchar(100),1): 05/16/06 Select CONVERT(varchar(100),2): 06.05.16 Select CONVERT(varchar(100),3): 16/05/06 Select CONVERT(varchar(100),4): 16.05.06 Select CONVERT(varchar(100),5): 16-05-06 Select CONVERT(varchar(100),6): 16 05 06 Select CONVERT(varchar(100),7): 05 16,06 Select CONVERT(varchar(100),8): 10:57:46 Select CONVERT(varchar(100),9): 05 16 2006 10:57:46:827AM Select CONVERT(varchar(100),10): 05-16-06 Select CONVERT(varchar(100),11): 06/05/16 Select CONVERT(varchar(100),12): 060516 Select CONVERT(varchar(100),13): 16 05 2006 10:57:46:937 Select CONVERT(varchar(100),14): 10:57:46:967 Select CONVERT(varchar(100),20): 2006-05-16 10:57:47 Select CONVERT(varchar(100),21): 2006-05-16 10:57:47.157 Select CONVERT(varchar(100),22): 05/16/06 10:57:47 AM Select CONVERT(varchar(100),24): 10:57:47 Select CONVERT(varchar(100),25): 2006-05-16 10:57:47.250 Select CONVERT(varchar(100),100): 05 16 2006 10:57AM Select CONVERT(varchar(100),101): 05/16/2006 Select CONVERT(varchar(100),102): 2006.05.16 Select CONVERT(varchar(100),103): 16/05/2006 Select CONVERT(varchar(100),104): 16.05.2006 Select CONVERT(varchar(100),105): 16-05-2006 Select CONVERT(varchar(100),106): 16 05 2006 Select CONVERT(varchar(100),107): 05 16,2006 Select CONVERT(varchar(100),108): 10:57:49 Select CONVERT(varchar(100),109): 05 16 2006 10:57:49:437AM Select CONVERT(varchar(100),110): 05-16-2006 Select CONVERT(varchar(100),111): 2006/05/16 Select CONVERT(varchar(100),112): 20060516 Select CONVERT(varchar(100),113): 16 05 2006 10:57:49:513 Select CONVERT(varchar(100),114): 10:57:49:547 Select CONVERT(varchar(100),120): 2006-05-16 10:57:49 Select CONVERT(varchar(100),121): 2006-05-16 10:57:49.700 Select CONVERT(varchar(100),126): 2006-05-16T10:57:49.827 Select CONVERT(varchar(100),130): 18 ???? ?????? 1427 10:57:49:907AM Select CONVERT(varchar(100),131): 18/04/1427 10:57:49:920AM
总结 以上是内存溢出为你收集整理的SQLserver自定义样式主键-函数实现篇全部内容,希望文章能够帮你解决SQLserver自定义样式主键-函数实现篇所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)