在XML列中搜索字符串的过程

时间:2013-02-27 19:10:32

标签: sql-server sql-server-2008 tsql

我有一个程序,我想用它来搜索xml列并返回我搜索的值。现在我只想让它返回值本身。就像现在一样,它返回正确的预期行数,但返回的值为null!我添加了演员,但它返回相同!

有关如何获取搜索字符串而不仅仅是null的任何想法?谢谢!

enter image description here

CREATE PROCEDURE [dbo].[FindString]
    @findString NVARCHAR(100)
AS
BEGIN

    SET NOCOUNT ON

;WITH XMLNAMESPACES (Default 'http://www.w3.org/2001/XMLSchema' )

SELECT CAST(x.value('Value[1]', 'varchar(30)') As varchar(30)) As Value

FROM dbo.XmlTable x

CROSS APPLY x.XmlDocument.nodes('/*') a(x)

WHERE XmlDocument.exist('//*/text()[contains(., sql:variable("@findString"))]') = 1

    RETURN
END


GO

1 个答案:

答案 0 :(得分:1)

这有用吗?

CREATE PROCEDURE [dbo].[FindString]
    @findString NVARCHAR(100)
AS
BEGIN

    SET NOCOUNT ON

;WITH XMLNAMESPACES (Default 'http://www.w3.org/2001/XMLSchema' )

SELECT CAST(x.value('.', 'varchar(30)') As varchar(30)) As Value

FROM dbo.XmlTable x

CROSS APPLY x.XmlDocument.nodes('//*[contains(text()[1], sql:variable("@findString"))]') a(x)

    RETURN
END


GO