通过SQL自动添加流水号------
--自动流水号为纯数字的情况,如 201104190005
------
declare
@x
int
,
@y
char
(8)
select
@x=
COUNT
(
no
)
from
TestNo
where
CAST
(DT
as
date
)=
CAST
(GETDATE()
as
date
)
if(@x=0)
begin
select
@y=
convert
(
char
(8),getdate(),112)
insert
TestNo(
No
,DT)
values
(
@y+
'0001'
,GETDATE())
end
else
insert
TestNo(
No
,DT)
values
(
(
select
MAX
(
No
)+1
from
TestNo
where
CAST
(DT
as
date
)=
CAST
(GETDATE()
as
date
))
,GETDATE())
select
*
from
TestNo
------
--自动流水号为字符的情况,如 WT201104190005
------
declare
@x1
int
,
@y1
char
(8)
select
@x1=
COUNT
(CharNo)
from
TestNo
where
CAST
(DT
as
date
)=
CAST
(GETDATE()
as
date
)
if(@x1=0)
begin
select
@y1=
convert
(
char
(8),getdate(),112)
insert
TestNo(CharNo,DT)
values
(
'WT'
+@y1+
'0001'
,GETDATE())
end
else
begin
insert
TestNo(CharNo,DT)
values
(
'WT'
+
cast
((
select
MAX
(
CAST
(
SUBSTRING
(charno,3,12)
as
bigint
))+1
from
TestNo
where
CAST
(DT
as
date
)=
CAST
(GETDATE()
as
date
))
as
varchar
(50))
,GETDATE())
end
select
*
from
TestNo
--------------------------------------------------------