Compare data in 2 different tables

  • Hello, can anyone help?

    I have 2 different structure tables (there are more columns than this, but these are the important ones that I need to extract data from):

    table 1: UserRoles

    cols: UserRoleID (PK), UserID, RoleID, ExpiryDate

    table2: Receipts

    cols: ReceiptID (PK), UserID, DateEnd

    I need to select all of the users from table UserRoles that have the RoleID of 5

    I need to take the ExpiryDate and UserID of each of these records

    and then run a query on table Receipts to search for a matching UserID and change the value in DateEnd to the ExpiryDate from table UserRoles

    What is the best way to put this query together?

    Thanks for any advice you can give

  • Something like this?

    UPDATE Receipts

    SET DateEnd = u.ExpiryDate

    FROM UserRoles u

    INNER JOIN Receipts r

    ON u.UserId = r.UserId

    WHERE u.RoleId = 5

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Many thanks, that worked great

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply