日期:2014-05-18  浏览次数:20486 次

如何在查询的时候再追加一个字段?
已知:

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.城市勘察类 1 2      
5_ 2_ 1.工程地质 1 2      
6_ 2_ 2.水文地质 1 2      
7_ 2_ 3.控制测量 1 2      
10_ 2_ 4.地形测量 1 2      
11_ 10_ 1.文字材料 1 2      
12_ 10_ 2.500地形图 1 2      
13_ 10_ 3.1000地形图 1 2      
14_ 10_ 4.其它比例尺 1 2      

先要输出这样的结果,请问如何来搞,谢谢,

就是在查询的时候再追加一个字段,并输出如下的结果(注:前四列输出不变)
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                                                                                                
2_ 0_ B.城市勘察类 1 2       B
5_ 2_ 1.工程地质 1 2       B-1
6_ 2_ 2.水文地质 1 2       B-2
7_ 2_ 3.控制测量 1 2       B-3
10_ 2_ 4.地形测量 1 2       B-4
11_ 10_ 1.文字材料 1 2       B-4-1
12_ 10_ 2.500地形图 1 2       B-4-2
13_ 10_ 3.1000地形图 1 2       B-4-3
14_ 10_ 4.其它比例尺 1 2       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], <