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 2008 Forums
 Transact-SQL (2008)
 For loop update

Author  Topic 

daniel50096230
Yak Posting Veteran

99 Posts

Posted - 2011-12-12 : 21:30:12
I need a loop to update the data. For example,

Select Distinct Customer_ID FROM A

Then For each customer_ID that selected, then update

Update A Set Status = 'A' Where Customer_ID = "Customer selected from the select statement"

How can i write it?

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-12-12 : 21:41:47
[code]
update A
set Status = 'A'
from A
inner join
(
select Customer_ID
from A
group by Customer_ID having count(*) = 1
) D on A.Customer_ID = D.Customer_ID
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-12-12 : 21:44:11
another way . .

update A
set Status = 'A'
from
(
select Customer_ID, Status, cnt = count(*) over (partition by Customer_ID)
from A
) A
where cnt = 1



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -