Appending Text to FOR XML SQL Select Statement

天大地大妈咪最大 提交于 2019-12-11 20:01:52

问题


Hello from an SQL nO0b,
I have three Select statements I want to join:

Select 2 is a FOR XML Select to which I want to append Selects 1 & 3 (which simply display some text) as a kind of header/footer.

All three Selects work as intended and form three parts of a nicely-formed podcast .xml, but I need them to end up in one result so I can eventually save it to an .xml file (which will be my next headache, I suppose).

I have tried permutations of +, UNION, and JOIN that my inexperienced mind can come up with, but to no avail.

Select #1 (simply some text)

Select  '<?xml version=''1.0''?>
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"  xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
    <channel>
      <atom:link href="http://www.mysite.com/podcast/podcast.xml" rel="self" type="application/rss+xml" />
  <itunes:image href="http://www.mysite.com/Portals/0/Images/Audio/podcast_logo.jpg"/>
  <link>http://www.mysite.com/Audio.aspx</link>
<title>My Podcast</title>
<description>My Podcast's Description</description>
<language>en-us</language>
<copyright>&#xA9; 2010</copyright>
      <itunes:subtitle>Subtitle</itunes:subtitle>
      <itunes:owner>
        <itunes:name>Name</itunes:name>
        <itunes:email>user@domain.com</itunes:email>
      </itunes:owner>
      <itunes:category text="Religion &amp; Spirituality">
        <itunes:category text="Christianity" />
      </itunes:category>
      <itunes:explicit>No</itunes:explicit>
      <ttl> 120 </ttl>'   

Select #2 (my data)

Select 
(Select Cast(FieldValue as nvarchar(max)) from dbo.UserDefinedData where UserDefinedFieldId = 305 and UserDefinedRowId = item.UserDefinedRowId) as [title]
,(Select 'Today''s Program is ' + Cast(FieldValue as nvarchar(max)) from dbo.UserDefinedData where UserDefinedFieldId = 305 and UserDefinedRowId = item.UserDefinedRowId) as [description]  
,(Select Cast(Cast(FieldValue as nvarchar(max)) as DateTime) from dbo.UserDefinedData where UserDefinedFieldId = 306 and UserDefinedRowId = item.UserDefinedRowId) as [pubdate]
,(Select 'http://www.mysite.com/Portals/0/AudioFiles/RadioArchives/' + Cast(FieldValue as nvarchar(max)) from dbo.UserDefinedData where UserDefinedFieldId = 308 and UserDefinedRowId = item.UserDefinedRowId) as [guid]
 From 
dbo.UserDefinedRows item
 Where 
ModuleId = 820
 and UserDefinedRowID in (select UserDefinedRowID from UserDefinedData where UserDefinedFieldID = 306 and Cast(Cast(FieldValue as nvarchar(max)) as DateTime) between '2010-Nov-11' and '2010-Nov-18') 
 Order By 
[pubdate] DESC
for XML AUTO,ELEMENTS

Select #3 (more text)

Select '</channel>
</rss>'

(I am using MS SQLSMS 2008.) (The complex nature of the second select statement is due to the fact that it is retrieving data from a DotNetNuke "User-Defined Table/Form & List" which must first be "flattened".)

Any help would be appreciated, Thanks.


回答1:


Declare @V1 VarChar(Max), @V2 VarChar(Max), @V3 VarChar(Max)

Select @V1 = ...

Set @V2 = (Select ...)

Select @V3 = ...

Select @V1+@V2+@V3

Alternatively, cast #1 and #3 to XML.



来源:https://stackoverflow.com/questions/4217866/appending-text-to-for-xml-sql-select-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!