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 2005 Forums
 Transact-SQL (2005)
 How to use SELECT statement in UPDATE

Author  Topic 

vision99
Starting Member

14 Posts

Posted - 2009-06-26 : 12:12:05
Hi,

I have one temp table, am inserting values from another table

create table #temp1
(
rowtitle varchar(50),
[status] varchar(50),
[countvalue] int
)

insert into #temp1
(
rowtitle,
[status]
)
select
rowtitle,
[status]
from
ctladastatuses

I want to update [countvalue] field like

update #temp1
set [countvalue] = (select distinct rowtitle from ctladastatuses)

showing error msg.. while trying to update, plzz give some idea...


-Vision

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2009-06-26 : 12:18:13
What are you trying to accomplish with this query? What error are you getting. One reason your update is failing is that (select distinct rowtitle from ctladastatuses) will return more than one value.

Jim
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-06-26 : 12:19:25
update t1
set [countvalue]=dt.cnt
from #temp1 t1
join (select rowtitle,count(*) as cnt from ctladastatuses group by rowtitle)dt
on dt.rowtitle = t1.rowtitle


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

vision99
Starting Member

14 Posts

Posted - 2009-06-26 : 12:46:40
quote:
Originally posted by webfred

update t1
set [countvalue]=dt.cnt
from #temp1 t1
join (select rowtitle,count(*) as cnt from ctladastatuses group by rowtitle)dt
on dt.rowtitle = t1.rowtitle


No, you're never too old to Yak'n'Roll if you're too young to die.



Thanks for reply, but its not giving me the expected results i.e.,
select count(distinct rowtitle) will retrive the value '7', so '7' should be updated for all records for [countvalue] field..

-Vision
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-06-26 : 12:52:15
Then use your "count(distinct rowtitle)" in your formerly posted statement.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-26 : 13:58:54
[code]update t
set [countvalue] = (select count(distinct rowtitle) from ctladastatuses where rowtitle = t.rowtitle)
from #temp1 t
[/code]
Go to Top of Page
   

- Advertisement -