Solved

msg213 error when trying to insert data

Posted on 2012-03-27
5
387 Views
Last Modified: 2012-04-08
Hello!

I am trying to learn more about xml and sql-server. I have a copy of the book Sql server 2005 express edition for dummies from which I try to follow the exact instructions. I have not found any information on the books website that anything is wrong so assume I  missunderstand some detail.

The problem (Msg 213, Level 16, State 1, Line 1
Column name or number of supplied values does not match table definition.
)
 arise when I try to execute the following command in MS sql server management studio:

INSERT INTO Shipments VALUES(1,'<Root>
<Parts OrderID="1" PurchaseOrderNumber="NSSDJS#1">
<DeliveryDate>2006-12-30</DeliveryDate>
<Items>
   <ShipmentWeight>2.44</ShipmentWeight>
     <LineItem>
	        <Name>DentaKit for Adults</Name>
	        <Code>DK-R001</Code>
	        <Price>29.95</Price>
     </LineItem>
     <LineItem>
	        <Name>Retainer Brite 1 Year</Name>
	        <Code>DK-RB1Y</Code>
	        <Price>35.00</Price>
     </LineItem>
</Items>
</Parts>
</Root>')
GO

Open in new window


Before, I have used these statements to create xm-schema and a table.

CREATE XML SCHEMA COLLECTION Parts AS
N'<?xml version="1.0" encoding="UTF-16"?>
<xsd:schema
 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <xsd:element name="parts">
  <xsd:complexType>
   <xsd:choice  minOccurs="0" maxOccurs="unbounded" > 
    <xsd:sequence>
     <xsd:element name="code" type="xsd:string"/>
     <xsd:element name="name" type="xsd:string"/>
     <xsd:element name="price" type="xsd:decimal"/>
    </xsd:sequence>
   </xsd:choice>
  </xsd:complexType>
 </xsd:element>
</xsd:schema>
'
CREATE TABLE Shipments
(
   ShipmentID INTEGER PRIMARY KEY NOT NULL,
   ShipmentDate DATETIME NOT NULL,
   ShippedParts XML (Parts)
)
GO

Open in new window


Please, give me a hint on what to change to make it work. More info can be found in attached file

Best regards
Marcus Steen
msg213.pdf
0
Comment
Question by:marcgu
  • 3
  • 2
5 Comments
 
LVL 32

Accepted Solution

by:
bhess1 earned 500 total points
Comment Utility
You are missing the date field.  With your table having a shipping id, a shipment date, and the  shippedParts XML field, you need to include all three in your VALUES statement.  Try this:

INSERT INTO Shipments VALUES(1, CURRENT_TIMESTAMP, 
'<Root>
<Parts OrderID="1" PurchaseOrderNumber="NSSDJS#1">
<DeliveryDate>2006-12-30</DeliveryDate>
<Items>
   <ShipmentWeight>2.44</ShipmentWeight>
     <LineItem>
	        <Name>DentaKit for Adults</Name>
	        <Code>DK-R001</Code>
	        <Price>29.95</Price>
     </LineItem>
     <LineItem>
	        <Name>Retainer Brite 1 Year</Name>
	        <Code>DK-RB1Y</Code>
	        <Price>35.00</Price>
     </LineItem>
</Items>
</Parts>
</Root>')
GO

Open in new window

0
 

Author Comment

by:marcgu
Comment Utility
Hi!

Thanks for your comment, It's leading me  closer to the solution, but still something is wrong because I now get the following error-message:

Msg 6913, Level 16, State 1, Line 1
XML Validation: Declaration not found for element 'Root'. Location: /*:Root[1]



I don't understand what I have to to do.
0
 
LVL 32

Expert Comment

by:bhess1
Comment Utility
Your Schema doesn't have the <Root> element defined in it.  In a CREATE XML SCHEMA COLLECTION declaration, the first element defined becomes the root element of the document.  So, in your case, the root element is <Parts>.

There is a much more detailed explanation in this article at Beyond Relational, coder's website that I really like.
0
 

Author Comment

by:marcgu
Comment Utility
Hi!

Thanks for the link.

 All of the examples in the article as well as this works now:

DECLARE @cust AS XML(Parts)
SET @cust = '<parts></parts>'

Open in new window


But, no matter how I try to change the code and remove <root></root> I, will still get msg6913 error message.

Msg 6913, Level 16, State 1, Line 1
XML Validation: Declaration not found for element 'Parts'. Location: /*:Parts[1]


For instance, I tried like this (and several other options)

INSERT INTO Shipments VALUES(1, CURRENT_TIMESTAMP, 
'<Parts OrderID="1" PurchaseOrderNumber="NSSDJS#1">
<DeliveryDate>2006-12-30</DeliveryDate>
<Items>
   <ShipmentWeight>2.44</ShipmentWeight>
     <LineItem>
	        <Name>DentaKit for Adults</Name>
	        <Code>DK-R001</Code>
	        <Price>29.95</Price>
     </LineItem>
     <LineItem>
	        <Name>Retainer Brite 1 Year</Name>
	        <Code>DK-RB1Y</Code>
	        <Price>35.00</Price>
     </LineItem>
</Items>
</Parts>')
GO

Open in new window




In case you or any one else has a final clue what´s going wrong for me, I will award you the points. You have actually answerd my original question. For me, it´s just annoying when I do not understand why I fail to follow the example.

/Marcus
0
 

Author Closing Comment

by:marcgu
Comment Utility
This solved my initial question perfectly.

However, the solution showed that I had more problems which I probably have to adress in a new question.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now