日期:2014-05-16 浏览次数:20539 次
create table test(编号 nvarchar(20))
DECLARE @Sequence varchar(4)
SET @Sequence = RIGHT(
(
SELECT MAX(编号)
FROM test
WHERE 编号 LIKE 'ABCDE'+CONVERT (varchar, DatePart(YEAR,GETDATE())) + '%'
),
4
)
IF @Sequence IS NOT NULL
BEGIN
insert test values('ABCDE'+CONVERT (varchar, DatePart(YEAR,GETDATE()))+ right(cast(power(10,4) as varchar)+convert(int,@Sequence)+1,4))
END
ELSE
BEGIN
insert test values('ABCDE'+CONVERT (varchar, DatePart(YEAR,GETDATE()))+'0001')
END
select * from test
/*
ABCDE20140001
ABCDE20140002
ABCDE20140003
*/
--建表
create table test(编号 nvarchar(20))
--创建函数 返回0001
set ansi_nulls on
GO
set quoted_identifier on
GO
create function dbo.testcreateid()
returns varchar(15)
as
begin
declare @Sequence varchar(4)
set @Sequence = RIGHT(
(
select MAX(编号)
from test
where 编号 LIKE 'ABCDE'+CONVERT (varchar, DatePart(YEAR,GETDATE())) + '%'
),
4
)
if @Sequence IS NOT NULL
beg