sql 用户定义函数自动生成自增长ID
1
---流动人员号自动编号函数,由区域代码-年月日-序列号组成的 每日新序号
2
---geovindu@163.com 涂聚文 www.dusystem.com
3
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetDayFloatingID]') and xtype in (N'FN', N'IF', N'TF'))
4
drop function [dbo].[GetDayFloatingID]
5
GO
6
CREATE FUNCTION GetDayFloatingID(@headStr nvarchar(10),@date datetime)
7
RETURNS nvarchar(50)
8
BEGIN
9
declare @oid2 nvarchar(50)
10
declare @oid nvarchar(50)
11
declare @day nvarchar(2)
12
declare @month nvarchar(2)
13
declare @year nvarchar(4)
14
declare @ym nvarchar(8)
15
set @day=day(@date)
16
if len(@day)=1
17
set @day='0'+@day --使日为两位长
18
set @month=month(@date)
19
if len(@month)=1
20
set @month='0'+@month --使月为两位长
21
--set @year=right(convert(nvarchar,year(@date)),2)
22
set @year=convert(nvarchar,year(@date))
23
set @ym=@year+@month+@day --组成年月日字符
24
25
--格式BJ200808200001
26
if exists(select * from Populations)
27
begin
28
select top 1 @oid2=FloatingID from Populations order by FloatingID desc --获取最后一条的编号,一定要有id,并且自动生成的,倒排序
29
end
30
else
31
begin
32
set @oid2=@headStr+@ym+'00000' --没有记录是默认为今天
33
end
34
35
--流水号不是本月的,重新开始一个新的流水号
36
if convert(nvarchar,left(@oid2,6))<>@headStr+@ym
37
begin
38
--用本月的年月号开始
39
set @oid2=@headStr+@ym+'00000'
40
end
41
42
declare @str nvarchar(50) --临时流水号
43
44
set @str=convert(nvarchar,(convert(int,right(@oid2,4))+1)) --流水号加一
45
while (5-len(@str)>0)
46
begin
47
set @str='0'+@str
48
end
49
set @oid2=@headStr+@ym+@str
50
--print @oid2
51
52
--如果该流水号已经存在,则重新获取
53
while exists(select * from Populations where FloatingID=@oid2)
54
begin
55
56
set @str=convert(nvarchar,(convert(int,right(@oid2,5))+1)) --流水号加一
57
while (5-len(@str)>0)
58
begin
59
set @str='0'+@str
60
end
61
set @oid2=@headStr+@ym+@str
62
-- print @oid2
63
end
64
65
set @oid=convert(nvarchar,@oid2)
66
--print 'UL'+convert(nvarchar,year(getdate()))+convert(nvarchar,month(getdate()))+@str
67
RETURN @oid
68
END
69
GO
70
--测试
71
DECLARE @S varchar(30)
72
select @S=dbo.GetDayFloatingID('02',getdate())
73
select @s as '流动编号'
74
75
--按月自动增长
76
--如果当月,没有记录号,开始创建,如果有,在此基此上加1,以月新增长序列号
77
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetMonthFloatingID]') and xtype in (N'FN', N'IF', N'TF'))
78
drop function [dbo].[GetMonthFloatingID]
79
GO
80
CREATE FUNCTION GetMonthFloatingID(@headStr nvarchar(10),@idate datetime)
81
RETURNS nvarchar(50)
82
BEGIN
83
--@idate datetime,@headStr varchar(10),
84
declare @fid varchar(100),@dat varchar(20),@ym varchar(20),@str nvarchar(50),@olde varchar(30),@y varchar(10),@m varchar(10),@d varchar(10)
85
--set @headStr='01'
86
--set @idate=cast('2009-04-3' as datetime) --getdate()
87
--set @ym=cast(getdate() as varchar(30))
88
--找到当月最大的值
89
select top 1 @fid=FloatingID from Populations
90
where month(cast(substring(FloatingID,3,8) as datetime))=month(@idate) and year(cast(substring(FloatingID,3,8) as datetime))=year(@idate)
91
order by substring(FloatingID,11,5) desc
92
--select @fid=MAX(substring(FloatingID,11,5)) from Populations
93
-- where month(getdate())
94
set @y=cast(year(@idate) as varchar(10)) --
95
set @m=cast(month(@idate) as varchar(10))
96
if len(@m)=1
97
set @m='0'+@m
98
set @d=cast(day(@idate) as varchar(10))
99
if len(@d)=1
100
set @d='0'+@d
101
set @ym=@y+@m+@d
102
if @fid<>''
103
--加一
104
begin
105
106
select @str=convert(nvarchar,(convert(int,right(@fid,5))+1))
107
while (5-len(@str)>0)
108
begin
109
set @str='0'+@str
110
end
111
set @olde=@headStr+@ym+@str
112
--select @olde
113
end
114
else
115
begin
116
set @olde=@headStr+@ym+'00000'
117
--select @olde
118
--print '2'
119
end
120
set @olde=convert(nvarchar,@olde)
121
--print 'UL'+convert(nvarchar,year(getdate()))+convert(nvarchar,month(getdate()))+@str
122
RETURN @olde
123
end
124
GO
125
--测试
126
select dbo.GetMonthFloatingID('09',getdate())
127
128
---2009-03-04 涂聚文 geovindu@163.com

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!