就是我有一个字段是aa 这个字段是nvarchar型的 默认值是abc200300
每次插入一条数据 这个字段就变成abc200301 abc200302 abc200303...
------解决方案--------------------
create table wos
(aa nvarchar(20) constraint df_aa default('abc200300')
)
create proc sp_addwos
as
begin
set nocount on
declare @ta int,@maxaa nvarchar(20)
select @maxaa=max(aa) from wos
if @maxaa is null
begin
insert into wos(aa) values('abc200301')
end
else
begin
select @ta=cast(right(@maxaa,2) as int)+1
insert into wos(aa) select left(@maxaa,7)+replicate('0',2-len(@ta))+rtrim(@ta)
end
end
-- 测试
exec sp_addwos
select aa from wos
/*
aa
--------------------
abc200301
(1 row(s) affected)
*/
exec sp_addwos
go 10
select aa from wos
/*
aa
--------------------
abc200301
abc200302
abc200303
abc200304
abc200305
abc200306
abc200307
abc200308
abc200309
abc200310
abc200311
(11 row(s) affected)
*/
------解决方案--------------------
CREATE TABLE [dbo].[maxbh](
[biaoshi] [char](3) COLLATE Chinese_PRC_CI_AS NOT NULL,
[maxbh] [char](11) COLLATE Chinese_PRC_CI_AS NULL,
[recnum] [int] NULL,
[mkbh] [char](2) COLLATE Chinese_PRC_CI_AS NULL
CONSTRAINT [PK_maxbh] PRIMARY KEY CLUSTERED
(
[biaoshi] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
--exec Get_maxbh 'aaa',1
ALTER PROCEDURE [dbo].[Get_maxbh]
@biaoshi varchar(3), --单据类型标识
@addflag integer,
@maxbh varchar(11)=NULL output --直接返回编号
AS
--内部变量声明
declare @recnum integer,
@rowcount integer
set @recnum=0
/*非事务内容执行*/
--每一过程拥有一唯一区界号,过程内的异常编号在此基础上增加
declare @errcode integer
set @errcode=1800
declare @return integer
set @return=0
--启动事务处理
declare @tran_point int --控制事务嵌套
set @tran_point=@@trancount --保存事务点
if @tran_point=0
begin tran tran_Get_maxbh
else
save tran tran_Get_maxbh
set @addflag =1
if @addflag=1 or @addflag=3