比如说入库的时候产生RK200906190001
出库时CK200906190002之类的
RK 入库,20090619日期,0001 流水号
谢谢
出库时CK200906190002之类的
RK 入库,20090619日期,0001 流水号
谢谢
解决方案 »
- sql2005 to sql2000 (FOR XML PATH)
- BIHUMAN义务技术推广课程之微软商业智能
- 求SQL分类汇总,统计每月、每客户订单金额语句
- XP+SP3+SQL Server 2000连接问题
- 怎么更改登陆名
- 从没遇到的难题,解决了要多少分请说话!
- 存储过程调试中输入DATETIME值
- 简单问题
- 求一统计SQL . 急.................................
- 急急急!!!怎样在DOS下加大汉字显示(UCDOS5.0,FOXPRO2.6 For DOS)
- 如何取出第一行的值,改变其中某几个值后插入数据库
- 怎么样在sqlserver2000 建链接服务器到 sqlserver2005 ?
在学习中遇到这个问题
数据库里有编号字段
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 行)
*/
insert into @ta
select 'RK200906190001'
select 'RK'+CONVERT(VARCHAR(10),GETDATE(),112)+RIGHT('0000'+CAST((SELECT MAX(SUBSTRING(A,11,4))+1 FROM @TA) AS VARCHAR),4)
--你自己想写到哪都行
for insert
as
declare @maxtmp char(11)
declare @rtime datetime
declare @b_id int
set @rtime=getdate()
declare @r_id char(11)
select @maxtmp=max(t_id) from test
select @b_id=id,@b_type=crtype from inserted
if left(@maxtmp,8)=convert(char(8),@rtime,112)
---and @b_type='入库' 加个判断
begin
set @r_id = convert(char(11),convert(bigint,@maxtmp)+1)
end
else
begin
set @r_id = convert(char(8),@rtime,112)+'001'
end
update test set t_id=@r_id where @b_id=id