views:

161

answers:

4

How can i update bulk data in sql server 2005 by sending the data in XML form ? i am able to insert bulk data into the table, but i am not getting idea to update the data.

+1  A: 
Insert into #TempTable
//Basically do bulk insert into temp table then...


Update MyTable
  Set Field1 = tt.Field1,
      Field2 = tt.Field2,
      ...
FROM #TempTable tt
where primaryKey = tt.PrimaryKey

Note this is kind suedo code. so replace fieldx with your field names and replace primaryKey with the primarykey field name or unique identifier field name for the table.

John Hartsock
thanks..... i will try this and get back to u.....
Nimesh
I tried this and is working fine....
Nimesh
A: 

update requires you to determine some external logic.

for instance, if the primary key of the incoming record already exists, update the other columns, otherwise insert this record.

i might suggest you write some xslt to create update statements out of the incoming xml stream, then run that sql script.

Randy
yeah... i need to do something like this.. if the primary key exists i need to update it, else i need to insert the data. All i have is an xml with all the data's. if PK doesn't exists, i am adding zero for that field.
Nimesh
A: 

Check out the OPENXML function. You map also want to load the XML data into a variable table so you can easily join or compose the rest of the query as you would between normal database tables.

Matthew Whited
A: 

SQL Server 2005 and up has native support for XML data types, and also supports the XQuery language for shredding XML into relational data columns.

Check out the Introduction to XQuery in SQL Server 2005 to get a feel for how this works.

marc_s