Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Update Top 1 Expand / Collapse
Author
Message
Posted Monday, January 3, 2005 10:57 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, July 29, 2014 6:18 PM
Points: 200, Visits: 192

Hi All,

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_ApplicantAsset
SET 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 ???

Thanks

Ben

Post #153385
Posted Monday, January 3, 2005 11:28 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 2:25 PM
Points: 39,910, Visits: 36,250
I'm not sure I completely understood what you're trying to do, but the below code should be fairly close to what you're trying.

If I've missed the mark by a few miles, please post the table definitions.

Update tbl_ApplicantAsset SET tbl_ApplicantAsset = 'Home' FROM tbl_ApplicantAsset INNER JOIN
(select TransactionID, min([Order]) as minOrder from tbl_ApplicantAsset where AssetType = N'House') Filtered
On tbl_ApplicantAsset.TransactionID = Filtered.TransactionID AND tbl_ApplicantAsset.[Order] = Filtered.minOrder



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #153389
Posted Tuesday, January 4, 2005 2:20 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Thursday, January 9, 2014 3:33 AM
Points: 1,559, Visits: 672

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?

/Kenneth




Post #153414
Posted Tuesday, January 4, 2005 4:45 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, July 29, 2014 6:18 PM
Points: 200, Visits: 192

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

Thanks again!

Ben

Post #153628
Posted Wednesday, January 5, 2005 3:47 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Thursday, January 9, 2014 3:33 AM
Points: 1,559, Visits: 672

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..

/Kenneth

 




Post #153697
Posted Wednesday, January 5, 2005 2:57 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, July 29, 2014 6:18 PM
Points: 200, Visits: 192
Thanks again Kenneth...the asset table does not have any child tables, and the reason for the update is to introduce a new asset type 'Home' for all home owners (as we now wish to distinguish investment properties from homes). AS long as the new home asset is still realted to the same TransactionId, all will be well!
Post #153868
Posted Wednesday, March 2, 2005 3:09 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, March 18, 2009 4:41 PM
Points: 1, Visits: 2
BTW, "UPDATE TOP" will work in SQL Server 2005.
Post #165215
Posted Wednesday, March 2, 2005 3:18 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, July 29, 2014 6:18 PM
Points: 200, Visits: 192
Cool! will be great to see what 2005 can do! Sounds like there is gonna be lots of new and useful commands...which will be good, but even more to learn!
Post #165218
Posted Wednesday, March 2, 2005 4:27 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, June 14, 2005 11:44 PM
Points: 4, Visits: 1

Here's an alternative that should also work.

 

Update ta
SET ta.tbl_ApplicantAsset = 'Home'
FROM tbl_ApplicantAsset ta
where 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')




Post #165243
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse