?
Solved

SQL Server XML Parse Question Help

Posted on 2008-11-07
3
Medium Priority
?
265 Views
Last Modified: 2013-11-05
Hello all.  I got some great help with this but I just need one more bit of help.  The following attached code is my syntax for a stored procedure I have.   I also placed in a test string I pass to it for the XML.  This works great if all the data is either updating or inserting.  For example if I pass to this proc the following:

'<Root><Data key1="1" key2="2" key3="1" val="45.00"></Data><Data key1="1" key2="3" key3="1" val="65.00"></Data><Root>'  

the proc will if not exist insert these records into my table.  If I run it again it does the update perfectly.  The problem is when I need to "Insert" new data.  Anything that exists I need to update if not insert.  So if I pass the following after I run the above the first time.


'<Root><Data key1="1" key2="2" key3="1" val="45.00"></Data><Data key1="1" key2="3" key3="1" val="65.00"></Data><Data key1="1" key2="4" key3="1" val="99.00"></Data><Root>'  

I need the first two to update and the last new one to insert.  The proc I have throws an error that it cant insert a duplicate key constraint.  So I believe the issue is the proc will either all Insert or all Update.  I hope this makes sense.  I need the new record to insert and the others to update.  Thanks for any help.  Attached is the proc syntax.

ALTER PROCEDURE [dbo].[spImportDataSummary](@xmlString xml) AS
 
declare @key1 int
declare @key2 int
declare @key3 int
declare @val float
 
;WITH XMLTable AS (
   SELECT D.element.value('@key1', 'int') as key1, D.element.value('@key2', 'int') as key2, 
   D.element.value('@key3', 'int') as key3,
   D.element.value('@val', 'float') as val
 FROM @xmlString.nodes('/Root/Data') AS D(element)
)
select @key1=XMLTable.key1, @key2=XMLTable.key2 , @key3=XMLTable.key3, @val=XMLTable.val from XMLTable
if exists( select * from DataSummary where FundInvestmentPartnerID=@key1 and LineID=@key2 and Section=@key3)
begin
;WITH XMLTable AS (
   SELECT D.element.value('@key1', 'int') as key1, D.element.value('@key2', 'int') as key2,
   D.element.value('@key3', 'int') as key3,
   D.element.value('@val', 'float') as val
 FROM @xmlString.nodes('/Root/Data') AS D(element)
)
UPDATE DataSummary
 SET Amount = XMLTable.val
FROM DataSummary INNER JOIN XMLTable on XMLTable.Key1=DataSummary.FundInvestmentPartnerID
     AND XMLTable.Key2=DataSummary.LineID AND XMLTable.Key3=DataSummary.Section
end
else
begin
;WITH XMLTable AS (
   SELECT D.element.value('@key1', 'int') as key1, D.element.value('@key2', 'int') as key2,
   D.element.value('@key3', 'int') as key3,
   D.element.value('@val', 'float') as val
 FROM @xmlString.nodes('/Root/Data') AS D(element)
)
INSERT INTO DataSummary (FundInvestmentPartnerID, LineID, Section, Amount)
 SELECT XMLTable.key1, XMLTable.key2, XMLTable.key3, XMLTable.val from XMLTable
end
 
---------------------------------------------
ALSO IF YOU NEED THE TABLE TO CREATE HERE IT IS
 
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[DataSummary](
	[FundInvestmentPartnerID] [int] NOT NULL,
	[LineID] [int] NOT NULL,
	[Section] [int] NOT NULL,
	[Amount] [float] NULL,
 CONSTRAINT [PK_DataSummary] PRIMARY KEY CLUSTERED 
(
	[FundInvestmentPartnerID] ASC,
	[LineID] ASC,
	[Section] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

Open in new window

0
Comment
Question by:sbornstein2
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 2000 total points
ID: 22910042
Try it this way:

ALTER PROCEDURE [dbo].[spImportDataSummary](@xmlString xml) AS
 
declare @key1 int
declare @key2 int
declare @key3 int
declare @val float
 
;WITH XMLTable AS (
   SELECT D.element.value('@key1', 'int') as key1, D.element.value('@key2', 'int') as key2,
   D.element.value('@key3', 'int') as key3,
   D.element.value('@val', 'float') as val
 FROM @xmlString.nodes('/Root/Data') AS D(element)
)
select @key1=XMLTable.key1, @key2=XMLTable.key2 , @key3=XMLTable.key3, @val=XMLTable.val from XMLTable

-- if exists( select * from DataSummary where FundInvestmentPartnerID=@key1 and LineID=@key2 and Section=@key3)
-- begin
;WITH XMLTable AS (
      SELECT      D.element.value('@key1', 'int') as key1,
                  D.element.value('@key2', 'int') as key2,
                  D.element.value('@key3', 'int') as key3,
                  D.element.value('@val', 'float') as val
      FROM      @xmlString.nodes('/Root/Data') AS D(element)
      Where      EXISTS(
                  select      1
                  from      DataSummary
                  where      FundInvestmentPartnerID = D.element.value('@key1', 'int')
                              and LineID = D.element.value('@key2', 'int')
                              and Section = D.element.value('@key3', 'int'))
      )
      UPDATE      DataSummary
      SET            Amount = XMLTable.val
      FROM      DataSummary
                  INNER JOIN XMLTable on XMLTable.Key1=DataSummary.FundInvestmentPartnerID
                                                AND XMLTable.Key2=DataSummary.LineID AND XMLTable.Key3=DataSummary.Section
--end
--else
--begin
;WITH XMLTable AS (
      SELECT      D.element.value('@key1', 'int') as key1,
                  D.element.value('@key2', 'int') as key2,
                  D.element.value('@key3', 'int') as key3,
                  D.element.value('@val', 'float') as val
      FROM      @xmlString.nodes('/Root/Data') AS D(element)
      Where      NOT EXISTS(
                  select      1
                  from      DataSummary
                  where      FundInvestmentPartnerID = D.element.value('@key1', 'int')
                              and LineID = D.element.value('@key2', 'int')
                              and Section = D.element.value('@key3', 'int'))

)
      INSERT      DataSummary (FundInvestmentPartnerID, LineID, Section, Amount)
      SELECT      XMLTable.key1, XMLTable.key2, XMLTable.key3, XMLTable.val from XMLTable
--end
0
 

Author Comment

by:sbornstein2
ID: 22911161
I will try this tomorrow AC as the dev sql box is down this evening.  I can't wait to test it out, it would be a huge help if this works.  I will get back to you soon on this.
0
 

Author Closing Comment

by:sbornstein2
ID: 31514328
sorry for the delay ac, this was completely perfect.  Thanks again
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question