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 3879432
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:43:33+00:00 2026-05-19T22:43:33+00:00

i am new in SQL server. my requirement is I have to take thousands

  • 0

i am new in SQL server. my requirement is I have to take thousands of records from c# as a XML file and from that file I am taking data in temp table. And from table I have to check record one by one if it is present then Update else insert. So I have written a stored procedure, but it is giving me the following error

Msg 102, Level 15, State 1, Procedure
InsertIntoMyTable, Line 13
Incorrect
syntax near ‘cast’.
Msg 102, Level 15,
State 1, Procedure InsertIntoMyTable,
Line 18
Incorrect syntax near ‘LOOP’.
Msg 156, Level 15, State 1, Procedure
InsertIntoMyTable, Line 25
Incorrect syntax near the keyword ‘END’.

Stored procedure:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE InsertIntoMyTable
    @mytable xml 
 AS
BEGIN
    SELECT 
    cast(colx.query('data(PointsliceId) ') as int) as PointSliceId,
    cast(colx.query('data(Pt_timestamp) ') as datetime)     as Point_timestamp
    cast(colx.query('data(FloatValue) ') as float)     as Float_Value
INTo #TMP FROM @mytable.nodes('DocumentElement/mytable') AS Tabx(Colx)


For IDX in (select * from TMP)
LOOP
if((select count(*) from PointValue_Float where PointSliceId=IDX.PointSliceId and Pt_timeStamp=IDX.Pt_timeStamp)>0 )            
          update PointValue_Float set FloatValue=t.FloatValue from #TMP t  where t.PointSliceId=PointValue_Float.PointSliceId and t.Pt_timeStamp=PointValue_Float.Pt_timeStamp 
else
        insert into PointValue_Float(PointSliceId,Pt_timeStamp,FloatValue) SELECT PointSliceId,Pt_timeStamp,FloatValue FROM #TMP
END LOOP

END
GO
  • My Table Name Is pointvalue_float where i have to check data is present or not if present then update else insert
  • 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-19T22:43:34+00:00Added an answer on May 19, 2026 at 10:43 pm

    Creating a manual loop in a SQL Server procedure is always a bad idea – SQL Server operates in sets of data – and your statement should also be set-oriented.

    In your case, what I would do is this:

    • shred the XML into a temporary table (as you already do)
    • then update the existing values based on a join condition
    • remove those rows updated from the temporary table
    • the remaining rows need to be inserted

    So your code would be something like:

    CREATE PROCEDURE InsertIntoMyTable @mytable xml 
    AS BEGIN
       SELECT 
          colx.value('(PointsliceId)[1]', 'INT') AS PointSliceId,
          colx.value('(Pt_timestamp)[1]', 'DATETIME') AS Point_timestamp
          colx.value('(FloatValue)[1]', 'FLAOT') AS Float_Value
       INTO #TMP 
       FROM @mytable.nodes('DocumentElement/mytable') AS Tabx(Colx)
    
    -- udpate the existing rows
    UPDATE dbo.PointValue_Float
    SET FloatValue = t.FloatValue 
    FROM #TMP t  
    WHERE t.PointSliceId = PointValue_Float.PointSliceId 
      AND t.Pt_timeStamp = PointValue_Float.Pt_timeStamp 
    
    -- remove those from the #TMP table
    DELETE FROM #TMP
    WHERE EXISTS
      (SELECT * FROM dbo.PointValue_Float 
       WHERE PointSliceId = #TMP.PointSliceId AND Pt_timeStamp = #TMP.Pt_timeStamp)
    
    -- INSERT the remaining rows    
    INSERT INTO 
        dbo.PointValue_Float(PointSliceId, Pt_timeStamp, FloatValue) 
      SELECT 
         PointSliceId, Pt_timeStamp, FloatValue 
      FROM #TMP
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Change Data Capture is a new feature in SQL Server 2008. From MSDN: Change
I am new to SQL Server, and I have been tasked with setting permissions
I'm brand new to SQL Server 2008, and have some newbie questions about the
I am pretty much new to using SQL Server 2000. I have been using
We have an application which uses WAMP server. Now, there is a new requirement
I have a SQL Server 2005 data-mart star schema with the usual fact and
A site I developed has a new requirement to get weather data from the
I have a requirement where I need to get data from the previous row
i am preparing myself for importing data from excel sheet to sql server 2005.I
I have a require ment to read data from a table(SQL 2005) and send

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.