Showing posts with label key. Show all posts
Showing posts with label key. Show all posts

Sunday, March 11, 2012

"ON DUPLICATE KEY UPDATE"?

Hi,

I am migrating from MySQL to SQL Express. In MySQL, I import data from Oracle using the "INSERT INTO .... ON DUPLICATE KEY UPDATE". With the "ON DUPLICATE KEY UPDATE", when there is a duplicate key in MySQL exists, the respective records will be updated (so to avoid inserting the data twice). I cannot remove the duplicated rows in MySQL as it contains extra fields with user-input value.

Is there any way I can do the same in SQL Express? Is it a must that I need to create a temp table to hold the downloaded data and then update the duplicated data in the original before an INSERT?

Thanks

Raymond

There is no equivalent functionality. You can however do the following:

1. You can create an unique index on the key column(s) with the IGNORE_DUP_KEY option set to on. This will result in the duplicate rows being ignored during the INSERT operation for instance. You will have to however update the rows by inserting into another table and then performing update

2. Use a worktable approach where you insert all the rows into the worktable and then use insert/update as into the main table.

|||

Thanks,

I think I will go for the second option, as there is only a few new records (inserts) every time, and most the other records involve changes (updates).

Raymond

Thursday, February 9, 2012

#8172 [error] need help

-- The following code throw exception~

-- Need HELP!!

 
use AdventureWorksgo--drop table T--gocreate table T(ProductModelIDint primary key, Instructionsxml (Production.ManuInstructionsSchemaCollection))goinsert Tselect ProductModelID, Instructionsfrom Production.ProductModelwhere ProductModelID=7goselect Instructionsfrom T--1) insert <Location 1000/>. Note: <Root> must be singleton in the queryupdate Tset Instructions.modify(' declare namespace MI="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions"; insert <MI:Location LocationID="1000" LaborHours="1000" > These are manu steps at location 1000. <MI:step>New step1 instructions</MI:step> Instructions for step 2 are here <MI:step>New step 2 instructions</MI:step> </MI:Location> as first into (/MI:root)[1]')goselect Instructionsfrom Tdeclare @.aaaaaintset @.aaaaa=1000-- delete an attributeupdate Tset Instructions.modify(' declare namespace MI="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions"; delete(/MI:root/MI:Location[@.LocationID='+@.aaaaa+']/@.LaborHours) ')--,--Instructions.modify('-- declare namespace MI="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";-- delete(/MI:root/MI:Location[@.LocationID='+@.aaaaa+']/text()) --'),--Instructions.modify('-- declare namespace MI="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";-- delete(/MI:root/MI:Location[@.LocationID='+@.aaaaa+']/MI:step[2]) --')goselect Instructionsfrom T-- cleanupdrop table Tgo

That means you can't use dynamic built string as the argument of modify method. You needsql:variable("@.var") function, for example:

update T
set Instructions.modify('
declare namespace MI="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
delete(/MI:root/MI:Location[@.LocationID=sql:variable("@.aaaaa")]/@.LaborHours)
')

To learn more about XQuery in SQL2005, you can refer to:

http://msdn.microsoft.com/library/en-us/dnsql90/html/sql2k5_xqueryintro.asp?frame=true

|||

Thx very very very very muchBig Smile