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
 General SQL Server Forums
 New to SQL Server Programming
 Update on primary key column

Author  Topic 

ra_pa_ta
Starting Member

13 Posts

Posted - 2011-09-23 : 12:21:26
The table I need to update has a primary key on two columns codeid and priceid the codeid 22 will have a new price id and I have to update the table .how do I do it this is what I have so far
Update A Set A.Codeid = T.tintFeatureCode,
A.PriceID = T.PriceID,
A.strFlag = T.strFlag,
A.LastUpdate = T.LastUpdate
From dbo.tblrefFeaturePrice As A
Inner Join dbo.Sourcefile As T On T.Codeid = A.Codeid
Where A.LastUpdate < T.LastUpdate

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-23 : 12:29:00
if the table has primary key on two columns codeid and priceid and you've new record coming up for codeid 22 with new priceid then it should be an insert not an update as its a new unique combination.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

paultech
Yak Posting Veteran

79 Posts

Posted - 2011-09-27 : 10:07:27
I couldn't understand the relation between the code you post and what you want , but my suggestion to you , instead of updateing the record . you can delete it and insert a new one again with the new values of the primary key , because the PK here is not autoincreament

Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2011-09-27 : 12:45:53
"you can delete it and insert a new one again with the new values of the primary key"

If that's what the O/P wants to do then an UPDATE will be better.
Go to Top of Page

jassi.singh
Posting Yak Master

122 Posts

Posted - 2011-09-27 : 13:51:26
Hi,

Update query shoud work and update the priceid. you should use codeid and old priceid in where clause and set priceid with new priceid and please first check whether combination of codeid and new priceid does not exist in the table.

Please mark answer as accepted if it helped you.

Thanks,
Jassi Singh
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-27 : 23:05:27
quote:
Originally posted by jassi.singh

Hi,

Update query shoud work and update the priceid. you should use codeid and old priceid in where clause and set priceid with new priceid and please first check whether combination of codeid and new priceid does not exist in the table.

Please mark answer as accepted if it helped you.

Thanks,
Jassi Singh


no way to do it here other than manually quoting replying and telling that this is fine or something

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

vmvadivel
Yak Posting Veteran

69 Posts

Posted - 2011-09-27 : 23:07:52
>>no way to do it here other than manually quoting replying and telling that this is fine or something

Thanks for that clarification. I was searching for the option all through the site for past few mins :)

Best Regards
Vadivel

http://vadivel.blogspot.com
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-27 : 23:09:28
quote:
Originally posted by vmvadivel

>>no way to do it here other than manually quoting replying and telling that this is fine or something

Thanks for that clarification. I was searching for the option all through the site for past few mins :)

Best Regards
Vadivel

http://vadivel.blogspot.com


welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -