简体   繁体   中英

update fields based on table to table?

I am in brain stroming situation. I thought, lets explain to geeks, So they can help me to resolve this problem.

I have table called Sharepoint_Users in Oracle. (see below format and data). I have access oracle db from linked server.Don't worry about it. Just consider it is sql table.

SharePoint_Users

ID         Status
1           Active
2           InActive
3           Active
4           InActive

another two tables called aspnet_user and aspnet_UsersInRoles in SQL server.

aspnet_users
UserID                                       UserName      

A7DFDDAE-4DB8-476D-9C29-677763406F71          1
D9910E14-9206-4460-88CA-4C39DE620192          2
F188B1DF-03A6-4332-BA89-3B3C6682E9BA          3
728E77E7-693A-4015-92CA-02F0A403C29A          4

asnet_usersInRoles

UserID                                      RoleID

A7DFDDAE-4DB8-476D-9C29-677763406F71        1E36A840-2EBB-44EC-8861-0E3D262AC676 ----> InActive
D9910E14-9206-4460-88CA-4C39DE620192        0B54F223-E0D4-4CFC-84C3-7C98C1BFC6DA --->Active

Now here is the challenge.

* When users status changes in SharePoint_Users table from 'Active' to 'InActive'
 OR 'InActive' to 'Active'. We need to update same users RoleID in 
 asnet_usersInRoles table.


* And also I need insert new records those not exists in asnet_usersInRoles 
  table but exists in aspnet_users table.
  If user not found in aspnet_users should not insert them into 
  asnet_usersInRoles.(always users will be the same SharePoint_Users 
  and aspnet_users)

Please help me out write a sp to get it done. I will run job for this sp every 1 hr to update.

To be able to update when SharePoint_Users changes I guess you need a trigger but you also write that you will run the SP every 1 hr so...

Anyway here is an insert statement for your second part and an update statement for the first part. Since you do not know what rows have been changed the update updates all rows.

insert into asnet_usersInRoles(UserID, RoleID)
select
  UserID,
  case s.[Status] when 'Active'
    then '0B54F223-E0D4-4CFC-84C3-7C98C1BFC6DA'
    else '1E36A840-2EBB-44EC-8861-0E3D262AC676'
  end
from aspnet_users as au
  inner join (select ID, min([Status]) as [Status]
              from SharePoint_Users
              group by ID) as s
    on au.UserName = s.ID
where not exists (select *
                  from asnet_usersInRoles as uir
                  where au.UserID = uir.UserID)

update asnet_usersInRoles
set RoleID = case s.[Status] when 'Active'
               then '0B54F223-E0D4-4CFC-84C3-7C98C1BFC6DA'
               else '1E36A840-2EBB-44EC-8861-0E3D262AC676'
             end
from asnet_usersInRoles as uir
  inner join aspnet_users as u
    on uir.UserID = u.UserID
  inner join (select ID, min([Status]) as [Status]
              from SharePoint_Users
              group by ID) as s
    on u.UserName = s.ID  

When you want something to modify data in a table when another table is updated, use a trigger:

CREATE TRIGGER SharePoint_Users_Trg_Upd ON SharePoint_Users
AFTER UPDATE
AS
/* update statements here */
GO

This way you don't need the sp to listen every hour.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM