Results 1 to 4 of 4

Thread: [RESOLVED] Help, only returning first result

  1. #1

    Thread Starter
    Hyperactive Member mulhearn22's Avatar
    Join Date
    Jun 2007
    Location
    Cherry Hill, NJ
    Posts
    347

    Resolved [RESOLVED] Help, only returning first result

    Hi,

    I am trying to create a temp table from an XML input. For whatever reason, it's only returning the first XML node in the table. Help as to why this is happening would be great!!

    XML:
    '<searchTerms><term>a</term><term>b</term><term>c</term></searchTerms>'

    SQL:
    ALTER PROCEDURE [dbo].[sp_BasicSearch]
    @searchXML XML
    AS
    BEGIN
    SET NOCOUNT ON;

    --declare handle
    DECLARE @handle INT
    --prepare xml document
    EXEC sp_xml_preparedocument @handle OUTPUT, @searchXML

    SELECT * INTO #xml
    FROM OPENXML (@handle, 'searchTerms')
    WITH (term varchar(100) 'term')

    SELECT * FROM #xml
    RETURN
    END

    Result: 'a'
    VS 2010 / .NET 4.0 / ASP.NET 4.0

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Help, only returning first result

    I know very little about XML - but after looking at the books-online for OPENXML I modified your xml-string a bit to look like this

    Code:
    DECLARE @idoc int
    DECLARE @doc varchar(1000)
    SET @doc ='<searchTerms><term termID="A"></term><term termID="B"></term></searchTerms>'
    --Create an internal representation of the XML document.
    EXEC sp_xml_preparedocument @idoc OUTPUT, @doc
    -- Execute a SELECT statement that uses the OPENXML rowset provider.
    SELECT    *
    FROM       OPENXML (@idoc, '/searchTerms/term',1)
                WITH (termID  varchar(10))
    and it returns

    Code:
    termID     
    ---------- 
    A
    B
    
    (2 row(s) affected)
    ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/8088b114-7d01-435a-8e0d-b81abacc86d6.htm

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    Hyperactive Member mulhearn22's Avatar
    Join Date
    Jun 2007
    Location
    Cherry Hill, NJ
    Posts
    347

    Re: Help, only returning first result

    Thank you! That worked. Much appreciated!
    VS 2010 / .NET 4.0 / ASP.NET 4.0

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [RESOLVED] Help, only returning first result

    This also works - and respects the original XML string.

    You need to specify the node "/term" and use a flags value of 1 and then ask for text() in the column pattern.

    Code:
    Create PROCEDURE [dbo].[sp_BasicSearch]
    @searchXML XML
    AS
    BEGIN
    SET NOCOUNT ON;
    select @searchxml
    --declare handle
    DECLARE @handle INT
    --prepare xml document
    EXEC sp_xml_preparedocument @handle OUTPUT, @searchXML
    
    select * FROM OPENXML (@handle, '/searchTerms/term',1)
    WITH (term varchar(100) 'text()')
    
    RETURN
    END
    
    
    go
    
    Exec [dbo].[sp_BasicSearch] '<searchTerms><term>a</term><term>b</term><term>c</term></searchTerms>'
    this returns

    Code:
    term                                                                                                 
    ---- 
    a
    b
    c
    This is pretty complicated stuff - the options available are extensive but seem well documented.

    Good luck!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width