0
XML是否有可能產生使用下面的XML「FOR XML」在SQL Server生產從多個表中的SQL Server
<Person>
<HomeID>1</HomeID>
<DayID>1</DayID>
<String>I get up at 07:00</String>
<String>I have breakfast at 07:30</String>
<String>I go to office at 08:00</String>
<String>I have lunch at 13:00</String>
<String>I come back from office at 17:00</String>
<String>I have dinner at 19:00</String>
<String>I sleep at 21:30</String>
<DayID>2</DayID>
<String>I get up at 08:00</String>
<String>I have breakfast at 08:30</String>
<String>I have lunch at 13:00</String>
<String>I have dinner at 20:00</String>
<String>I sleep at 23:00</String>
</Person>
<Person>
<HomeID>2</HomeID>
<DayID>1</DayID>
<String>I get up at 07:00</String>
<String>I have breakfast at 07:30</String>
<String>I go to office at 08:00</String>
<String>I have lunch at 13:00</String>
<String>I come back from office at 17:00</String>
<String>I have dinner at 19:00</String>
<String>I sleep at 21:30</String>
<DayID>2</DayID>
<String>I get up at 08:00</String>
<String>I have breakfast at 08:30</String>
<String>I have lunch at 13:00</String>
<String>I have dinner at 20:00</String>
<String>I sleep at 23:00</String>
<Person>
我最初的嘗試是非常糟糕的。
Select HomeID,
(
Select DayID,
(
SELECT TB2.RndString+' '+CAST(TB1.timevalue AS varchar(5))
FROM TB1,TB2
where TB1.DayID=TB2.DayType and TB1.TimeCode=TB2.StringCode
FOR XML PATH ('String'), TYPE
)
from TB1
for XML AUTO, TYPE
)
from TB1
for XML AUTO, ELEMENTS
我有兩個表,TB1和TB2。在TB1
字段是HomeID,大衛·,時間碼,TIMEVALUE。
HomeID DayID TimeCode timevalue
1 1 1 07:00:00
1 1 2 07:30:00
1 1 3 08:00:00
1 1 4 13:00:00
1 1 5 17:00:00
1 1 6 19:00:00
1 1 7 21:30:00
1 2 1 08:00:00
1 2 2 08:30:00
1 2 3 13:00:00
1 2 4 20:00:00
1 2 5 23:00:00
2 1 1 08:00:00
2 1 2 08:30:00
2 1 3 09:00:00
2 1 4 13:00:00
2 1 5 18:00:00
2 1 6 20:00:00
2 1 7 22:00:00
2 2 1 09:00:00
2 2 2 10:00:00
2 2 3 13:00:00
2 2 4 19:00:00
2 2 5 22:30:00
TB2中的字段是DayType,StringCode,RndString。
DayType StringCode RndString
1 1 I get up at
1 2 I have breakfast at
1 3 I go to office at
1 4 I have lunch at
1 5 I come back from office at
1 6 I have dinner at
1 7 I sleep at
2 1 I get up at
2 2 I have breakfast at
2 3 I have lunch at
2 4 I have dinner at
2 5 I sleep at
注: TB1.DayID = TB2.DayType和TB1.TimeCode = TB2.StringCode
這是不是你正在尋找的XML。 XML結構和您使用的縮進不匹配。以下是您的XML實際外觀的更好視圖。 http://pastebin.com/Ub8AAwAG –