1、使用C#自动生成四位凭证号
--创建函数实现数字部分的自增
CREATE FUNCTION f_NextXH()
RETURNS nvarchar(12)
AS
BEGIN
RETURN(SELECT 'XH'+CONVERT(varchar(4),year(GETDATE()))+RIGHT(1000001+ISNULL(RIGHT(MAX(StudentGuid),6),0),6) FROM StudentInfo WITH(XLOCK,PAGLOCK))
END
GO
--在表中应用函数
CREATE TABLE StudentInfo(
StudentGuid nvarchar(50) PRIMARY KEY DEFAULT dbo.f_NextXH(),
StudentName nvarchar(50) NOT NULL,
Sex nvarchar(50) NOT NULL,
[Identity] nvarchar(50) NOT NULL,
Introduction nvarchar(50) NOT NULL,
)
再例如:
--创建函数实现数字部分的自增
CREATE FUNCTION f_NextCJ()
RETURNS nvarchar(15)
AS
BEGIN
RETURN(SELECT 'CJ'+CONVERT(varchar(100), GETDATE(), 112)+RIGHT(10001+ISNULL(RIGHT(MAX(ScoreGuid),5),0),5) FROM ScoreInfo WITH(XLOCK,PAGLOCK))
END
GO
--在表中应用函数
CREATE TABLE [ScoreInfo](
ScoreGuid nvarchar(50) PRIMARY KEY DEFAULT dbo.f_NextCJ(),
StudentGuid nvarchar(50) NOT NULL,
[Year] nvarchar(50) NOT NULL,
[Semester] nvarchar(50) NOT NULL,
[Subject] nvarchar(50) NOT NULL,
[Score] numeric(8, 2) NOT NULL,
[ScoreTime] datetime NOT NULL,)