`
sogotobj
  • 浏览: 618689 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

同一表内,各部份行自动重新编号T-SQL处理代码

阅读更多

--同一表内,各部份行自动重新编号T-SQL处理代码

select identity(int,1,1) as id,* into # from DS order by Department

select Department,min(id) as n,max(id) as m into #DMN
from #
group by Department

update DS set DS.[Sequence]=a.id-(d.n-1)
from # as a,#DMN as d
where a.custid=DS.custid and a.Department=d.Department

drop table #
drop table #DMN

--附:DS表生成脚本及执行结果示意图

CREATE TABLE [dbo].[DS] (
[CustId] [int] NOT NULL PRIMARY KEY,
[Department] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[Sequence] [int] NULL
) ON [PRIMARY]

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics