Reading from XML does not work

  • ALTER Procedure [dbo].[TestOpenXML]

    @strXML ntext

    AS

    Declare @xmlDat xml,

    @xmlPointer int

    SELECT @strXML = N''

    exec sp_xml_preparedocument @xmlPointer OUTPUT, @strXML

    INSERT INTO Employee(EmployeeName, EmployeeSalary) Select eName, eSalary From OpenXML(@xmlPointer,'/record/deptid/Employee',2) WITH (eName varchar(200) '@Name', eSalary real '@Salary')

    Go

  • Instead of using OpenXML you should look into XQuery.

    If you have trouble getting XQuery to work, please attach the XML file together with what you've tried and we probably can help you getting it to work.

    Edit: Regarding your code in prev. post: With the SELECT @strXML = N'' you set your xml document to an empty string and your variable @xmlDat is not used at all...



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • Hi

    Your XML is not valid.

    * XML is case sensitive

    * Start element "record" and end element "RECORD"

    * Start element "deptid" and end element "DEPT"

    * Start element "DEPTID" and end element "DEPT"

    Greets

    Flo

  • Florian Reischl (4/30/2009)


    Hi

    Your XML is not valid.

    * XML is case sensitive

    * Start element "record" and end element "RECORD"

    * Start element "deptid" and end element "DEPT"

    * Start element "DEPTID" and end element "DEPT"

    Greets

    Flo

    Hi Flo,

    were do you see the XML code? Attached a screenshot of the post as it's shown on my scrren (Firefox 2.0.0.9)... :doze:



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • Hey Lutz!

    Install Firefox 3.0.10, then err... click "Quote" at his post 😛

    Greets

    Flo

  • Spotted that and corrected the xml. Here it is:

    ''

  • ''

  • ''

  • Any good resources of starting with XQuery

  • Hi kavitus

    Still not valid:

    <DEPTID = "10">

    ... is missing the attribute qualifier.

    Tip for posting XML the best way is just attach as file.

    Greets

    Flo

  • kavitus (4/30/2009)


    Any good resources of starting with XQuery

    I found the following article very helpful:

    http://www.simple-talk.com/sql/t-sql-programming/xml-jumpstart-workbench/

    @Flo:

    The "quote-trick" works on my FireFox-Version, too... :w00t:



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • kavitus (4/30/2009)


    Any good resources of starting with XQuery

    There are some good example in BOL.

    Here a little sample for your XML. Do not copy the rendered code! It is damaged by the render engine of this forum... Use the "Quote" button for this post and copy the source from there.

    DECLARE @strXML NVARCHAR(MAX)

    SELECT @strXML = N'

    '

    Declare @xmlDat xml

    SELECT @xmlDat = @strXML

    SELECT

    T.C.value('../@id', 'int') DeptId,

    T.C.value('@Name', 'varchar(30)') Name,

    T.C.value('@Salary', 'int') Salary

    FROM @xmlDat.nodes('RECORD/DEPTID/Employee') T(C)

    Greets

    Flo

  • lmu92 (4/30/2009)


    @Flo:

    The "quote-trick" works on my FireFox-Version, too... :w00t:

    Just kidding ;-). The quote-trick works on every browser. The problem is not the browser but the java-script client-side render engine...

    Sorry

    Flo

  • To make your XML show up, you need to qoute it with [ code="xml"]...[/code] tags, instead of '...'. Like this:

    kavitus (4/30/2009)


    Click the QUOTE button on my post to see how I did this.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • Thanks guys. I guess the problem was with the XML not being valid.

Viewing 15 posts - 1 through 14 (of 14 total)

You must be logged in to reply to this topic. Login to reply