5

I would like to query multiple tables from SQL Server 2005 and create a single XML document and do this in a stored procedure.

I know that I can query multiple tables within a stored procedure and get a DataSet in my .NET application that can be easily saved as XML. However, I'm trying to do something similar within the context of a stored procedure.

Essentially I want to do something like this:

declare @x xml
select @x = x.result
from (select y.* from tabley y for xml path('y')
      union
      select a.* from tablea a for xml path('aa')
     ) as x
select @x
4

3 回答 3

14

If you want them just one after the other, you can try something like this:

SELECT
    (SELECT y.* FROM dbo.TableY FOR XML PATH('y'), TYPE) AS 'YElements',
    (SELECT a.* FROM dbo.TableA FOR XML PATH('aa'), TYPE) AS 'AElements'
FOR XML PATH(''), ROOT('root')

That return an XML something like:

<root>
   <YElements>
     <Y>
       ....
     </Y>
     <Y>
       ....
     </Y>
      ......
   </YElements>
   <AElements>
     <A>
       ....
     </A>
     <A>
       ....
     </A>
      ......
   </AElements>
</root>
于 2012-08-09T16:10:55.827 回答
1
SELECT                                                               -- Root Starts
   (SELECT  '1' AS ErrorCode FOR XML PATH(''), TYPE) AS 'Results',   -- Level 1 Starts
       (select                                                       -- Level 2 Starts
             (select      '1'         CustomerID,                    -- Level 2 Detail Starts
                          'John'      CustomerName,
                          'Doe'       CustomerLastname,
                          'Y'         Active
              for xml path('Customers'), type) AS 'Customer'         -- Level 2 Detail Ends
       for xml path(''), TYPE) AS 'Response'                         -- Level 2 Ends
   FOR XML PATH('')                                                  -- Level 1 Ends
,ROOT('BaseXML')                                                     -- Root Ends
于 2017-03-14T19:36:38.420 回答
0

在 sql server 中转换 Table XML。

Declare @RESULTXML XML
Declare @SMS_REGISTER TABLE([id] VARCHAR(30),[status] VARCHAR(30))
Declare @EMAIL_REGISTER TABLE([id] VARCHAR(30),[status] VARCHAR(30))
Declare @ODP_REGISTER TABLE([id] VARCHAR(30),[status] VARCHAR(30))

Select @RESULTXML =( 
SELECT (SELECT * FROM @SMS_REGISTER FOR XML PATH('sms'), TYPE) AS 'smss',
         (SELECT * FROM @EMAIL_REGISTER FOR XML PATH('email'), TYPE) AS         'emails',
         (SELECT * FROM @ODP_REGISTER FOR XML PATH('odp'), TYPE) AS 'odps'
    FOR XML PATH('subroot'), ROOT('root') )

像这样返回 XML

    <根>
       <子根>
        <短信>
          <短信>
            <id>NT0000000020</id>
            <status>注册</status>
          </sms>
          <短信>
            <id>NT0000000021</id>
            <status>注册</status>
          </sms>
          <短信>
            <id>NT0000000022</id>
            <status>注册</status>
          </sms>
          <短信>
            <id>NT0000000023</id>
            <status>注册</status>
          </sms>
        </smss>
        <电子邮件>
          <电子邮件>
            <id>NT0000000024</id>
            <status>注册</status>
          </电子邮件>
          <电子邮件>
            <id>NT0000000025</id>
            <status>注册</status>
          </电子邮件>
        </电子邮件>
      </subroot>
     </root>
   

于 2014-02-24T11:30:29.077 回答