看不太懂一个xml函数,请看懂的朋友告诉下
-- Return the value of the element by given the name of the element
CREATE FUNCTION dbo.fnXmlParserGetValueByName (@XmlString nvarchar(4000) , @Tag nvarchar(100))
RETURNS nvarchar(4000) AS
BEGIN
DECLARE @TagStart nvarchar(102) , @TagEnd nvarchar(103)
SET @TagStart = ' < ' + @Tag + '> '
SET @TagEnd = ' </ ' + @Tag + '> '
DECLARE @StartNo int, @EndNo int
SET @StartNo = CHARINDEX(@TagStart, @XmlString)
SET @EndNo = CHARINDEX(@TagEnd, @XmlString)
IF @StartNo = 0 OR @EndNo = 0
RETURN NULL
DECLARE @Value nvarchar(4000)
SET @Value = SUBSTRING(@XmlString, @StartNo + LEN(@TagStart) , @EndNo - @StartNo - LEN(@TagStart) )
RETURN @Value
END
测试代码:
declare @xmlstring nvarchar(4000)
DECLARE @TagStart nvarchar(102) , @TagEnd nvarchar(103),@Tag nvarchar(105),@StartNo nvarchar(10),@EndNo nvarchar (20)
set @xmlstring = ' <grp> Empty </grp> <params> <param> <name> RowNum </name> <CorpPhone> 52 </CorpPhone> </param> </params> <hgt> 85 </hgt> <description> 3 </description> 公司電話 '
set @tag = 'CorpPhone '
select @TagStart = ' < ' + @Tag + '> '
select @TagEnd = ' </ ' + @Tag + '> '
select @StartNo = CHARINDEX(@TagStart, @XmlString)
select @EndNo = CHARINDEX(@TagEnd, @XmlString)
select @TagStart
select @TagEnd
select len( 'CorpPhone ')
select @EndNo
------解决方案--------------------功能非常简单,就是返回xml里边一个元素的值.如> <CorpPhone> 52 </CorpPhone> 的值是52则
declare @xmlstring nvarchar(4000)
DECLARE @TagStart nvarchar(102),@Tag nvarchar(105)
set @xmlstring = ' <grp> Empty </grp> <params> <param> <name> RowNum </name> <CorpPhone> 52 </CorpPhone> </param> </params> <hgt> 85 </hgt> <description> 3 </description> 公司電話 '
set @tag = 'CorpPhone '
select dbo.fnXmlParserGetValueByName(@xmlstring,@tag)
返回的结果就是52
------解决方案--------------------有时间帮你看一下
------解决方案--------------------hehe,学习再学习。