I have the table tblCollectionGameList
CREATE TABLE dbo.tblCollectionGameList
(
ListID smallint IDENTITY(0,1) NOT NULL,
CollectionID smallint NOT NULL,
IncludedSectionID smallint NOT NULL,
CONSTRAINT PK_CollectionGameList_ListID PRIMARY KEY CLUSTERED (ListID ASC),
CONSTRAINT FK_CollectionGameList_SectionInfo FOREIGN KEY (CollectionID) REFERENCES dbo.tblSectionInfo (SectionID),
CONSTRAINT FK_CollectionGameList_SectionInfo2 FOREIGN KEY (IncludedSectionID) REFERENCES dbo.tblSectionInfo (SectionID)
)
which I run the following MERGE command on after passing an XML set of values to from my website
SELECT
CAST(colx.query('data(CollectionID) ') AS varchar) AS CollectionID,
CAST(colx.query('data(IncludedSectionID) ') AS varchar) AS IncludedSectionID
INTO #TEMPtblCollectionGameList
FROM @XMLTable.nodes('DocumentElement/XMLTable') AS Tabx(Colx)
MERGE dbo.tblCollectionGameList AS t
USING #TEMPtblCollectionGameList AS s
ON (t.CollectionID = s.CollectionID AND t.IncludedSectionID= s.IncludedSectionID)
WHEN NOT MATCHED BY TARGET
THEN
INSERT (CollectionID, IncludedSectionID)
VALUES (s.CollectionID, s.IncludedSectionID)
WHEN NOT MATCHED BY SOURCE
THEN
DELETE
;
which is not working right. If I have an existing set of data in my table:
ListID CollectionID IncludedSectionID
34 86 0
35 86 1
and I try to place new data from my web page for a collection with a different CollecitonID it is removing the old data and placing the new data in:
ListID CollectionID IncludedSectionID
38 92 10
39 92 11
what is wrong with my MERGE code that is making it affect items outside the CollectionID that the webpage is passing for an update?
The solution was to add the following:
this was gather by the help at the guys from SQLteam.com.