在sql中写了这样一个自定义函数,这个函数式用来生成流水账号的,账号的格式是这样的:S+日期字符串+000,下一个数字+001;每天的流水账号从000开始,函数的创建代码如下:
CREATE FUNCTION [dbo].[GetSingleID]()
RETURNS Varchar(12)
AS
BEGIN
-- Declare the return variable here
DECLARE @Temp varchar(12)
-- Add the T-SQL statements to compute the return value here
select @Temp =
'S'+ convert(varchar(8),getdate(),112)+ Right(10000+Convert(int,Right(ISNULL(Max(ExaminationID),0),4)+1),4)
From T_SinglePatient
Where Left(ExaminationID,8) = convert(varchar(8),getdate(),112)
-- Return the result of the function
RETURN @Temp
END
我想在每次在一个表中插入一条记录的时候在ExaminationID中自动插入该函数,也就是将这个字段的默认值设置为GetSingleID();如下图所示:
先不说这个函数是不是能实现我之前