Solved

Help with MSSQL XML query

Posted on 2014-03-06
1
215 Views
Last Modified: 2014-03-06
Hello I have 2 tables

Programme:  ProgrammeID, Start Date, End Date
Title:  ID, ProgrammeID, Language, Title

I need a query that will return
<programme startdate='blah' enddate='blah'>
     <title lang='en'>Some Title</title>
     <title lang='de'>Some other title</title>
</programme>

My problem is that usually when I have a structure like this I would return
<programme startdate='blah' enddate='blah'>
   <titles>
     <title lang='en'>Some Title</title>
     <title lang='de'>Some other title</title>
   </titles>
</programme>

but I need the first XML structure

What I have so far that is totally wrong is

      SELECT e.[startdate] as '@start'
              ,e.[enddate]  as '@end'
              ,e.MediaItemID as '@channel'
                  ,(
                        SELECT
                              lang as '@lang'
                              ,title as 'title'
                        FROM dbo.[MediaItemEPGtitle] et
                        WHERE e.[MediaItemEPGID] = et.[MediaItemEPGID]
                        FOR XML path ('title')
                    )  as title
              --,case e.[previouslyShown] when 0 then '' end as [previously-shown]
              --,case e.[previouslyShown] when 2 then '' end as [new]

        FROM [dbo].[MediaItemEPG] e
        WHERE (e.enddate between @StartDate and @EndDate) or  (e.startdate  between @StartDate and @EndDate)
        FOR XML PATH('programme'), TYPE, ROOT('tv') ;
0
Comment
Question by:KConner32
1 Comment
 
LVL 2

Accepted Solution

by:
KConner32 earned 0 total points
ID: 39909429
Figured it out

      SELECT e.[startdate] as '@start'
              ,e.[enddate]  as '@end'
              ,e.MediaItemID as '@channel'
                  ,(
SELECT (
SELECT lang, title
FROM MediaItemEPGtitle et
WHERE e.[MediaItemEPGID] = et.[MediaItemEPGID]
for xml auto, type).query('
for $sp in /*
return
<title lang="{data($sp/@lang)}" >{data($sp/@title)}</title>')
                  )  
              --,case e.[previouslyShown] when 0 then '' end as [previously-shown]
              --,case e.[previouslyShown] when 2 then '' end as [new]

        FROM [dbo].[MediaItemEPG] e
        WHERE (e.enddate between @StartDate and @EndDate) or  (e.startdate  between @StartDate and @EndDate)
        FOR XML PATH('programme'), TYPE, ROOT('tv') ;
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question