开发者

sql server 2005 xquery read xml type

I have declared a @xmldata as xml. How to read it? 开发者_如何学GoI tried

declare @xmldata as xml

set @xmldata = <root><row ename='abc' eid='1'/></root>

SELECT @XMLDATA

but it returned an error. I want to fetch eid column from the above xml type.


Try this:

declare @xmldata as xml
set @xmldata = '<root><row ename="abc" eid="1"/></root>'

SELECT    
   @xmldata.value('(/root/row/@eid)[1]', 'int') AS 'EID'

Update: if you need to select from your table and extract something from the XML column, use this approach:

SELECT
     tbl.ID,
     tbl.XmlColumn.value('(/root/row/@ename)[1]', 'varchar(25)') AS 'EName'
FROM
   dbo.YourTable tbl
WHERE 
   (...some condition here...)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜