开发者

Select values out of XML column

This is the str开发者_如何学JAVAucture of my table

CREATE TABLE [dbo].[TableA]
(
    [ObjectID] [int] IDENTITY(1,1) NOT NULL,
    [CGPracticeCode] [varchar](5) NULL,
    [TotalAmt] [decimal](11, 2) NULL,
    [SplitAmt] [xml] NULL,
)

The value in the SplitAmt Column are in the below format, no of rows may vary

'<Values>
  <Row>
    <PracticeCode>BE9</PracticeCode>
    <Value>20</Value>
  </Row>
  <Row>
    <PracticeCode>BEA</PracticeCode>
    <Value>3</Value>
  </Row>
</Values>'

Now How do I get values like this... (No problem in repeating 1st 3 columns)

ObjectID, CGPracticeCode, TotalAmt, PracticeCode, [Value]


Have a look at this article by Alex Homer

To return the first Row elements you can use:

select a.ObjectId, 
       a.CGPracticeCode, 
       a.TotalAmt, 
       a.SplitAmt.value('(/Values/Row/PracticeCode)[1]', 'nvarchar(50)') as PracticeCode, 
       a.SplitAmt.value('(/Values/Row/Value)[1]', 'int') as [Value]
from [dbo].[TableA] a 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜