如何在查询的时候再追加一个字段?
已知:
1.数据库结构:
CREATE TABLE [dbo].[Doctree] (
[key] [nvarchar] (40) COLLATE Chinese_PRC_CI_AS NULL ,
[parent] [nvarchar] (40) COLLATE Chinese_PRC_CI_AS NULL ,
[text] [nvarchar] (200) COLLATE Chinese_PRC_CI_AS NULL ,
[image] [smallint] NULL ,
[selectedimage] [smallint] NULL
) ON [PRIMARY]
GO
2.select * from Doctree输出结果:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2_0_B.城市勘察类12
5_2_1.工程地质12
6_2_2.水文地质12
7_2_3.控制测量12
10_2_4.地形测量12
11_10_1.文字材料12
12_10_2.500地形图12
13_10_3.1000地形图12
14_10_4.其它比例尺12
先要输出这样的结果,请问如何来搞,谢谢,
就是在查询的时候再追加一个字段,并输出如下的结果(注:前四列输出不变)
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2_0_B.城市勘察类12 B
5_2_1.工程地质12 B-1
6_2_2.水文地质12 B-2
7_2_3.控制测量12 B-3
10_2_4.地形测量12 B-4
11_10_1.文字材料12 B-4-1
12_10_2.500地形图12 B-4-2
13_10_3.1000地形图12 B-4-3
14_10_4.其它比例尺12 B-4-4
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
[解决办法]
CREATE TABLE [dbo].[Doctree] (
[key] [nvarchar] (40) COLLATE Chinese_PRC_CI_AS NULL ,
[parent] [nvarchar] (40) COLLATE Chinese_PRC_CI_AS NULL ,
[text] [nvarchar] (200) COLLATE Chinese_PRC_CI_AS NULL ,
[image] [smallint] NULL ,
[selectedimage] [smallint] NULL
) ON [PRIMARY]
GO
insert into Doctree
select '2_ ', '0_ ', 'B.城市勘察类 ',1,2
union all select '5_ ', '2_ ', '1.工程地质 ',1,2
union all select '6_ ', '2_ ', '2.水文地质 ',1,2
union all select '7_ ', '2_ ', '3.控制测量 ',1,2
union all select '10_ ', '2_ ', '4.地形测量 ',1,2
union all select '11_ ', '10_ ', '1.文字材料 ',1,2
union all select '12_ ', '10_ ', '2.500地形图 ',1,2
union all select '13_ ', '10_ ', '3.1000地形图 ',1,2
union all select '14_ ', '10_ ', '4.其它比例尺 ',1,2
go
create function f_getparent(@parent nvarchar(40))
returns varchar(1000)
as
begin
declare @key nvarchar(40)
declare @str varchar(1000)
select @str=left([text],1)+ '- ',@key=[parent] from doctree where [key]=@parent
while isnull(@key, ' ') <> '0_ '
begin
select @str=@str+left([text],1)+ '- ',@key=[parent] from doctree where [key]=@key
end
return reverse(left(@str,len(@str)-1))
end
go
select [key],
[parent],
[text],
[image],
[selectedimage],
case when parent= '0_ '
then left([text],1)
else dbo.f_getparent([parent])+ '- '+left([text],1) end as str
from Doctree