我正在尝试对多个表中表示的层次结构进行文本打印。表格如下所示:
create table #par1 ( par1_id int primary key identity, par1_data varchar(8) )
create table #par2 ( par2_id int primary key identity, par1_id int , par2_data varchar(8) )
create table #par3 ( par3_id int primary key identity , par2_id int , par3_data varchar(8) )
create table #par4 ( par4_id int primary key identity , par3_id int , par4_data varchar(8) )
create table #par5 ( par5_id int primary key identity, par4_id int , par5_data varchar(8) )
insert into #par1 values ( 'a' )
insert into #par1 values ( 'b' )
insert into #par1 values ( 'c' )
insert into #par1 values ( 'c' )
insert into #par2 values ( 1 , 'aa' )
insert into #par2 values ( 2 , 'bb' )
insert into #par2 values ( 3, 'cc' )
insert into #par2 values ( 4, 'cc' )
insert into #par3 values ( 1 , 'aaa' )
insert into #par3 values ( 2 , 'bbb' )
insert into #par3 values ( 3 , 'ccc' )
insert into #par3 values ( 4 , 'ddd' )
insert into #par4 values ( 1 , 'aaaa' )
insert into #par4 values ( 2 , 'bbbb' )
insert into #par4 values ( 3 , 'cccc' )
insert into #par4 values ( 4 , 'dddd' )
insert into #par5 values ( 1 , 'wwwww' )
insert into #par5 values ( 1 , 'xxxxx' )
insert into #par5 values ( 1 , 'yyyyy' )
insert into #par5 values ( 1 , 'zzzzz' )
我在http://vyaskn.tripod.com/hierarchies_in_sql_server_databases.htm处使用代码来创建正确的格式,但我无法打破单表结构来挖掘多个表。产生这种结果的 TSQL 是什么样的:
----a
--------aa
------------aaa
----------------aaaa
--------------------wwwww
--------------------xxxxx
--------------------yyyyy
--------------------zzzzz
----b
--------bb
------------bbb
...etc
谢谢。