当前位置: 代码迷 >> Sql Server >> 应该如何插入
  详细解决方案

应该如何插入

热度:78   发布时间:2016-04-27 18:40:17.0
应该怎么插入
表结构

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
--> 测试数据: #Tif object_id('tempdb.dbo.#T') is not null drop table #Tcreate table #T (ID int,TYPE_ID varchar(7),Person_ID int)insert into #Tselect 1,'abc_001',1000 union allselect 2,'abc_002',1001 union allselect 3,'abc_001',1000 union allselect 4,'abc_002',1001 union allselect 5,'abc_003',1000select 'abc_'+right('00'+ltrim(max(right([type_id],3))+1),3) from #T where person_id=1000/*abc_004*/
  相关解决方案