日期:2014-05-18  浏览次数:20460 次

应该怎么插入
表结构

ID TYPE_ID Person_ID
1 abc_001 1000
2 abc_002 1001
3 abc_001 1000
4 abc_002 1001
5 abc_003 1000

我现在想再插入一条记录 Person_ID为1000 TYPE_ID字段中已经有abc_001、abc_002、abc_003再插的话应插入abc_004

请问abc_004我就应该怎么取得出来然后插进去?TYPE_ID的类型是char(8)

------解决方案--------------------
SQL code
select 'abc'+right('00'+ltrim(max(right([type_id],3))+1),3) from tb where person_id=1000

------解决方案--------------------
SQL code
--> 测试数据: #T
if object_id('tempdb.dbo.#T') is not null drop table #T
create table #T (ID int,TYPE_ID varchar(7),Person_ID int)
insert into #T
select 1,'abc_001',1000 union all
select 2,'abc_002',1001 union all
select 3,'abc_001',1000 union all
select 4,'abc_002',1001 union all
select 5,'abc_003',1000

select 'abc_'+right('00'+ltrim(max(right([type_id],3))+1),3) from #T where person_id=1000
/*
abc_004
*/