Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 807871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:28:17+00:00 2026-05-15T00:28:17+00:00

I am passing a XML document as a input to a stored procedure in

  • 0

I am passing a XML document as a input to a stored procedure in Microsoft SQL Server 2005.
This is the sample XML being passed as input

<Strategy StrategyID="0" TOStrategyID="8" ShutdownQtySell="1" ShutdownQtyBuy="1">
      <ParameterRange ParameterSetID="6" ParameterRangeID="1" ParameterRangeFrom="0" ParameterRangeTo="20" ParameterAutoTakeOut="False">
      </ParameterRange>
      <ParameterRange ParameterSetID="6" ParameterRangeID="4" ParameterRangeFrom="21" ParameterRangeTo="40" ParameterAutoTakeOut="False">
      </ParameterRange>
      <ParameterRange ParameterSetID="6" ParameterRangeID="5" ParameterRangeFrom="41" ParameterRangeTo="60" ParameterAutoTakeOut="False">
      </ParameterRange>
      <ParameterRange ParameterSetID="6" ParameterRangeID="6" ParameterRangeFrom="61" ParameterRangeTo="80" ParameterAutoTakeOut="False">
      </ParameterRange>
      <ParameterRange ParameterSetID="6" ParameterRangeID="7" ParameterRangeFrom="81" ParameterRangeTo="100" ParameterAutoTakeOut="False">
      </ParameterRange>
</Strategy>

I am able to retrieve the data using OpenXML functionality in SQL server

I am using this to get the data corresponding to ParameterRange rows

SELECT ParameterRangeID as iRangeID,  
       ParameterSetID as iSetID,  
       ParameterRangeFrom as fRangeFrom,  
       ParameterRangeTo as fRangeTo,  
       ParameterAutoTakeOut as bTakeoutEnabled  
 FROM OPENXML(@idoc, '/Strategy/ParameterRange', 1)  
 WITH (ParameterSetID int,ParameterRangeID int,ParameterRangeFrom float,ParameterRangeTo float,ParameterAutoTakeOut bit)  

Now, I need to insert/update these rows into a table TempRanges which has (iRangeID,iSetID) as the primary key.
If there is a row with the primary key, I want to update it the latest values and If there is no row with that primary key, I need to insert into the table.

How can I accomplish this inside the Stored Procedure ?

Thanks,
Sri

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T00:28:18+00:00Added an answer on May 15, 2026 at 12:28 am

    What you are trying to achieve is called an UPSERT. SQL2008 supports this directly through MERGE

    For SQL2005 you will need to break it into two operations. One UPDATE using an INNER JOIN on key and one INSERT using an OUTER JOIN for the extra records.

    SET NOCOUNT ON
    
    IF (OBJECT_ID('tempdb..#TempRanges') IS NULL)
    BEGIN
    CREATE TABLE #TempRanges(
        [iRangeID] [int] NOT NULL,
        [iSetID] [int] NOT NULL,
        [fRangeFrom] [float] NOT NULL,
        [fRangeTo] [float] NOT NULL,
        [bTakeoutEnabled] [bit] NOT NULL,
     CONSTRAINT [PK_TempRanges] PRIMARY KEY CLUSTERED 
    (
        [iRangeID] ASC,
        [iSetID] ASC
    )
    )
    END
    ELSE
    BEGIN
    DELETE FROM #TempRanges
    INSERT INTO #TempRanges([iRangeID], [iSetID], [fRangeFrom], [fRangeTo], [bTakeoutEnabled])
    SELECT '1', '6', '0', '20', '0' UNION ALL
    SELECT '4', '6', '21', '40', '0' UNION ALL
    SELECT '5', '6', '999', '60', '0' UNION ALL
    SELECT '6', '6', '61', '80', '0' 
    END
    
    
    
    DECLARE @doc XML
    
    SET @doc = '<Strategy StrategyID="0" TOStrategyID="8" ShutdownQtySell="1" ShutdownQtyBuy="1">
          <ParameterRange ParameterSetID="6" ParameterRangeID="1" ParameterRangeFrom="0" ParameterRangeTo="20" ParameterAutoTakeOut="False">
          </ParameterRange>
          <ParameterRange ParameterSetID="6" ParameterRangeID="4" ParameterRangeFrom="21" ParameterRangeTo="40" ParameterAutoTakeOut="False">
          </ParameterRange>
          <ParameterRange ParameterSetID="6" ParameterRangeID="5" ParameterRangeFrom="41" ParameterRangeTo="60" ParameterAutoTakeOut="False">
          </ParameterRange>
          <ParameterRange ParameterSetID="6" ParameterRangeID="6" ParameterRangeFrom="61" ParameterRangeTo="80" ParameterAutoTakeOut="False">
          </ParameterRange>
          <ParameterRange ParameterSetID="6" ParameterRangeID="7" ParameterRangeFrom="81" ParameterRangeTo="100" ParameterAutoTakeOut="False">
          </ParameterRange>
    </Strategy>'
    
    DECLARE @idoc INT
    
    EXEC sp_xml_preparedocument @idoc OUTPUT, @doc
    
    
    UPDATE    #TempRanges
    SET    fRangeFrom =ParameterRangeFrom, fRangeTo =ParameterRangeTo, bTakeoutEnabled =ParameterAutoTakeOut
    FROM OPENXML(@idoc, '/Strategy/ParameterRange', 1)
    WITH (ParameterSetID int,ParameterRangeID int,ParameterRangeFrom float,ParameterRangeTo float,ParameterAutoTakeOut bit)  AS input
    INNER JOIN #TempRanges ON #TempRanges.iRangeID = input.ParameterRangeID AND #TempRanges.iSetID = input.ParameterSetID
    
    PRINT CONVERT(VARCHAR(10) ,@@ROWCOUNT) + ' Updated'
    
    
    INSERT INTO #TempRanges
               ([iRangeID]
               ,[iSetID]
               ,[fRangeFrom]
               ,[fRangeTo]
               ,[bTakeoutEnabled])
    SELECT ParameterRangeID as iRangeID,  
           ParameterSetID as iSetID,  
           ParameterRangeFrom as fRangeFrom,  
           ParameterRangeTo as fRangeTo,  
           ParameterAutoTakeOut as bTakeoutEnabled  
    FROM OPENXML(@idoc, '/Strategy/ParameterRange', 1)
    WITH (ParameterSetID int,ParameterRangeID int,ParameterRangeFrom float,ParameterRangeTo float,ParameterAutoTakeOut bit)  AS input
    LEFT OUTER JOIN #TempRanges ON #TempRanges.iRangeID = input.ParameterRangeID AND #TempRanges.iSetID = input.ParameterSetID
    WHERE #TempRanges.iRangeID IS NULL
    
    
    PRINT CONVERT(VARCHAR(10) ,@@ROWCOUNT) + ' Inserted'
    
    SELECT * FROM #TempRanges
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 545k
  • Answers 545k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • added an answer Well, start off by thinking of which bits of data… May 17, 2026 at 9:17 am
  • added an answer For this task it is a good idea to use… May 17, 2026 at 9:15 am
  • added an answer This is exactly how the Skyhook database (built into many… May 17, 2026 at 9:15 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.