在学习中遇到这个问题
数据库里有编号字段
BH00001
BH00002
BH00003
BH00004
如何实现自动增长 --下面的代码生成长度为8的编号,编号以BH开头,其余6位为流水号。
--得到新编号的函数
CREATE FUNCTION f_NextBH()
RETURNS char(8)
AS
BEGIN
RETURN(SELECT 'BH'+RIGHT(1000001+ISNULL(RIGHT(MAX(BH),6),0),6) FROM tb WITH(XLOCK,PAGLOCK))
END
GO--在表中应用函数
CREATE TABLE tb(
BH char(8) PRIMARY KEY DEFAULT dbo.f_NextBH(),
col int)--插入资料
BEGIN TRAN
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)
COMMIT TRAN--显示结果
SELECT * FROM tb
/*--结果
BH col
---------------- -----------
BH000001 1
BH000002 2
BH000003 4
BH000004 14
--*/ create table tb
(id int identity,
name varchar(10),
code as 'BH'+right('0000'+cast(id as varchar),5))
go
insert tb(name) select 'A'
union all select 'B'
union all select 'C'
union all select 'D'select * from tbdrop table tb/*
id name code
----------- ---------- ------------
1 A BH00001
2 B BH00002
3 C BH00003
4 D BH00004(所影响的行数为 4 行)
*/
数据库里有编号字段
BH00001
BH00002
BH00003
BH00004
如何实现自动增长 --下面的代码生成长度为8的编号,编号以BH开头,其余6位为流水号。
--得到新编号的函数
CREATE FUNCTION f_NextBH()
RETURNS char(8)
AS
BEGIN
RETURN(SELECT 'BH'+RIGHT(1000001+ISNULL(RIGHT(MAX(BH),6),0),6) FROM tb WITH(XLOCK,PAGLOCK))
END
GO--在表中应用函数
CREATE TABLE tb(
BH char(8) PRIMARY KEY DEFAULT dbo.f_NextBH(),
col int)--插入资料
BEGIN TRAN
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)
COMMIT TRAN--显示结果
SELECT * FROM tb
/*--结果
BH col
---------------- -----------
BH000001 1
BH000002 2
BH000003 4
BH000004 14
--*/ create table tb
(id int identity,
name varchar(10),
code as 'BH'+right('0000'+cast(id as varchar),5))
go
insert tb(name) select 'A'
union all select 'B'
union all select 'C'
union all select 'D'select * from tbdrop table tb/*
id name code
----------- ---------- ------------
1 A BH00001
2 B BH00002
3 C BH00003
4 D BH00004(所影响的行数为 4 行)
*/
解决方案 »
- 怎么查询一个时间段内固定时间点的数据
- 请教为什么我的存储过程只能返回select的结果,而不能返回参数值@countpage ?
- 这个数据库备份问题怎么解决?http://community.csdn.net/Expert/topic/4561/4561716.xml?temp=.1408045
- 想问下关于数据库的问题啊,我怎么往视图里增加一个自增字段名?这个字段名在与该视图相关联的表中是不存在的。
- 请问这个怎么写?谢谢!
- 怎么使用一个表变量呀?
- 请问,如何动态改变字段进行查询?
- 新手入门,请教sql语句,谢谢!
- 这样的查询怎么写?上次别人回的结了贴才发现不行的。没分了,请原谅
- 我问道SQL的题,哪位大神能帮帮我啊,急。。。
- 想问下数据的对比
- ms—sql数据库同步订阅时那个订阅类型的服务端和客户端有什么区别?
china+right('00000'+cast(cast(substring(max(col),6,5)as int)+1 as char),5)
'china'+right('00000'+cast(cast(substring(max(col),6,5)as int)+1 as char),5)
if object_id('tb') is not null
drop table tb
go
create table tb([id] char(10),[date] datetime)
insert tb select 'china10001','2009-04-01 20:10:10'
insert tb select 'china10002','2009-03-30 19:05:05'
insert tb select 'china10003','2009-03-25 06:05:01'
insert tb select 'china10005','2009-03-10 12:05:05'
insert tb select 'china10007','2009-03-30 06:55:01'
go--创建函数
create FUNCTION f_NextBH()
RETURNS char(10)
AS
BEGIN
RETURN(SELECT 'china'+RIGHT(100001+ISNULL(RIGHT(MAX(id),5),0),5) FROM tb WITH(XLOCK,PAGLOCK))
END
GO
-->更新例默认值
alter table tb add constraint df_id default(dbo.f_NextBH()) for id
--alter table tb drop constraint df_id
-->插入测试数据
insert tb(date) select '2009-03-31 06:55:01'
-->查询
select * from tb
/*
id date
---------- -----------------------
china10001 2009-04-01 20:10:10.000
china10002 2009-03-30 19:05:05.000
china10003 2009-03-25 06:05:01.000
china10005 2009-03-10 12:05:05.000
china10007 2009-03-30 06:55:01.000
china10008 2009-03-31 06:55:01.000(6 行受影响)*/
--得到新编号的函数
CREATE FUNCTION f_NextBH()
RETURNS char(8)
AS
BEGIN
RETURN(SELECT 'BH'+RIGHT(1000001+ISNULL(RIGHT(MAX(BH),6),0),6) FROM tb WITH(XLOCK,PAGLOCK))
END
GO--在表中应用函数
CREATE TABLE tb(
BH char(8) PRIMARY KEY DEFAULT dbo.f_NextBH(),
col int)--插入资料
BEGIN TRAN
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)
COMMIT TRAN--显示结果
SELECT * FROM tb
/*--结果
BH col
---------------- -----------
BH000001 1
BH000002 2
BH000003 4
BH000004 14
--*/
if object_id('tb') is not null
drop table tb
go
create table tb([id] char(10),[date] datetime)
insert tb select 'china10001','2009-04-01 20:10:10'
insert tb select 'china10002','2009-03-30 19:05:05'
insert tb select 'china10003','2009-03-25 06:05:01'
insert tb select 'china10005','2009-03-10 12:05:05'
insert tb select 'china10007','2009-03-30 06:55:01'
go--创建函数
create FUNCTION f_NextBH()
RETURNS char(10)
AS
BEGIN
RETURN(SELECT 'china'+RIGHT(100001+ISNULL(RIGHT(MAX(id),5),0),5) FROM tb WITH(XLOCK,PAGLOCK))
END
GO
-->更新例默认值
alter table tb add constraint df_id default(dbo.f_NextBH()) for id
--alter table tb drop constraint df_id
-->插入测试数据
insert tb select 'china12000','2009-03-31 06:55:01' --从12000开始
insert tb(date) select '2009-03-31 06:55:01'
-->查询
select * from tb
/*
id date
---------- -----------------------
china10001 2009-04-01 20:10:10.000
china10002 2009-03-30 19:05:05.000
china10003 2009-03-25 06:05:01.000
china10005 2009-03-10 12:05:05.000
china10007 2009-03-30 06:55:01.000
china10008 2009-03-31 06:55:01.000
china12000 2009-03-31 06:55:01.000
china12001 2009-03-31 06:55:01.000(8 行受影响)
*/