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...)
精彩评论