/**//**功能:中断ID的连续处理**适用:SQLServer2000/SQLServer2005**返回:NONE**作者:Flystone**日期:2008-05-13**/--问题引入/**//*表﹕test字段:u_nochar(2),u_nameidchar(5)---u_no-----------u_nameid-------01A000102B892103F8762.99.....比如我刪除了行66就是u_no為66的行,表紀錄成了:---u_no-----------u_nameid-------01A000102B892103F8762.6567.99.....我要讓u_no自己補齊刪除的數字﹐如下所示---u_no-----------u_nameid-------01A000102B892103F8762...6566.98.....*/--环境准备createtabletest(u_nochar(2),u_nameidchar(5))insertintotestselect'01','A0001'unionselect'02','B8921'unionselect'03','F8762'unionselect'04','C2345'unionselect'05','C2345'unionselect'06','C2345'unionselect'07','C2345'unionselect'08','C2345'unionselect'09','C2345'go--方法一:触发器createtriggertr_deleteidontestfordeleteasbegindeclare@iint,@cintselect@I=min(cast(u_noasint)),@c=count(1)fromdeletedupdatetestsetu_no=right('00'+ltrim(cast(u_noasint)-@c),2)whereu_no>@iendgo--原始记录select*fromtest/**//*u_nou_nameid------------01A000102B892103F876204C234505C234506C234507C234508C234509C2345(所影响的行数为9行)*/--单条删除deletefromtestwhereu_no='02'select*fromtest/**//*u_nou_nameid------------01A000102F876203C234504C234505C234506C234507C234508C2345(所影响的行数为8行)*/--批量删除开始deletefromtestwhereu_nobetween'02'and'04'select*fromtest/**//*u_nou_nameid------------01A000102C234503C234504C234505C2345(所影响的行数为5行)*/--方法二:过程处理--重新准备数据droptabletest--一定要删除哦,因为上面的表有触发器哦gocreatetabletest(u_nochar(2),u_nameidchar(5))insertintotestselect'01','A0001'unionselect'02','B8921'unionselect'03','C8762'unionselect'04','D2345'unionselect'05','E2345'unionselect'06','F2345'unionselect'07','G2345'unionselect'08','H2345'unionselect'09','I2345'gocreateprocproc_test@u_novarchar(2),@Lintasbegindeclare@ichar(2)set@i=right('00'+ltrim(cast(@u_noasint)+@L-1),2)deletefromtestwhereu_nobetween@u_noand@iupdatetestsetu_no=right('00'+ltrim(cast(u_noasint)-@L),2)whereu_no>@u_noendgo--原始记录select*fromtest/**//*u_nou_nameid------------01A000102B892103F876204C234505C234506C234507C234508C234509C2345(所影响的行数为9行)*/--单条删除execproc_test'02',1select*fromtestgo/**//*u_nou_nameid------------01A000102F876203C234504C234505C234506C234507C234508C2345(所影响的行数为8行)*/--批量删除开始execproc_test'02',2select*fromtest/**//*u_nou_nameid------------01A000102E234503F234504G234505H234506I2345(所影响的行数为6行)*/--清除环境droptabletestdropprocproc_test