34,593
社区成员
发帖
与我相关
我的任务
分享
create table test (aid int identity(1,1) primary key,pp varchar(10),rr varchar(10),co varchar(10))
--创建表
--插入表资料
insert test
select '55','d',''
union
select 'ee','e',''
union
select 'o09','o',''
union
select 'pol','p','p'
union
select 'iuy','u','uyt'
union
select 'qwr','q','14'
union
select 'tyn','m',''
union
select '99','oi','uty01'
union
select 'qwe','rty','1425'
union
select '0158','iu145','ii'
union
select 'zxc','yu','78'
union
select 'as','df',''
select aid,pp = 'ak'+right((1000+aid),3), rr,co from test
==================
aid pp rr co
----------- -------- ---------- ----------
1 ak001 iu145 ii
2 ak002 d
3 ak003 oi uty01
4 ak004 df
5 ak005 e
6 ak006 u uyt
7 ak007 o
8 ak008 p p
9 ak009 rty 1425
10 ak010 q 14
11 ak011 m
12 ak012 yu 78
(12 行受影响)
declare @test table(aid int identity(1,1) primary key,pp varchar(10),rr varchar(10),co varchar(10))
--创建表
insert @test
select '55','d',''
union all
select 'ee','e',''
union all
select 'o09','o',''
union all
select 'pol','p','p'
union all
select 'iuy','u','uyt'
union all
select 'qwr','q','14'
union all
select 'tyn','m',''
union
select '99','oi','uty01'
union all
select 'qwe','rty','1425'
union all
select '0158','iu145','ii'
union all
select 'zxc','yu','78'
union all
select 'as','df',''
union all
select 'as','df','' ----加一条测试
declare @n int,@str varchar(10)
set @n=0
update @test
set @n= case when @n!=12 then @n+1 else 1 end ,pp='ak'+right('000'+ltrim(@n),3)
select * from @test
aid pp rr co
----------- ---------- ---------- ----------
1 ak001 d
2 ak002 oi uty01
3 ak003 e
4 ak004 u uyt
5 ak005 o
6 ak006 p p
7 ak007 q 14
8 ak008 m
9 ak009 rty 1425
10 ak010 iu145 ii
11 ak011 yu 78
12 ak012 df
13 ak001 df
(13 行受影响)
update tb set pp='ak'+right(1000+aid,3)
update test set
pp='Ak'+replicate('0',3-len(aid))+aid
如果aid是自增的话,可以直接
update tb set pp='ak'+right(1000+aid,3)
create table test (aid int identity(1,1) primary key,pp varchar(10),rr varchar(10),co varchar(10))
--创建表
--插入表资料
insert test
select '55','d',''
union
select 'ee','e',''
union
select 'o09','o',''
union
select 'pol','p','p'
union
select 'iuy','u','uyt'
union
select 'qwr','q','14'
union
select 'tyn','m',''
union
select '99','oi','uty01'
union
select 'qwe','rty','1425'
union
select '0158','iu145','ii'
union
select 'zxc','yu','78'
union
select 'as','df',''
declare @i int;
update test set
@i = isnull(@i,0) + 1,
pp = 'ak'+right(1000+@i,3);
select * from test;
drop table test
/*
aid pp rr co
----------- ---------- ---------- ----------
1 ak001 iu145 ii
2 ak002 d
3 ak003 oi uty01
4 ak004 df
5 ak005 e
6 ak006 u uyt
7 ak007 o
8 ak008 p p
9 ak009 rty 1425
10 ak010 q 14
11 ak011 m
12 ak012 yu 78
(12 行受影响)
*/
update [Table] set pp='ak'+right(1000+aid,3)
---参考:
在学习中遇到这个问题
数据库里有编号字段
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
--*/
declare @i int;
update test set
@i = isnull(@i,0) + 1,
pp = 'ak'+right(1000+@i,3);
select * from test;