centdevs
asked on
Update each row in a select statement
I need to create a job to clean up some data every night. I need to do a select statement (that joins multiple tables) and then loop through each record and do an update to one of the related tables. How can I do this?
why not update all the rows at once, based on the select statement?
ASKER
That's fine too, I didn't know I could.
like this
update t3
set field = t4.field
from
table1 t1
join table2 t2 on t1.field = t2.field
join tabl3 t3 on t2.field2 = t3.field2
join table4 t4 on t3.field3 = t4.field3
update t3
set field = t4.field
from
table1 t1
join table2 t2 on t1.field = t2.field
join tabl3 t3 on t2.field2 = t3.field2
join table4 t4 on t3.field3 = t4.field3
ASKER
Having a bit of trouble getting my where clause in there. Here is the select clause that shows the rows that would need to be updated:
SELECT p.PROF_UserID, p.PROF_acctno, ur.RoleID, r.RoleName, m.MemberType, m.MemberStatus
FROM dev..profile p (NOLOCK)
INNER JOIN mastermember..vwMember m
ON m.Member_acctno = p.PROF_acctno
INNER JOIN dev..aspnet_UsersInRoles ur
ON ur.UserID = p.PROF_UserID
INNER JOIN dev..aspnet_Roles r
ON r.RoleID = ur.RoleID
WHERE m.MemberStatus IN ('103', '109')
AND ur.RoleID IN ('21a3ea4b-3bc0-4308-bb3f- 03e70e3b62 f5',
'1141de1a-ddf8-40a1-9fc5-d 0eb0ee2c58 0',
'936f46a7-a0b7-4162-8353-e d6c2391b5b 2',
'204035cf-aa6a-47f3-af47-b 834b73aed0 7')
SELECT p.PROF_UserID, p.PROF_acctno, ur.RoleID, r.RoleName, m.MemberType, m.MemberStatus
FROM dev..profile p (NOLOCK)
INNER JOIN mastermember..vwMember m
ON m.Member_acctno = p.PROF_acctno
INNER JOIN dev..aspnet_UsersInRoles ur
ON ur.UserID = p.PROF_UserID
INNER JOIN dev..aspnet_Roles r
ON r.RoleID = ur.RoleID
WHERE m.MemberStatus IN ('103', '109')
AND ur.RoleID IN ('21a3ea4b-3bc0-4308-bb3f-
'1141de1a-ddf8-40a1-9fc5-d
'936f46a7-a0b7-4162-8353-e
'204035cf-aa6a-47f3-af47-b
what do you want to update?
ASKER
I need to update dev..aspnet_UsersInRoles.R oleID
to what value(or field)?
ASKER
ur.RoleID = '0029df3c-6163-40ac-b254-7 db6bad4978 0'
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Awesome, thanks!!