0

I am attempting to both create a table, with two columns, and to insert an XML document's data into the table. I am currently unable to get the code to run, so I am unsure of what could be causing issues. Any help would be greatly appreciated. Thank you.

CREATE TABLE TestProc(
TestProcID INT IDENTITY PRIMARY KEY,
Procedures XML,
);

DECLARE @TestProcXML xml;
SET @TestProcXML =
'
<TestProc>
<Proc>
<Description>the first procedure.</Description>
<SubProc>the first inner-proc.</SubProc>
<SubProc>the second inner-proc.</SubProc>
</Proc>
<Proc>
<Description>the second procedure.</Description>
</Proc>
<Proc>
<Description>the third procedure.</Description>
</Proc>
</TestProc>
'
INSERT INTO TestProc(Procedures);
4
  • Primary key declaration seems buggy. It should be TestProcID int IDENTITY(1,1) PRIMARY KEY, Commented Nov 30, 2018 at 0:24
  • You can also try INSERT INTO TestProc (1, Procedures); Commented Nov 30, 2018 at 0:24
  • It is not completely clear from the quest exactly what the error you are getting is. Actually including the error message will improve your question. I have taken a stab at providing an answer by giving you a 'corrected' version of your sample. If that doesn't solve your question you will need to give more information. Commented Nov 30, 2018 at 1:46
  • You are telling where to insert (insert something into table TestProc into the column Procedures, but forgot to tell the command the content to insert. As told in one answer you can either use VALUES or simply SELECT Commented Nov 30, 2018 at 12:03

2 Answers 2

2

Although not completely clear on the issue, there are several elements of the code provided that could cause a failure.

I have provided a corrected version below. (NOTE: This was tested to work in MS SQL Server 2012)

CREATE TABLE TestProc
(
    TestProcID INT IDENTITY(1,1) PRIMARY KEY,
    Procedures XML,
);

DECLARE @TestProcXML xml;
SET @TestProcXML = '
    <TestProc>
        <Proc>
            <Description>the first procedure.</Description>
            <SubProc>the first inner-proc.</SubProc>
            <SubProc>the second inner-proc.</SubProc>
        </Proc>
        <Proc>
            <Description>the second procedure.</Description>
        </Proc>
        <Proc>
            <Description>the third procedure.</Description>
        </Proc>
    </TestProc>
    '

INSERT INTO TestProc(Procedures)
VALUES (@TestProcXML)

Specific fixes included are:

  • IDENTITY -> IDENTITY(1,1)
  • Added VALUES section to insert so there is data actually inserted into the table
Sign up to request clarification or add additional context in comments.

1 Comment

The first fix is rather - uhm - not needed, while the second - quite probably - is the spot to push :-)
0

You can simply select the values when you inserting them into table just like below. This is the easiest way to inserting according to your scenario.

 CREATE TABLE TestProc(
    TestProcID INT IDENTITY(1,1) PRIMARY KEY,
    Procedures XML,
    );

    DECLARE @TestProcXML xml;
     SELECT @TestProcXML =
    '
    <TestProc>
    <Proc>
    <Description>the first procedure.</Description>
    <SubProc>the first inner-proc.</SubProc>
    <SubProc>the second inner-proc.</SubProc>
    </Proc>
    <Proc>
    <Description>the second procedure.</Description>
    </Proc>
    <Proc>
    <Description>the third procedure.</Description>
    </Proc>
    </TestProc>
    '


    INSERT INTO dbo.TestProc
            (Procedures)
    SELECT @TestProcXML

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.