I need some help trying to import the following XML document
<ROOT>
<MeasuresList>
<Measure ID="164">
<Names>
<Name ID="1072687" langid="33"/>
<Name ID="1017672" langid="13">miljoner skär</Name>
</Names>
</Measure>
<Measure ID="362">
<Names>
<Name ID="1072687" langid="33"/>
<Name ID="1017672" langid="13">miljoner skär</Name>
<Name ID="4068857" langid="19">hyller</Name>
<Name ID="3330057" langid="18">ράφια</Name>
<Name ID="3291105" langid="20">raflar</Name>
<Name ID="2813622" langid="10"/>
</Names>
</Measure>
<Measure ID="162">
<Names>
<Name ID="1072687" langid="33"/>
<Name ID="1017672" langid="13">miljoner skär</Name>
<Name ID="1072427" langid="36"/>
<Name ID="438237" langid="20">sayfa başına geçen dakika</Name>
</Names>
</Measure>
</MeasuresList>
Into a dbo.table (ID int PRIMARY KEY, langid int FK, Value NVARCHAR(MAX)) I am using
ALTER PROCEDURE [dbo].[AddNewMeasuresList]
(
@XmlData XML
)
AS
DECLARE @doc int;
EXEC sp_xml_preparedocument @doc OUTPUT, @XmlData
BEGIN TRANSACTION T1
Insert Measure_Name
SELECT *
FROM OPENXML(@doc,'//ROOT/MeasuresList/Measure/Names/Name') WITH Measure_Name AS mes
WHERE NOT EXISTS
(
SELECT 1 FROM dbo.Measure_Name WHERE ID=mes.ID
)
COMMIT TRANSACTION T1
EXEC sp_xml_removedocument @doc
However i'm getting an exception in my c# application
System.Data.SqlClient.SqlException (0x80131904):
Violation of PRIMARY KEY constraint 'PK_Measure_Name'.
Cannot insert duplicate key in object 'dbo.Measure_Name'. The duplicate key value is (1072687)
The dbo.Measure_Name Table is empty but there are duplicate entries in other Measure XML nodes with the same Name ID.
Since my SELECT statement returns all "Name" which results in duplicate PRIMARY KEYS, how do i change the select statement to return all values but with only DISTINCT/UNIQUE ID's?