Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Help with MSSQL XML query

Posted on 2014-03-06
1
Medium Priority
?
226 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

609 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