Inserting XML

  • I went with the suggestion by "J Livingston SQL" and got it right. The question as written seems to be misleading.

  • I looked at the code and there was obviously nothing wrong with it. 😎 So why say there was an error ?

    I looked at the options - they were all nonsense (the first option had no connection at all with the code; the fourth was nonsense, given the table definition; and the other three were just plain false).

    So I picked the first option, just to see the explanation: "The XML document needs to attribute for ID to be quoted" - that explanation isn't even English, it's just gibberish! But the words in it, although not strung together to mean anything, were clearly tied to the fourth option, so the table definition I had been looking at wasn't the intended table definition.

    What a strange question ! :laugh:

    Maybe I should have guessed, as did two earlier commenters, that the table create statement was for the wrong table, and thus the reason for no error; then it would have been easy.

    Tom

  • Mighty (6/1/2016)


    Choose the "correct" answer by eliminating the ones that were definitely correct.

    Agree with the rest, code is working and no error occurs. A bit strange though, because according to me the values are not well formed XML.

    From BOL

    Well-formed XML and the xml Data Type

    The xml data type implements the ISO standard xml data type. Therefore, it can store well-formed XML version 1.0 documents and also so-called XML content fragments with text nodes and an arbitrary number of top-level elements in an untyped XML column. The system checks that the data is well-formed, does not require the column to be bound to XML schemas, and rejects data that is not well-formed in the extended sense. This is true also of untyped XML variables and parameters.

    What, do you think that neither an XML document nor an XML content fragment can have a single element with a single attribute? I'm pretty sure that the ISO standard allows such a probably useless thing.

    Or do you perhaps think that "an arbitrary number" means "a number greater than one"?

    Tom

  • No applicable answer listed as the code is perfectly valid (maybe not valid on SQL Server 2000 but cannot be bothered to check).

    😎

  • Yeah, something was missed in the question itself. I read it several times and didn't see the problem, so I did what I normally do - go get coffee. I still didn't see it, ran the code, received no error and guessed at the answer. Maybe it's harder than we think to write code with errors when you try to. 😀

  • TomThomson (6/1/2016)


    What, do you think that neither an XML document nor an XML content fragment can have a single element with a single attribute? I'm pretty sure that the ISO standard allows such a probably useless thing."?

    My understanding is that a well formed XML document contains one single root element. I don't see the definition of that element in the value, and when is was converted, it was not added either.

  • Ed Wagner (6/1/2016)


    Yeah, something was missed in the question itself. I read it several times and didn't see the problem, so I did what I normally do - go get coffee. I still didn't see it, ran the code, received no error and guessed at the answer. Maybe it's harder than we think to write code with errors when you try to. 😀

    Probably Steve was using SqlPrompt with auto correction 😉

    😎

  • TomThomson (6/1/2016)


    What, do you think that neither an XML document nor an XML content fragment can have a single element with a single attribute? I'm pretty sure that the ISO standard allows such a probably useless thing.

    Or do you perhaps think that "an arbitrary number" means "a number greater than one"?

    Oops, overlooked that part on fragments.

    Found some references (in Pro T-SQL 2012 Programmer's Guide) where they say that there are more than one elements needed. So that seems to be wrong.

  • Alan.B (5/31/2016)


    I don't know why you get an error. This code

    CREATE TABLE Mytest (id INT, x XML)

    INSERT mytest

    VALUES

    (1, CAST('1200' AS XML))

    , (2, CAST( '1201' AS XML))

    ... works just fine on my system.

    works for me, too. SQL Server 2012

  • Happy beginning of a new week, try this :-):

    DECLARE @Mytest TABLE (id INT, x XML);

    INSERT @Mytest VALUES

    (1, CAST(1200 AS XML))

    , (2, CAST(1201 AS XML));

    https://msdn.microsoft.com/en-us/library/ms187928.aspx

  • This code work just fine it has no errors.

    CREATE TABLE Mytest (id INT, x XML)

    INSERT mytest

    VALUES

    (1, CAST('1200' AS XML))

    , (2, CAST( '1201' AS XML))

    select *

    from Mytest

    idx

    11200

    21201

  • My apologies here. I must have mixed up some code with the explanation, or maybe hadn't finished this and somehow put it for publication. No idea what I was thinking.

    I've edited the question and explanation. Points awarded back

  • I tried the modified code and it still works for me on SQL Server 2012

  • tom.w.brannon (6/1/2016)


    I tried the modified code and it still works for me on SQL Server 2012

    I know. Hilarious, ain't it.

  • OK, I found the issue. The editor stripped out some of the XML. Instead of seeing

    (1, CAST('<customer id = "1200">Acme</customer>' AS XML))

    the editor stripped this to:

    (1, CAST('Acme' AS XML))

    I've edited the HTML to use the codes for angle brackets and it should display correctly now.

    No excuses for the old explanation. Not sure why that was shown that way.

Viewing 15 posts - 16 through 30 (of 36 total)

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