就是自动生成 日期+流水号 的编号
记得看过说用触发器可以自动生成 200806060001这样的编号
请哪位大牛指导一二...不胜感激
记得看过说用触发器可以自动生成 200806060001这样的编号
请哪位大牛指导一二...不胜感激
解决方案 »
- sql 查询语句
- test
- clistctrl 单击行并且获得被点行的内容
- SQL中Union用法
- 数据库 'SD206N_2012年新帐套1_Data' 中提取的逻辑页 (1:160) 属于对象 '0',而非对象 'sysindexes'。
- 数据库空闲连接超时限定
- 请教存储过程中字符串变量问题 在线等
- sqlserver用户名与登录名的关系
- 触发器不能用,提示MSDTC不能用?
- 如果以管理者的身份进入数据库,如何可以得到这个数据库的所有用户?
- 执行DBCC CHECKDB('dbname') ,提示需要在单用户模式下执行,可是sp_dboption 'dbname', 'single user', 'true'
- CAST 或 CONVERT: 为类型 'datetime' 指定的特性无效
--创建得到当前日期的视图
CREATE VIEW v_GetDate
AS
SELECT dt=CONVERT(CHAR(6),GETDATE(),12)
GO--得到新编号的函数
CREATE FUNCTION f_NextBH()
RETURNS char(12)
AS
BEGIN
DECLARE @dt CHAR(6)
SELECT @dt=dt FROM v_GetDate
RETURN(
SELECT @dt+RIGHT(1000001+ISNULL(RIGHT(MAX(BH),6),0),6)
FROM tb WITH(XLOCK,PAGLOCK)
WHERE BH like @dt+'%')
END
GO--在表中应用函数
CREATE TABLE tb(
BH char(12) PRIMARY KEY DEFAULT dbo.f_NextBH(),
col int)--插入资料
INSERT tb(col) VALUES(1)
INSERT tb(col) VALUES(2)
INSERT tb(col) VALUES(3)
DELETE tb WHERE col=3
INSERT tb(col) VALUES(4)
INSERT tb(BH,col) VALUES(dbo.f_NextBH(),14)--显示结果
SELECT * FROM tb
/*--结果
BH col
------------------- -----------
050405000001 1
050405000002 2
050405000003 4
050405000004 14
--*/
create table test(id varchar(18), --流水号,日期(8位)+时间(4位)+流水号(4位)
name varchar(10) --其他字段
)go
--创建生成流水号的触发器
create trigger t_insert on test
INSTEAD OF insert
as
declare @id varchar(18),@id1 int,@head varchar(12)
select * into #tb from inserted
set @head=convert(varchar,getdate(),112)+replace(convert(varchar(5),getdate(),108),':','')
select @id=max(id) from test where id like @head+'%'
if @id is null
set @id1=0
else
set @id1=cast(substring(@id,13,4) as int)
update #tb set @id1=@id1+1
,id=@head+right('0000'+cast(@id1 as varchar),4)
insert into test select * from #tb
go
--插入数据,进行测试
insert into test(name)
select 'aa'
union all select 'bb'
union all select 'cc'--修改系统时间,再插入数据测试一次
insert into test(name)
select 'aa'
union all select 'bb'
union all select 'cc'--显示测试结果
select * from test
--删除测试环境
drop table test/*--测试结果
id name
------------------ ----------
2004022720430001 aa
2004022720430002 bb
2004022720430003 cc
2004022720430004 aa
2004022720430005 bb
2004022720430006 cc(所影响的行数为 6 行)
--*/
-------------------------实例
--drop table t
--go
--建表
create table t (coding varchar(20) not null,a int)
go
--新建个函数
--drop function [dbo].[AutoFollowExamCode]
create function [dbo].[AutoFollowExamCode](@date datetime)
returns varchar(13)
as
begin
declare @str varchar(13)
select @str=isnull(max(coding),'20'+right(datename(year,@date),2)+right('0'+right(datepart(month,@date),2),2)+right('0'+datename(day,@date),2)+'00000')
from t
where coding like '20'+right(datename(year,@date),2)+right('0'+right(datepart(month,@date),2),2)+right('00'+datename(day,@date),2)+'%'
select @str=left(@str,8)+right('00000'+convert(varchar(5),convert(int,right(@str,5))+1),5)
return (@str)
end
--把表的默认值设置成[dbo].[AutoFollowExamCode](getdate())
--测试
go
--加约束
ALTER TABLE dbo.t ADD CONSTRAINT
DF_t_coding DEFAULT [dbo].[AutoFollowExamCode](getdate()) FOR coding
--删除测试数据
go
--
declare @i int
select @i=8
while @i>0
begin
insert into t(a) values ('1')
select @i=@i-1
end
--显示结果
go
select * from t
go
drop function AutoFollowExamCode
go
drop table t