如何在表中增加一列如下值:CNPA000001 ,CNPA000002
在一个表中增加一列值Code,值如下:
Code name
CNPA000001 笔
CNPA000002 纸
CNPA000003 书本
。。。。
[解决办法]
Create Table TEST
(CodeVarchar(10),
nameNvarchar(20))
Insert TEST(name) Select N '笔 '
Union All Select N '纸 '
Union All Select N '书本 '
GO
Update A Set Code = 'CNPA ' + Right(1000000 + (Select Count(*) From TEST Where name <= A.name), 6) From TEST A
Select * From TEST Order By Code
GO
Drop Table TEST
--Result
/*
Codename
CNPA000001书本
CNPA000002笔
CNPA000003纸
*/
[解决办法]
create table #test(id int identity , Code as 'CNPA '+right(10000000+id,6),name varchar(10))
insert into #test(name) values( '笔 ')
insert into #test(name) values( '纸 ')
insert into #test(name) values( '书本 ')
select * from #test
drop table #test
/*
id Code name
----------- ---------------- ----------
1 CNPA000001 笔
2 CNPA000002 纸
3 CNPA000003 书本
(所影响的行数为 3 行)
*/