简体   繁体   English

SQL Server中带有XML列的临时表

[英]Temp Table with XML column in SQL Server

I have a table in SQL Server and I am putting the values of the result set from the table into XML like this: 我在SQL Server有一个表,并且将表中结果集的值放入XML如下所示:

select 
  lAccountID,
  sName,
  sAge
from 
  AccountDVDetails 
where 
  laccountID = 10 
for xml raw ('Values'), ROOT ('Accounts'), ELEMENTS 

This query gives me XML like 该查询给我XML

<Accounts>
<Values>
  <lAccountID>10</lAccountID>
  <sName>A</sName>
  <sAge>21</sAge>
</Values>
<Values>
  <lAccountID>10</lAccountID>
  <sName>B</sName>
  <sAge>22</sAge>
</Values>
<Values>
  <lAccountID>10</lAccountID>
  <sName>C</sName>
  <sAge>23</sAge>
</Values>
</Accounts>

Now I want to store this XML with lAccountId in a temporary table #tmpAccount like 现在,我要将带有lAccountId XML存储在临时表#tmpAccount例如

   lAccountId     XMLValue
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
     10           <Accounts><Values><lAccountID>10</lAccountID><sName>A</sName><sAge>21</sAge></Values><Values><lAccountID>10</lAccountID><sName>B</sName><sAge>22</sAge></Values><Values><lAccountID>10</lAccountID><sName>C</sName><sAge>23</sAge></Values></Accounts> 

How can I achieve this? 我该如何实现? I have tried putting the XML into string variable but the XML gets truncated to a certain limit. 我尝试将XML放入string变量,但是XML被截断到一定的限制。

Any help will be deeply appreciated. 任何帮助将不胜感激。 Thanks. 谢谢。

Check This. 检查一下。

         select distinct  
        (
          select 
          lAccountID,
          sName,
          sAge
         from 
          AccountDVDetails 
          where 
          laccountID = 10 
        for xml  raw ('Values'), ROOT ('Accounts'), ELEMENTS  
         )as XMLValue
         ,lAccountID into #tmpAccount  from  AccountDVDetails 
         where
         laccountID = 10 

         select * from #tmpAccount

Try this for a nested, hierarchical XML for many Accounts 尝试此操作以获取许多帐户的嵌套的分层XML

First I declare some table variables to mock-up a test scenario. 首先,我声明一些表变量以模拟测试方案。 I assume, that there is a parent table with accounts: 我假设有一个带有帐户的父表:

DECLARE @dummyAccount TABLE(lAccountID INT IDENTITY,SomeData VARCHAR(100));
DECLARE @dummyAccountDetail TABLE(lAccountDetail INT IDENTITY,lAccountID INT,sName VARCHAR(100),sAge INT);--do not store the age as int but the DoB!!!

INSERT INTO @dummyAccount VALUES('Account 1'),('Account 2');
INSERT INTO @dummyAccountDetail VALUES(1,'Jane',20),(1,'John',30)
                                     ,(2,'Tim',40),(2,'Tom',50);

The query will list the accounts as first level (I added some descibing attributes just to show the principles, but you can easily let them away. The sub-select will create an XML for each account separately. 该查询会将帐户列为第一级(我添加了一些描述属性只是为了显示原理,但您可以轻松地将它们放开。子选择将为每个帐户分别创建XML。

SELECT A.lAccountID 
      ,A.SomeData 
      ,(
        SELECT D.lAccountID
              ,D.sName
              ,D.sAge
        FROM @dummyAccountDetail AS D
        WHERE D.lAccountID=A.lAccountID
        FOR XML PATH('Values'),ROOT('Accounts'),TYPE
       )

FROM @dummyAccount AS A     

You can easily create the table #tmpAccount just with 您可以轻松创建表#tmpAccount

SELECT ...
INTO #tmpAccount
FROM ...

...or, if it exists already, just use ...或者,如果已经存在,只需使用

INSERT INTO #tmpAccount
SELECT ...

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

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