I have a table where I want to copy some data back to the same table with two columns getting changed:
e.g:
TableMusic
titleid musicid tap_id value
1234 1 3 this is music
1324 1 3 this is music test
I want the titleid and the value to be written back to the same table with musicid=3
Here the indexed columns are titleid, musicid
I have done the following script but got an error: attempt to insert duplicate key row in object ‘tablemusic’ with unique index ‘pk_tablemusic’
insert into tablemusic (titleid, musicid, value )
select titleid, 3, value
from tablemusic
where musicid=1
What am I doing wrong here? Can someone come up with a clean script to get the job done.
following is an extract from my DB for one titleID:
titleid musicid tap_id value
462847 1 3 this is my music value
462847 2 1
462847 3 1
462847 4 1
this is the same for all titleid’s. Now what I want to do is copy the value from musicid=1 to musicid=3 and also sometime change the tap_id to 3. I hope this is clear enough of what i want to achieve
Final Result want to achieve:
titleid musicid tap_id value
462847 1 3
462847 2 1
462847 3 3 this is my music value
462847 4 1
The complexity is that some records exist so trying a simple insert duplicates the key and you get the error. What you have to do here is to break up the query into two prts