Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Update/Replace Statement

Author  Topic 

fiendz138
Starting Member

5 Posts

Posted - 2013-12-27 : 11:56:49
I have been tasked with cleaning up our call tracking database.

I am looking to run this update:

UPDATE CallLog set extcaltyp=replace(extcaltyp, 'SW - MS ACCESS', 'MS Office')

UPDATE CallLog set extcaltyp=replace(extcaltyp, 'SW - MS EXCEL', 'MS Office')

I get an error that the record already exists when the 2nd statement runs. Is there a way I can get around this so that I can change these to the same value?

Thanks in advance for any help you can provide.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2013-12-27 : 12:36:58
Are you getting the error through Management Studio? Are you using a New Query window? If not, you should be.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

fiendz138
Starting Member

5 Posts

Posted - 2013-12-27 : 13:26:20
Yes. I am using the new query window.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2013-12-27 : 13:26:58
Show us the exact error.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

fiendz138
Starting Member

5 Posts

Posted - 2013-12-27 : 13:38:46
I'm going to have to run the update again to get you the exact error.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-12-28 : 03:35:03
Is there a UNIQUE index/constraint on extcaltyp?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2013-12-28 : 09:12:46
[code]
UPDATE CL1
set extcaltyp=replace(extcaltyp, 'SW - MS EXCEL', 'MS Office')
FROM CallLog AS CL1
WHERE NOT EXISTS
(
SELECT *
FROM CallLog AS CL2
WHERE CL2.MyPKey <> CL1.MyPKey
... any additional PKey fields here ...
AND CL2.OtherKey = CL1.OtherKey
... any additional additional key fields here ...
AND CL2.extcaltyp = replace(CL1.extcaltyp, 'SW - MS EXCEL', 'MS Office')
)
[/code]
then
[code]
SELECT Col1, Col2, ...
FROM CallLog
WHERE extcaltyp LIKE '%SW - MS EXCEL%'
[/code]
to see any rows that remain that did NOT get converted. Those records clash.
Go to Top of Page

fiendz138
Starting Member

5 Posts

Posted - 2013-12-30 : 23:28:51
All,
This is the error I am getting:

Msg 2627, Level 14, State 1, Line 147
Violation of PRIMARY KEY constraint 'PK_extcalltp_Extcaltyp'. Cannot insert duplicate key in object 'dbo.extcalltp'. The duplicate key value is (MS Office).

Kristen,
I am kind of new to SQL. Would your update statement allow me to change the records to MS Office even though it already exists?
The statement has been terminated
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2013-12-31 : 04:07:39
The error message says it all. You are not allowed to have duplicate values in the column.



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-12-31 : 10:25:58
quote:
Originally posted by fiendz138

All,
This is the error I am getting:

Msg 2627, Level 14, State 1, Line 147
Violation of PRIMARY KEY constraint 'PK_extcalltp_Extcaltyp'. Cannot insert duplicate key in object 'dbo.extcalltp'. The duplicate key value is (MS Office).

Kristen,
I am kind of new to SQL. Would your update statement allow me to change the records to MS Office even though it already exists?
The statement has been terminated


Either redefine your primary key to include more columns which will provide a unique combination or drop the primary key itself before you do the update

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

fiendz138
Starting Member

5 Posts

Posted - 2013-12-31 : 13:15:43
How would I drop the primary key? And would I need to do that each time I want to update with a duplicate value?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-12-31 : 13:32:30
quote:
Originally posted by fiendz138

How would I drop the primary key? And would I need to do that each time I want to update with a duplicate value?


use ALTER TABLE...DROP CONSTRAINT to drop the PK. Then recreate it with column combination as per your business relevance
The change is one time. Once you redefine primary key based on group of columns you'll be able to do updation thereafter

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2014-01-02 : 07:25:45
quote:
Originally posted by fiendz138

All,
Would your update statement allow me to change the records to MS Office even though it already exists?


Yes.

It will convert anything to MS OFFICE if that does not already exist, and then the second query will show you anything that it could NOT convert to MS OFFIce because it already exists.

I strongly advise you NOT to drop the primary key / unique constraint. It is presumably there to stop you have two entries of MS OFFICE for a single entity.

If you are changing MS ACCESS and MS EXCEL to MS OFFICE then it seems highly likely to be that someone will have both ... and thus your change to MS OFFICE will attempt to give them two records with the same name, whereas I expect what you want is a single such record.

So my plan would be:

Change MS ACCESS to MS OFFICE where there is not existing record of MS OFFICE for that entity.

Delete MS ACCESS records that are then left behind (probably best to check that you only do that where there EXISTS an MS OFFICE record - otherwise someone else might have added a new MS ACCESS record in the meantime)

Then do the same converting MS EXCEL to MS OFFICE and deleting any MS EXCEL that are left behind (where MS OFFICE already exists)
Go to Top of Page
   

- Advertisement -