我需要一些帮助来尝试导入以下 XML 文档
<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>
进入我正在使用的 dbo.table (ID int PRIMARY KEY, langid int FK, Value NVARCHAR(MAX))
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
但是我在我的 c# 应用程序中遇到了一个异常
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)
dbo.Measure_Name 表为空,但在其他具有相同名称 ID 的 Measure XML 节点中存在重复条目。
由于我的 SELECT 语句返回所有导致重复主键的“名称”,我如何更改选择语句以返回所有值但只有 DISTINCT/UNIQUE ID?