this is the first time I am working with an XML input in SQL.
I created the following procedure to insert all records from my XML string into my table which works well so far.
Can someone tell me how I have to change this so that it only inserts a record as new if the itemID (every record in my XML has this as well) does not yet exist in my table, column itemID - otherwise it should update the existing record with the new data from the XML.
I know how to use IF NOT EXISTS and UPDATE in general but am not sure how to realise this with an XML string as the input.
My procedure (so far):
ALTER PROCEDURE [dbo].[editor_UpdateQuestions]
@xml xml
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO editor_Questions
(
categoryID,
question,
sortID,
modDate,
modBy
)
SELECT ParamValues.x1.value('categoryID[1]', 'int'),
ParamValues.x1.value('question[1]', 'nvarchar(1000)'),
ParamValues.x1.value('sortID[1]', 'int'),
GETDATE(),
ParamValues.x1.value('modBy[1]', 'varchar(50)')
FROM @xml.nodes('/ranks/item') AS ParamValues(x1)
END
Example XML input:
<ranks>
<item><itemID>25</itemID><categoryID>1</categoryID><question>some text</question><sortID>1</sortID><modBy>abc</modBy></item>
<item><itemID>12</itemID><categoryID>1</categoryID><question>some text 2</question><sortID>2</sortID><modBy>abc</modBy></item>
<item><itemID>9</itemID><categoryID>1</categoryID><question>some text 3</question><sortID>3</sortID><modBy>abc</modBy></item>
</ranks>
Many thanks in advance for any help with this, Tim.