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
 General SQL Server Forums
 New to SQL Server Programming
 Looping through table

Author  Topic 

OldMySQLUser
Constraint Violating Yak Guru

301 Posts

Posted - 2008-12-05 : 07:53:01
I have a SQL Server 2005 table

Payment_Scores

Reference DebtorNo Percent Score
--------- -------- ------- -----
1234567 2 89 5

which I need to loop through and then populate the fields 'Percent' and 'Score' in a table

Debtor

DebtID DebtorNo Percent Score
------ -------- ------- -----
10134 2 (null) (null)

by matching the 'DebtID' and 'DebtorNo' after matching the 'Code' field in table

Debt

Code DebtID
---- ------
3234567 44567
1234567 10134
8123456 22657

so I end up with table Debtor being

DebtID DebtorNo Percent Score
------ -------- ------- -----
10134 2 89 5

How can I acheive this please?

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-12-05 : 07:58:04
With ordinary JOINs.
UPDATE		x
SET x.percent = ps.percent,
x.score = ps.score
FROM Payment_Scores AS ps
INNER JOIN Debt AS d ON d.Code = ps.Reference
INNER JOIN Debtor AS x ON x.DebtorNo = d.DebtID



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

OldMySQLUser
Constraint Violating Yak Guru

301 Posts

Posted - 2008-12-05 : 08:39:25
Much appreciated.
Go to Top of Page
   

- Advertisement -