开发者

sql server 2005 xquery read xml type

开发者 https://www.devze.com 2023-03-13 14:59 出处:网络
I have declared a @xmldata as xml. How to read it? 开发者_如何学GoI tried declare @xmldata as xml

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

精彩评论

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