Click to See Complete Forum and Search --> : [RESOLVED] Import XML to SQL


vuyiswam
July 20th, 2009, 08:01 AM
Good Day all

I have a Sp that is written like this

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go




ALTER PROCEDURE [dbo].[Import_RegistrationsXM] @xml ntext

AS

set nocount on

DECLARE @doc int
EXEC sp_xml_preparedocument @doc OUTPUT, @xml

TRUNCATE TABLE _RegistrationXM
INSERT INTO _RegistrationXM
SELECT *
FROM OPENXML ( @doc , 'Import/Item' , 2)
WITH (
Student varchar(32),
SubjectCode varchar(32),
Campus varchar(32),
Repeat bit
)
EXEC sp_xml_removedocument @doc

truncate table _Duplicates

exec( 'sp_RegistrationsXM_Populate' )

select 'XM Registrations successfully imported!' [Result]

i have an XML File that is 3.21mb, when i this Sp with the XML i get the Following Error
[code]
Exception caught in: ExecuteStoredProc: Cannot insert duplicate key row in object 'dbo._RegistrationXM' with unique index '_RegistrationXM_index'. The statement has been terminated. [/pre]

in the table _RegistrationXM there are no Records and in the File there are no Duplicates too.

Thank you

Shuja Ali
July 20th, 2009, 03:16 PM
Well the SQL server says that there are duplicates. May you are missing a constraint or something in your table that has gone un-noticed. Take a closer look at your tables structure and verify that all constraints are setup properly. Also take a closer look at the XML. Load it into a temporary table first that doesn't have any constraints defined and then you can verify what is wrong with the data.

vuyiswam
July 21st, 2009, 02:25 AM
The Problem was that i had an unique index on a certain field.

Thanks