0

我正在尝试对多个表中表示的层次结构进行文本打印。表格如下所示:

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

谢谢。

4

1 回答 1

1

它看起来像数据库中的中间层代码。

但是直接附加到您的脚本的 T-SQL 是这样的:

CREATE TABLE #allPar (id int IDENTITY (1,1), originalID int, originalTable int, parentID int NULL, data varchar(8))

INSERT INTO [#allPar] ([originalID], [originalTable], [data])
SELECT [par1_id], 1, [par1_data] FROM #par1;

INSERT INTO [#allPar] ([originalID], [originalTable], [parentID], [data])
SELECT [par2_id], 2, [id], [par2_data] FROM #par2 INNER JOIN #allPar ON [#allPar].[originalID] = [#par2].[par1_id] AND [#allPar].[originalTable] = 1;

INSERT INTO [#allPar] ([originalID], [originalTable], [parentID], [data])
SELECT [par3_id], 3, [id], [par3_data] FROM #par3 INNER JOIN #allPar ON [#allPar].[originalID] = [#par3].[par2_id] AND [#allPar].[originalTable] = 2;

INSERT INTO [#allPar] ([originalID], [originalTable], [parentID], [data])
SELECT [par4_id], 4, [id], [par4_data] FROM #par4 INNER JOIN #allPar ON [#allPar].[originalID] = [#par4].[par3_id] AND [#allPar].[originalTable] = 3;

INSERT INTO [#allPar] ([originalID], [originalTable], [parentID], [data])
SELECT [par5_id], 5, [id], [par5_data] FROM #par5 INNER JOIN #allPar ON [#allPar].[originalID] = [#par5].[par4_id] AND [#allPar].[originalTable] = 4;

DECLARE @text [varchar](MAX);
SET @text = '';
WITH Recursed
AS
(
-- Anchor member definition
    SELECT [id], [originalID], [originalTable], [parentID], [data], 0 AS [Level], [originalID] AS rootID
    FROM #allPar [ap] WHERE [ap].[parentID] IS NULL
    UNION ALL
-- Recursive member definition
    SELECT [ap].[id], [ap].[originalID], [ap].[originalTable], [ap].[parentID], [ap].[data], [apParent].[Level] + 1, [rootID]
    FROM #allPar [ap] INNER JOIN Recursed [apParent]
        ON ap.[parentID] = [apParent].[id]
)
SELECT @text = @text + REPLICATE('-', [Level] * 4) + [data] + CHAR(13) + CHAR(10) FROM [Recursed] ORDER BY [rootID], [Level], [originalID]
PRINT @text
于 2012-06-07T19:19:51.860 回答