I have a table with the following fields: TransactionId, AssetType, AssetDetails, Order. Primary key is TransactionId, AssetType and Order.
One of the current assetTypes is 'House'. A Transaction can have many houses (Hence order is part of the key). I am trying to update the AssetType of the first house for each transaction to 'Home' (ie where AssetType is currently = 'House' and Order is the min 'Order' where for assetType House).
What i tried to do (that didnt work) was:
UPDATE TOP 1 tbl_ApplicantAssetSET AssetType = 'Home'FROM Asset
WHERE (tbl_Asset.AssetType = N'House') GROUP BY Asset.TransactionId
It seems Update Top 1 is not a valid statement...any suggestions for ways to go about this that will work ???
We walk in the dark places no others will enterWe stand on the bridge and no one may pass
You are aware that by updating 'House' to 'Home' you are in fact also changing the Primary Key for that record? Have you carefully analyzed what the consequences of such an update would be for the integrity of your data?
Thanks for the help guys...yu were right on the mark with the suggested code I didnt know i could do a join like that!!
I hadnt considered that I was changing the primary key, but have had a good look, and in this case, it wont have any adverse affects on the data
Of course I don't know your data as you do, but.... I must stress my point here.
I just want to make sure that you do realize that by doing this update, you will create a new instance (eg row) of the entity (in table) that previously didn't exist, and also by doing so, you will delete an existing instance of the entity, thereby changing the fact that it has ever existed, along with it being the 'parent' of several rows that I suppose have some relation to each other (the order differs only).
The point I'm trying to make is that you want to change the PK for some, but not for all of the registered and currently related asset records. This seems a little strange to me, that's why I'm, asking. I want you to be sure that you're not doing anything you'd later regret..
Here's an alternative that should also work.
Update taSET ta.tbl_ApplicantAsset = 'Home' FROM tbl_ApplicantAsset tawhere ta.tbl_ApplicantAsset = 'House'and ta.[Order] = ( select min([t2.Order]) from tbl_ApplicantAsset t2 where t2.TransactionID = ta.TransactionID and t2.AssetType = N'House')