Solved

Help with MSSQL XML query

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

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 needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

840 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