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 2000 Forums
 Transact-SQL (2000)
 How to use COUNT in an UPDATE

Author  Topic 

AlanS
Starting Member

28 Posts

Posted - 2005-11-23 : 11:46:11
I have a script that declares and populates two temp table variables, @T1 and @T2, both of which include the field "Common". Based on the value of "Common", each record in @T1 may have 0, 1 or more matching records in @T2. @T1 also includes the field "Counter", and @T2 also includes the field "TypeCode". Later in the script, I want to update all the instances of @T1.Counter with the number of matching records in @T2 which also have TypeCode = 1. I've tried this a dozen different ways, but always get some sort of parsing error, many of which say that I can't use an aggregate function (i.e., COUNT) in an UPDATE statement. I would think there's a simple way to do this, without resorting to a cursor, but can't figure out what it is. Books Online is no help. Anyone have an idea? Thanks!

X002548
Not Just a Number

15586 Posts

Posted - 2005-11-23 : 12:52:27
Please read the hint link in my sig and post some details for us...and BOL is a tremendous help...



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2005-11-23 : 23:46:04
I dont know whether you want this ... but just try out whether this satify ur query..

Update @T1 Set Counter = @t2.Counter From @T2
Where @T1.Common = @T2.Common And @T2.TypeCode = 1

??

or follow the Brett weblogs and post accordingly.. :-)

Sucess Comes to those who Believe in Beauty of their Dream..
Go to Top of Page
   

- Advertisement -