2012-12-31 67 views
0

執行下面的查詢xml auto或xml路徑?

SELECT  AssessmentFileGUID as '@FileId',AssessmentFileTypeId as '@FileTypeId',AssessmentFileName 
FROM   rat.AssessmentFile where AssessmentId=17 
for xml path('File'),root('Files') 

給了我下面的結果:

<Files> 
    <File FileId="2A23D836-612F-418E-BDE8-F182C5432A0D" FileTypeId="1"> 
    <AssessmentFileName>File-123213.pdf</AssessmentFileName> 
    </File> 
    <File FileId="CDA853B9-C587-4365-BAF5-972F8D217BAC" FileTypeId="2"> 
    <AssessmentFileName>File-343455.png</AssessmentFileName> 
    </File> 
</Files> 

不過,我需要它的格式如下:

<Files> 
    <File FileId="2A23D836-612F-418E-BDE8-F182C5432A0D" FileTypeId="1">File-123213.pdf</File> 
    <File FileId="CDA853B9-C587-4365-BAF5-972F8D217BAC" FileTypeId="2">File-343455.png</File> 
</Files> 

回答

2

指定應該爲內容作爲data()列,它應該在正確的地方去:

declare @T table (
    AssessmentFileGUID uniqueidentifier not null, 
    AssessmentFileTypeId int not null, 
    AssessmentFileName varchar(max) not null, 
    AssessmentId int not null 
) 
insert into @T(AssessmentFileGUID,AssessmentFileTypeId,AssessmentFileName, 
    AssessmentId) values 
('2A23D836-612F-418E-BDE8-F182C5432A0D',1,'File-123213.pdf',17), 
('CDA853B9-C587-4365-BAF5-972F8D217BAC',2,'File-343455.png',17) 

SELECT  AssessmentFileGUID as '@FileId',AssessmentFileTypeId as '@FileTypeId', 
    AssessmentFileName as 'data()' //Change here 
FROM   @T where AssessmentId=17 
for xml path('File'),root('Files') 

結果:

<Files> 
    <File FileId="2A23D836-612F-418E-BDE8-F182C5432A0D" FileTypeId="1">File-123213.pdf</File> 
    <File FileId="CDA853B9-C587-4365-BAF5-972F8D217BAC" FileTypeId="2">File-343455.png</File> 
</Files> 
1

不知道,但嘗試這個

SELECT  
    AssessmentFileGUID as '@FileId', 
    AssessmentFileTypeId as '@FileTypeId', 
    AssessmentFileName as "File/text()" 
FROM   
    rat.AssessmentFile 
where 
    AssessmentId = 17 
FOR XML PATH('File'), ROOT('Files') 
+0

,讓我 <文件的fileid =」 2A23D836-612F-418E-BDE8-F182C5432A0D「FileTypeId =」1「> 歡迎使用HDFC Bank NetBanking-4-12-12.pdf <文件的fileid = 「CDA853B9-C587-4365-BAF5-972F8D217BAC」 FileTypeId = 「2」> Ipad_safari,文件上傳,issue.png

+1

對不起比拉爾了錯誤的答案,我不確定一下,但是你可以試試這個 SELECT AssessmentFileGUID as'File/@ FileId',AssessmentFileTypeId as'File/@ FileTypeId',AssessmentFileName as'File/text()「 FROM rat.AssessmentFile其中AssessmentId = 17 for xml路徑('文件') – Manibhadra

+0

謝謝!但這工作對我來說: AssessmentFileName as'data() 謝謝你的嘗試! :) –