?
Solved

Help with MSSQL XML query

Posted on 2014-03-06
1
Medium Priority
?
223 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

771 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