Id, FormatId, F1 ,F2 Id序号我设了自动加一,FormatId我想他也象这样"SL000001", 当Insert时就加1,FormatId我想他也能自动加一"SL000001","SL000002"... 能用一条sql什么办法实现.最好不要用中间表。有什么好方法? 谢谢! create table #test (id int identity, FormatId as 'SL'+right(10000000+id,6), F1 varchar(50)) go insert #test(F1) select '1' union all select '2' select * from #testdrop table #test /* id FormatId F1 ----------- -------------- ----- 1 SL000001 1 2 SL000002 2(所影响的行数为 2 行) */
create table tb(tid int identity,id as right(10000+tid,4),f int) goinsert into tb(f) values(1) select * from tb /* tid id f ----------- -------- ----------- 1 0001 1(所影响的行数为 1 行) */insert into tb(f) values(1) select * from tb /* tid id f ----------- -------- ----------- 1 0001 1 2 0002 1(所影响的行数为 2 行) */insert into tb(f) values(1) select * from tb /* tid id f ----------- -------- ----------- 1 0001 1 2 0002 1 3 0003 1(所影响的行数为 3 行) */insert into tb(f) values(1) select * from tb /* tid id f ----------- -------- ----------- 1 0001 1 2 0002 1 3 0003 1 4 0004 1(所影响的行数为 4 行) */insert into tb(f) values(1) select * from tb /* tid id f ----------- -------- ----------- 1 0001 1 2 0002 1 3 0003 1 4 0004 1 5 0005 1(所影响的行数为 5 行) */ drop table tb
Id序号我设了自动加一,FormatId我想他也象这样"SL000001",
当Insert时就加1,FormatId我想他也能自动加一"SL000001","SL000002"...
能用一条sql什么办法实现.最好不要用中间表。有什么好方法?
谢谢!
create table #test
(id int identity,
FormatId as 'SL'+right(10000000+id,6),
F1 varchar(50))
go
insert #test(F1) select '1'
union all select '2'
select * from #testdrop table #test
/*
id FormatId F1
----------- -------------- -----
1 SL000001 1
2 SL000002 2(所影响的行数为 2 行)
*/
goinsert into tb(f) values(1)
select * from tb
/*
tid id f
----------- -------- -----------
1 0001 1(所影响的行数为 1 行)
*/insert into tb(f) values(1)
select * from tb
/*
tid id f
----------- -------- -----------
1 0001 1
2 0002 1(所影响的行数为 2 行)
*/insert into tb(f) values(1)
select * from tb
/*
tid id f
----------- -------- -----------
1 0001 1
2 0002 1
3 0003 1(所影响的行数为 3 行)
*/insert into tb(f) values(1)
select * from tb
/*
tid id f
----------- -------- -----------
1 0001 1
2 0002 1
3 0003 1
4 0004 1(所影响的行数为 4 行)
*/insert into tb(f) values(1)
select * from tb
/*
tid id f
----------- -------- -----------
1 0001 1
2 0002 1
3 0003 1
4 0004 1
5 0005 1(所影响的行数为 5 行)
*/
drop table tb