Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SP Pass Values into another query

Status
Not open for further replies.

Westond

Programmer
Jan 19, 2005
43
0
0
US
SELECT AVG(field1 + field2) as theAverage FROM table WHERE ....

UPDATE Table SET Field = theAverage where ....

Why doesn't this work?

 
theAverage is a table. In order to reference another table in an update statement you need to specify that table as part of the from and list the columns that are going to match up. The select statement isn't needed.

Code:
update Table
set Field = Avg(theAverage.Field1+theAverage.Field2)
from theAverage
where ...
group by Avg(theAverage.Field1+theAverage.Field2)

Denny
MCSA (2003) / MCDBA (SQL 2000) / MCTS (SQL 2005) / MCITP Database Administrator (SQL 2005)

--Anything is possible. All it takes is a little research. (Me)
[noevil]
 
You're not using variables properly. Here's how it should be done....

Code:
Declare @TheAverage Real

Select @TheAverage = Avg(Field1 + Field2) From Table Where....

Update Table Set Field = @TheAverage Where....

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
You can also do it with a variable.
Code:
declare @avg numeric(18,2)
select @avg = avg(field1+field2) from theAverage

update Table
set Field = @avg
where ...


Denny
MCSA (2003) / MCDBA (SQL 2000) / MCTS (SQL 2005) / MCITP Database Administrator (SQL 2005)

--Anything is possible. All it takes is a little research. (Me)
[noevil]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top