【问题标题】:I want generate XML file in a hierarchical form我想以分层形式生成 XML 文件
【发布时间】:2012-11-21 12:20:40
【问题描述】:

我有一个这样的表(实际上它包含更多的 6000 条记录)

IdIndustry   |   IndustryCode  |   IndustryName  |  ParentId
---------------------------------
1    |  IND    |   Industry  |   NULL
2    |  PHARM  |   Pharmacy  |   1
3    |  FIN    |   Finance   |   NULL
4    |  CFIN   |   Corporate |   3
5    |  CMRKT  |   Capital M |   4

DDL:

CREATE TABLE [dbo].[tblIndustryCodes](
    [IdIndustry] [int] IDENTITY(1,1) NOT NULL,
    [IndustryCode] [nvarchar](5) NULL,
    [IndustryName] [nvarchar](50) NULL,
    [ParentId] [int] NULL,
CONSTRAINT [PK_tblIndustryCodes] PRIMARY KEY CLUSTERED ([IdIndustry] ASC)

插入:

INSERT INTO [tblIndustryCodes]
          ([IndustryCode]
          ,[IndustryName]
          ,[ParentId])
     VALUES
          ('IND','Industry',NULL),
          ('PHARM','Pharmacy',1),
          ('FIN','Finance',NULL),
          ('CFIN','Corporate Finance',3),
          ('CMRKT','Capital Markets',4)

我想生成一个这样的 XML 文件(简化树状结构)

<IND>
      <PHARM>
      </PHARM>
</IND>
<FIN>
      <CFIN>
            <CMRKT>
            </CMRKT>
      </CFIN>
<FIN>

我不想使用递归,因为这个表有超过 60000 条记录,它会显着降低性能。

如果我得到相同格式的输出,我会很高兴,因为我将使用这个输出 XML 来发送请求。

更重要的是,它本质上是动态的。

【问题讨论】:

  • 好的,我做了一些额外的家庭作业,以及我是如何知道这将通过“sp_xml_preparedocument”程序完成的。

标签: sql sql-server stored-procedures hierarchy for-xml-path


【解决方案1】:

尝试这个过程不太确定它的效率,因为我正在创建一个临时表来获取结果

create procedure get_path as begin
  DECLARE @cnt INT
  DECLARE @n INT
  DECLARE @tmpTable TABLE(id int, 
                indCode varchar(50), 
                indName varchar(100),
                parentId int,
                path varchar(500))

  insert @tmpTable 
          select [IdIndustry], [IndustryCode], [IndustryName], [ParentId],
          null from tbl

  select @cnt = count(*)  from @tmpTable where parentId is null
  update a set a.path = CONCAT(b.indName,'/',a.indName) from @tmpTable a, @tmpTable b where b.parentid is null and a.parentid = b.id
  select @n = count(*)  from @tmpTable where path is null
  while (@cnt < @n) begin
    update a set a.path = concat(b.path, '/', b.indName, '/', a.indName) from @tmpTable a, @tmpTable b where b.path is not null and a.parentid = b.id
    select @n = count(*) from @tmpTable where path is null
  end
  update @tmpTable set path = indName where parentid is null 
  select * from @tmpTable order by path
end
go

查询 1

exec get_path

结果

| ID | INDCODE |   INDNAME | PARENTID |                                  PATH |
-------------------------------------------------------------------------------
|  3 |     FIN |   Finance |   (null) |                               Finance |
|  4 |    CFIN | Corporate |        3 |                     Finance/Corporate |
|  5 |   CMRKT | Capital M |        4 | Finance/Corporate/Corporate/Capital M |
|  1 |     IND |  Industry |   (null) |                              Industry |
|  2 |   PHARM |  Pharmacy |        1 |                     Industry/Pharmacy |

希望这会有所帮助.....

SQL FIDDLE

【讨论】:

  • 您使用哪种语言获取语言?但如果有问题,最好先尝试一下,我们很乐意提供帮助......
  • 最高等级是什么意思?
  • 我的级别是指孩子和父母(一代)。
  • 抱歉,现在没有那么多时间。请发布您面临的任何问题。将在晚些时候尝试解决...
猜你喜欢
  • 2018-07-04
  • 2021-08-04
  • 2018-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多