简体   繁体   English

在SQL Server中为空时,生成表结构的XML

[英]Generate XML of the Structure of a table when it's empty in SQL Server

I am trying to generate a table containing one row that contains the XML of each temporary table in a stored procedure. 我正在尝试生成一个表,该表包含一行,该行包含存储过程中每个临时表的XML。

The problem is when the table is empty the FOR XML PATH('RS'), root('OR_RS') returns null . 问题是当表为空时, FOR XML PATH('RS'), root('OR_RS')返回null

What I want is: when the table is empty, return the structure of this table something like this : 我想要的是:当表为空时,返回此表的结构,如下所示:

<OR_RS>
    <ContactCode></ContactCode>
    <EmailPaper></EmailPaper>
    <ShortEmail></ShortEmail>
    <WebSite></WebSite>
    <Providers></Providers>
</OR_RS>  

I have tried to do this : 我试图做到这一点:

-- ,ISNULL( (SELECT  * FROM #OR_RS FOR XML PATH('RS'), root('OR_RS')), (SELECT ISNULL(ContactCode,'')  , ISNULL(EmailPaper,'') , ISNULL(ShortEmail,'') , ISNULL(WebSite,'')  , ISNULL(Providers,'')  FROM #OR_RS FOR XML RAW)) as OR_RS

but it returns always Null : 但它总是返回Null

 SELECT 1 as Id, (SELECT  * FROM #OR_MK   FOR XML PATH('MK'), root('OR_MK') ) as OR_MK
,(SELECT  * FROM #OR_CA FOR XML PATH('CA'), root('OR_CA') ) as OR_CA 
-- ,ISNULL( (SELECT  * FROM #OR_RS FOR XML PATH('RS'), root('OR_RS')), (SELECT ISNULL(ContactCode,'')  , ISNULL(EmailPaper,'') , ISNULL(ShortEmail,'') , ISNULL(WebSite,'')  , ISNULL(Providers,'')  FROM #OR_RS FOR XML AUTO)) as OR_RS
,(SELECT * FROM #OR_RS FOR XML PATH('RS'), root('OR_RS')) as OR_RS
,(SELECT  * FROM #OR_DC FOR XML PATH('DC'), root('OR_DC') ) as OR_DC
,(SELECT  * FROM #BENEFICIARY FOR XML PATH('BEN'), root('BENEFICIARY') ) as BENEFICIARY
,(SELECT * FROM #MK_REPORT for XML PATH('REP'), root('MK_REPORT')) as MK_REPORT

Whatever you try to do with this... 无论您尝试如何做...

You might want to have a look at sp_describe_first_result_set , but, since this is a procedure, it will not be that easy to get its result into your query... 您可能想看一下sp_describe_first_result_set ,但是,由于这是一个过程,因此将其结果输入查询并不容易。

You can use a trick (a RIGHT JOIN ) to enforce a resultset. 您可以使用技巧( RIGHT JOIN )来强制结果集。 And with ELEMENTS XSINIL you force the engine to include the column in any case. 而且,使用ELEMENTS XSINIL可以强制引擎在任何情况下都包括该列。 Otherwise XML takes missing elements as NULL , so your empty result would not be written into the result XML at all otherwise. 否则,XML将缺少的元素作为NULL ,因此,否则您的空结果根本不会写入结果XML中。 Try this: 尝试这个:

DECLARE @tbl TABLE(ID INT IDENTITY,SomeValue INT,SomeString VARCHAR(100));

SELECT * 
FROM @tbl
RIGHT JOIN (SELECT 1 AS x) AS tbl ON 1=1
FOR XML RAW, ELEMENTS XSINIL

The result 结果

<row xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <ID xsi:nil="true" />
  <SomeValue xsi:nil="true" />
  <SomeString xsi:nil="true" />
  <x>1</x>
</row>

If you want to get more details, you might add ,XMLDATA or ,XMLSCHEMA to generate full meta data. 如果您想了解更多详情,您可以添加,XMLDATA或者,XMLSCHEMA产生充分的元数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM