日期:2014-05-17 浏览次数:20828 次
CREATE TABLE #Temp(
[ID] [int] IDENTITY(1,1) NOT NULL,
[类别编码] [nvarchar](50) NULL,
[类别名称] [nvarchar](50) NULL,
[级次][int] NULL,
[是否末级][int]NULL
)
insert into #temp
select '1', '原材料', 1, 0 union all
select '101', '电气', 2, 0 union all
select '10101', '高压电器元件类', 3, 0 union all
select '1010101', '高压真空断路器', 4, 1 union all
select '1010102', '高压开关类', 4, 1 union all
select '1010103', '高压绝缘件类', 4, 1 union all
select '1010104', '高压二次元器件类', 4, 1 union all
select '1010105', '高压其它元件类', 4, 1 union all
select '1010106', '高压互感器', 4, 1 union all
select '10102', '低压电器元件类', 3, 0 union all
select '1010201', '低压断路器类', 4, 0 union all
select '101020101', '万能式断路器类', 5, 1 union all
select '101020102', '塑壳断路器类', 5, 1 union all
select '101020103', '小型断路器类', 5,1
;with abc as(
select *,case when (级次>1) then left(类别编码,len(类别编码)-2) end 父级编码 from #Temp
)
select * from abc order by 类别编码
drop table #Temp