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:

&lt;ranks&gt;
    &lt;item&gt;&lt;itemID&gt;25&lt;/itemID&gt;&lt;categoryID&gt;1&lt;/categoryID&gt;&lt;question&gt;some text&lt;/question&gt;&lt;sortID&gt;1&lt;/sortID&gt;&lt;modBy&gt;abc&lt;/modBy&gt;&lt;/item&gt;
    &lt;item&gt;&lt;itemID&gt;12&lt;/itemID&gt;&lt;categoryID&gt;1&lt;/categoryID&gt;&lt;question&gt;some text 2&lt;/question&gt;&lt;sortID&gt;2&lt;/sortID&gt;&lt;modBy&gt;abc&lt;/modBy&gt;&lt;/item&gt;
    &lt;item&gt;&lt;itemID&gt;9&lt;/itemID&gt;&lt;categoryID&gt;1&lt;/categoryID&gt;&lt;question&gt;some text 3&lt;/question&gt;&lt;sortID&gt;3&lt;/sortID&gt;&lt;modBy&gt;abc&lt;/modBy&gt;&lt;/item&gt;
&lt;/ranks&gt;


Many thanks in advance for any help with this, Tim.


Question: Adjustable sample size in clinical trial
Answer: I think AdamO's answer is great, but I think it's also  worth emphasizing out that this adaptive sample size design is how many (maybe even most? I've done theoretical work during internships at pharm companies, but can't say I've ever planned a real study...) clinical trials are run. 

That is to say, if a sequential design is used, initial patients are recruited and treated. Part way through the study, the currently collected data gets analyzed. Three possible actions can occur at this point: the data may show a statistically significant result and the study will be stopped because efficacy has been demonstrated, the data many statistically significantly show that there is no strong effect (for example, the upper end of the confidence interval is below some clinically significant threshold) and the study is stopped due to futility or the data is not yet conclusive (i.e. both a clinically significant effect and a clinically insignificant effect are contained in the confidence interval) in which more data will be collected. So you can see that in this case, the sample size is not fixed. 

An important note about this: you can't just run a standard test each time you "check" your data, otherwise you are doing multiple comparisons! Because the test statistics at different times should be positively correlated, it's not as big an issue as standard multiple comparison issues, but it still should be addressed for proper inference. Clinical trials, being regulated by the FDA, must state a plan for how they will address this (as @AdamO points out, SeqTrial provides software for this). However, often times academic researchers, not being regulated by the FDA, will continue to collect data until they find significance without adjusting for the fact that they are doing several comparisons. It's not the biggest abuse of statistical practice in research, but it still is an abuse.