Вопрос

I have xml which is passed as input to a stored procedure for a bulk insert. My xml as below,

NewDataSet>
- <Table1>
  <StationId>1</StationId> 
  <DateOfEvent>2013-11-15</DateOfEvent> 
  <Year>2013</Year> 
  <WeekOfYear>46</WeekOfYear> 
  <MonthOfYear>11</MonthOfYear> 
  <ItemType>CATEGORY</ItemType> 
  <ItemName>10234</ItemName> 
  <Clicks>1</Clicks> 
  <SiteName>sandvikas</SiteName> 
  </Table1>

I have written a stored procedure as below,

ALTER PROCEDURE [dbo].[InsertQuestion]
    (

        @XmlQuestionMeasurements xml

    )
AS
BEGIN


    DECLARE @docHandle int

    EXEC sp_xml_preparedocument @docHandle OUTPUT, @XmlQuestionMeasurements

    --insert data in to EP_QuestionMeasurement table
    Insert into Analytics(StationId,DateOfEvent,Year,WeekOfYear,MonthOfYear,ItemType,ItemName,Clicks,SiteName)
    SELECT StationId,DateOfEvent,Year,WeekOfYear,MonthOfYear,ItemType,ItemName,Clicks,SiteName
    FROM Openxml( @docHandle, '/NewDataSet/Table1', 2) 
    WITH ( StationId int,DateOfEvent date,  Year int ,WeekOfYear int,MonthOfYear int, ItemType VARCHAR(50),ItemName VARCHAR(50),Clicks int,SiteName VARCHAR(50)  ) 

END

Its inserts well. Now my issue is how i modify the stored procedure to prevent insert if same set of xml is passed again, To check if same data exists then prevent insert. Need to check condition that if this itemname exists in this date then no insert.

Regards

Это было полезно?

Решение

You may be able to do a MERGE--that would insert if the data is not already present and would update if it is.

If that doesn't suit you, you could also try inserting from the XML into a temp table, and then from the temp table into Analytics where tempTable.ItemName NOT IN(SELECT ItemName FROM Analytics) (assuming I'm understanding you correctly that ItemName is going to be distinct for all Analytics)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top