2012-07-22 137 views
1

我正在嘗試使用xml更新表。在SQL Server中使用XML更新

UPDATE dbo.M_Picture 
SET Sequence = T.c.query('Sequence') 
FROM dbo.M_Picture pic 
INNER JOIN @xml.nodes('/pictures/picture') T(c) 
ON pic.PictureId = T.c.query('pictureId') --I guess issue is in this line 

我使用的XML是

<pictures> 
    <picture> 
    <pictureId>30</pictureId> 
    <Sequence>4</Sequence> 
    </picture> 
    <picture> 
    <pictureId>31</pictureId> 
    <Sequence>4</Sequence> 
    </picture> 
</pictures> 

我得到的錯誤信息是

操作數類型衝突:XML是與詮釋

它不兼容有道理,因爲在

pic.PictureId=T.c.query('pictureId') 

pictureIdint

如何排序了這一點?

回答

2

試試這個:

UPDATE dbo.M_Picture 
SET Sequence = T.c.query('Sequence') -- this here *also* assign an XML fragment to "Sequence" 
FROM dbo.M_Picture pic 
INNER JOIN @xml.nodes('/pictures/picture') T(c) 
ON pic.PictureId = T.c.value('(pictureId)[1]', 'int') 

使用的XML的.value()方法 - 返回標型(如int

另外:我不知道,如果你真的想要分配的XML片段<Sequence>4</Sequence>添加到您的列Sequence - 或者您是否也想在那裏使用.value()方法!