Solved

Help with MSSQL XML query

Posted on 2014-03-06
1
209 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now