首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

应该如何插入

2012-03-01 
应该怎么插入表结构IDTYPE_IDPerson_ID1abc_00110002abc_00210013abc_00110004abc_00210015abc_0031000我

应该怎么插入
表结构

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*/ 

热点排行