--自已做标识列的例子--流水号:日期+当日编号:--创建得到最大id的函数 create function f_getid() returns varchar(12) as begin declare @id varchar(12),@dt varchar(8) select @dt=dt from v_getdate select @id=max(id) from tb where id like @dt+'-%' if @id is null set @id=@dt+'-'+'001' else set @id=@dt+'-'+right('000'+cast(cast(right(@id,3) as int)+1 as varchar),3) return(@id) end go--创建表 create table tb(id varchar(20) default dbo.f_getid() primary key,name varchar(10)) go--创建视图,得到当前日期(因为函数中不能使用getdate()) create view v_getdate as select dt=convert(varchar,getdate(),112)go /*--创建触发器,在删除表中的记录时,自动更新记录的id create trigger t_delete on tb AFTER delete as declare @id int,@mid int select @mid=min(id),@id=@mid-1 from deleted update tb set id=@id,@id=@id+1 where id>@mid --*/ go--插入记录测试 insert into tb(name) values('张三') insert into tb(name) values('张四') insert into tb(name) values('张五') insert into tb(name) values('张六') insert into tb(name) values('张七') insert into tb(name) values('张八') insert into tb(name) values('张九') insert into tb(name) values('张十')--显示插入的结果 select * from tb--删除部分记录 delete from tb where name in('张五','张七','张八','张十')--显示删除后的结果 select * from tbgo --删除环境 drop table tb drop view v_getdate drop function f_getid
select identity(int,1,1) as i_id ,a.* from t_yourtable
--如果是每月从1开始的.--自已做标识列的例子--流水号:年月+当月编号:--创建得到最大id的函数 create function f_getid() returns varchar(12) as begin declare @id varchar(12),@dt varchar(6) select @dt=dt from v_getdate select @id=max(id) from tb where id like @dt+'-%' if @id is null set @id=@dt+'-'+'0001' else set @id=@dt+'-'+right('0000'+cast(cast(right(@id,4) as int)+1 as varchar),4) return(@id) end go--创建表 create table tb(id varchar(20) default dbo.f_getid() primary key,name varchar(10)) go--创建视图,得到当前日期(因为函数中不能使用getdate()) create view v_getdate as select dt=convert(varchar(6),getdate(),112)go--插入记录测试 insert into tb(name) values('张三') insert into tb(name) values('张四') insert into tb(name) values('张五') insert into tb(name) values('张六') insert into tb(name) values('张七') insert into tb(name) values('张八') insert into tb(name) values('张九') insert into tb(name) values('张十')--显示插入的结果 select * from tbgo --删除环境 drop table tb drop view v_getdate drop function f_getid
我举的例子不是每个月编号都从一开始吗? --如果是每月从1开始的.--自已做标识列的例子--流水号:年月+当月编号:--创建得到最大id的函数 create function f_getid() returns varchar(12) as begin declare @id varchar(12),@dt varchar(6) select @dt=dt from v_getdate select @id=max(id) from tb where id like @dt+'-%' if @id is null set @id=@dt+'-'+'0001' else set @id=@dt+'-'+right('0000'+cast(cast(right(@id,4) as int)+1 as varchar),4) return(@id) end go--创建表 create table tb(id varchar(20) default dbo.f_getid() primary key,name varchar(10)) go--创建视图,得到当前日期(因为函数中不能使用getdate()) create view v_getdate as select dt=convert(varchar(6),getdate(),112)go--插入记录测试 insert into tb(name) values('张三') insert into tb(name) values('张四') insert into tb(name) values('张五') insert into tb(name) values('张六') insert into tb(name) values('张七') insert into tb(name) values('张八') insert into tb(name) values('张九') insert into tb(name) values('张十')--显示插入的结果 select * from tbgo --删除环境 drop table tb drop view v_getdate drop function f_getid
eg: P031106001 -- 第一位P表示采购单,031106表示日期,后三位是流水号。 /* 1:创建测试用的表结构 */
create table tablename(pono char(10),b int)
go
/* 2:创建一个得到当前日期的视图,为下面的自字义函数作准备 */
create view vGetdate
as
select getdate() as today
go
/* 3:用自定义函数来得到单号(因自定函数内不能用getdate()来得到当前日期,要用到上面的视图) */
create function getDH()
returns char(10)
As
begin
declare @dh1 char(10),@dh2 char(10)
select @dh1 = max(pono) from tableName
Set @dh1 = IsNull(@dh1,'P000000000')
select @dh2 = Left(@dh1,1) + right(convert(varchar(8),today,112),6) + '001' from vGetdate
if @dh1 >= @dh2
begin
set @dh2 = left(@dh1,7) + right('000'+ cast(cast(right(@dh1,3) as int)+1 as varchar),3)
end
return(@dh2)
end
go
/* 4:在字段默认值中填入 dbo.getdh() */
alter table tablename add constraint df_tablename_1 default(dbo.getdh()) for pono
-/* 5:测试:*/
insert tablename(b) values(1)
insert tablename(b) values(2)
Select * from tablename
-- 测试结果
pono b
---------- -----------
P031115001 1
P031115002 2
总结:此方法运用到了一些小技巧
1:用字段默认值来实现单号自增
2:用自定义函数来得到字段的默认值
3:因在自定义函数中不能用getdate()之类非确定的函数,用视图来得到当前日期
交流]自增号 就看马可的这个帖子吧。。
create function f_getid()
returns varchar(12)
as
begin
declare @id varchar(12),@dt varchar(8)
select @dt=dt from v_getdate
select @id=max(id) from tb where id like @dt+'-%'
if @id is null
set @id=@dt+'-'+'001'
else
set @id=@dt+'-'+right('000'+cast(cast(right(@id,3) as int)+1 as varchar),3)
return(@id)
end
go--创建表
create table tb(id varchar(20) default dbo.f_getid() primary key,name varchar(10))
go--创建视图,得到当前日期(因为函数中不能使用getdate())
create view v_getdate as select dt=convert(varchar,getdate(),112)go
/*--创建触发器,在删除表中的记录时,自动更新记录的id
create trigger t_delete on tb
AFTER delete
as
declare @id int,@mid int
select @mid=min(id),@id=@mid-1 from deleted
update tb set id=@id,@id=@id+1 where id>@mid
--*/
go--插入记录测试
insert into tb(name) values('张三')
insert into tb(name) values('张四')
insert into tb(name) values('张五')
insert into tb(name) values('张六')
insert into tb(name) values('张七')
insert into tb(name) values('张八')
insert into tb(name) values('张九')
insert into tb(name) values('张十')--显示插入的结果
select * from tb--删除部分记录
delete from tb where name in('张五','张七','张八','张十')--显示删除后的结果
select * from tbgo
--删除环境
drop table tb
drop view v_getdate
drop function f_getid
create function f_getid()
returns varchar(12)
as
begin
declare @id varchar(12),@dt varchar(6)
select @dt=dt from v_getdate
select @id=max(id) from tb where id like @dt+'-%'
if @id is null
set @id=@dt+'-'+'0001'
else
set @id=@dt+'-'+right('0000'+cast(cast(right(@id,4) as int)+1 as varchar),4)
return(@id)
end
go--创建表
create table tb(id varchar(20) default dbo.f_getid() primary key,name varchar(10))
go--创建视图,得到当前日期(因为函数中不能使用getdate())
create view v_getdate as select dt=convert(varchar(6),getdate(),112)go--插入记录测试
insert into tb(name) values('张三')
insert into tb(name) values('张四')
insert into tb(name) values('张五')
insert into tb(name) values('张六')
insert into tb(name) values('张七')
insert into tb(name) values('张八')
insert into tb(name) values('张九')
insert into tb(name) values('张十')--显示插入的结果
select * from tbgo
--删除环境
drop table tb
drop view v_getdate
drop function f_getid
--如果是每月从1开始的.--自已做标识列的例子--流水号:年月+当月编号:--创建得到最大id的函数
create function f_getid()
returns varchar(12)
as
begin
declare @id varchar(12),@dt varchar(6)
select @dt=dt from v_getdate
select @id=max(id) from tb where id like @dt+'-%'
if @id is null
set @id=@dt+'-'+'0001'
else
set @id=@dt+'-'+right('0000'+cast(cast(right(@id,4) as int)+1 as varchar),4)
return(@id)
end
go--创建表
create table tb(id varchar(20) default dbo.f_getid() primary key,name varchar(10))
go--创建视图,得到当前日期(因为函数中不能使用getdate())
create view v_getdate as select dt=convert(varchar(6),getdate(),112)go--插入记录测试
insert into tb(name) values('张三')
insert into tb(name) values('张四')
insert into tb(name) values('张五')
insert into tb(name) values('张六')
insert into tb(name) values('张七')
insert into tb(name) values('张八')
insert into tb(name) values('张九')
insert into tb(name) values('张十')--显示插入的结果
select * from tbgo
--删除环境
drop table tb
drop view v_getdate
drop function f_getid