SQL Server中的OpenXML用户说明
示例:
以下示例使用sp_xml_preparedocument创建 XML 图像的内部表示形式。然后对 XML 文档的内部表示形式执行使用OPENXML行集提供程序的SELECT语句。
declare @Pointer int
declare @xmlDOC nvarchar(1000)
set @xmlDOC='
<ROOT>
<Team LeagueTypeID="1" No="1" HostTeam="中国" QuestTeam="美国" LetBall="0" Time="2007-9-20 11:30:00"></Team>
<Team LeagueTypeID="1" No="2" HostTeam="hanguo" QuestTeam="ribn" LetBall="0" Time="2007-9-25 11:30:00"></Team>
</ROOT>'
execute sp_xml_preparedocument @Pointer output, @xmlDOC
select [No], MatchNumber, Game , HostTeam, QuestTeam, [Time]
from OPENXML (@Pointer,'/ROOT/Team')
with ([No] smallint, MatchNumber varchar(20),Game varchar(20), HostTeam varchar(20), QuestTeam varchar(20), [Time] varchar(20))
注意:
如果xml文本中存在中文,@xmlDOC不能声明为varchar类型,需声明为nvarchar类型,否则报错“An invalid character was found in text content.”