日期:2014-05-19  浏览次数:20413 次

一个sql语句
如何用一个SQL语句产生一系列流水编号,格式如下:
20070327-0001
20070327-0002
20070327-0003
20070327-0004
20070327-0005
......
20070328-0001
20070328-0002
......


Thanks!

------解决方案--------------------
select replace(convert(char(10),getdate(),120), '- ', ' ')+ '- '+right( '0000 '+ cast(isnull(max(cast(right(id,4) as int)),0)+1 as varchar),4) from tb where charindex(replace(convert(char(10),getdate(),120), '- ', ' ')+ '- ',id)> 0
------解决方案--------------------
WHILE 循環
------解决方案--------------------
SQL code
select  replace(convert(char(10),getdate(),120), '- ', ' ')+'-'+right(10000+ROW_NUMBER() over(order by a.id),4)
from sysobjects a,sysobjects b
/*
------------------------------------------------------
2008-10-27-0001
2008-10-27-0002
2008-10-27-0003
2008-10-27-0004
2008-10-27-0005
2008-10-27-0006
2008-10-27-0007
2008-10-27-0008
2008-10-27-0009
2008-10-27-0010
2008-10-27-0011
2008-10-27-0012
2008-10-27-0013
2008-10-27-0014
2008-10-27-0015
2008-10-27-0016
2008-10-27-0017
2008-10-27-0018
2008-10-27-0019
2008-10-27-0020
2008-10-27-0021
2008-10-27-0022
2008-10-27-0023
2008-10-27-0024
2008-10-27-0025
2008-10-27-0026
2008-10-27-0027
2008-10-27-0028
2008-10-27-0029
2008-10-27-0030
2008-10-27-0031
2008-10-27-0032
2008-10-27-0033
2008-10-27-0034
2008-10-27-0035
2008-10-27-0036
2008-10-27-0037
2008-10-27-0038
2008-10-27-0039
2008-10-27-0040
2008-10-27-0041
2008-10-27-0042
2008-10-27-0043
2008-10-27-0044
2008-10-27-0045
2008-10-27-0046
2008-10-27-0047
2008-10-27-0048
2008-10-27-0049
2008-10-27-0050
2008-10-27-0051
2008-10-27-0052
2008-10-27-0053
2008-10-27-0054
2008-10-27-0055
2008-10-27-0056
2008-10-27-0057
2008-10-27-0058
2008-10-27-0059
2008-10-27-0060
2008-10-27-0061
2008-10-27-0062
2008-10-27-0063
2008-10-27-0064
2008-10-27-0065
2008-10-27-0066
2008-10-27-0067
2008-10-27-0068
2008-10-27-0069
2008-10-27-0070
2008-10-27-0071
2008-10-27-0072
2008-10-27-0073
2008-10-27-0074
2008-10-27-0075
2008-10-27-0076
2008-10-27-0077
2008-10-27-0078
2008-10-27-0079
2008-10-27-0080
2008-10-27-0081
2008-10-27-0082
2008-10-27-0083
2008-10-27-0084
2008-10-27-0085
2008-10-27-0086
2008-10-27-0087
2008-10-27-0088
2008-10-27-0089
2008-10-27-0090
2008-10-27-0091
2008-10-27-0092
2008-10-27-0093
2008-10-27-0094
2008-10-27-0095
2008-10-27-0096
2008-10-27-0097
2008-10-27-0098
2008-10-27-0099
2008-10-27-0100
2008-10-27-0101
2008-10-27-0102
2008-10-27-0103
2008-10-27-0104
2008-10-27-0105
2008-10-27-0106
2008-10-27-0107
2008-10-27-0108
2008-10-27-0109
2008-10-27-0110
2008-10-27-0111
2008-10-27-0112
2008-10-27-0113
2008-10-27-0114
2008-10-27-0115
2008-10-27-0116
2008-10-27-0117
2008-10-27-0118
2008-10-27-0119
2008-10-27-0120
2008-10-27-0121
2008-10-27-0122
2008-10-27-0123
2008-10-27-0124
2008-10-27-0125
2008-10-27-0126
2008-10-27-0127
2008-10-27-0128
2008-10-27-0129
2008-10-27-0130
2008-10-27-0131
2008-10-27-0132
2008-10-27-0133
2008-10-27-0134
2008-10-27-0135
2008-10-27-0136
2008-10-27-0137
2008-10-27-0138
2008-10-27-0139
2008-10-27-0140
2008-10-27-0141
2008-10-27-0142
2008-10-27-0143
2008-10-27-0144
2008-10-27-0145
2008-10-27-0146
2008-10-27-0147
2008-10-27-0148
2008-10-27-0149
2008-10-27-0150
2008-10-27-0151
2008-10-27-0152
2008-10-27-0153
2008-10-27-0154
2008-10-27-0155
2008-10-27-0156
2008-10-27-0157
2008-10-27-0158
2008-10-27-0159
2008-10-27-0160
2008-10-27-0161
2008-10-27-0162
2008-10-27-0163
2008-10-27-0164
2008-10-27-0165
2008-10-27-0166
2008-10-27-0167
2008-10-27-0168
2008-10-27-0169
2008-10-27-0170
2008-10-27-0171
2008-10-27-0172
2008-10-27-0173
2008-10-27-0174
2008-10-27-0175
2008-10-27-0176
2008-10-27-0177
2008-10-27-0178
2008-10-27-0179
2008-10-27-0180
2008-10-27-0181
2008-10-27-0182
2008-10-27-0183
2008-10-27-0184
2008-10-27-0185
2008-10-27-0186
2008-10-27-0187
2008-10-27-018