Update table - Match columns

  • Hmm ok so maybe the title is a little obscure but I'll try to explain what I want to do.

    I have 2 tables:

    Table1 contains several columns and table 2 has some columns .

    Table2 came in the model at a later point in time.

    But Table 2 basically became a subset of Table1.

    So table2 contains columns that are also in table1. Now I want to remove these columns from table one and just reference to the ID in table2 that contains these properties.

    What's the best way to do this in T-SQL?

  • PLease provide us with some sample data..

    Thanks.

  • This is what I was trying

    UPDATE [Audit] as a

    SET a.BatchID = b.Id

    FROM Audit as a inner join Batch as b on a.BatchCode = b.Name

    AND a.BestBeforeDate = b.BestBefore

    AND a.ProductionDate = b.ProductionDate

    //Edit

    BatchCode, BestBeforeDate and ProductionDate are the mutual columns.

    I need to retrieve the ID from the BatchTable and that ID should be set for the column BatchID in Audit

  • Hi

    the_spoofer (6/24/2009)


    Now I want to remove these columns from table one and just reference to the ID in table2 that contains these properties.

    I don't understand the relation between this sentence and an UPDATE statement. If you want to remove those redundant columns and show data of both tables I would suggest to use a VIEW. If you want to update your data your statement looks fine.

    Could you please help open my eyes? 🙂

  • The update returns an error on the first line

    Incorrect syntax near the keyword 'as'.

    But I don't want to show these columns anymore in the Audit table. In the Audit table I just want to see the ID of the batch (which used to be all the batch information in the Audit table)

  • the_spoofer (6/24/2009)


    The update returns an error on the first line

    Incorrect syntax near the keyword 'as'.

    Oups... didn't read your statement correct. This should work:

    UPDATE a

    SET a.BatchID = b.Id

    FROM Audit as a inner join Batch as b on a.BatchCode = b.Name

    AND a.BestBeforeDate = b.BestBefore

    AND a.ProductionDate = b.ProductionDate

    You defined the alias "a" in your FROM clause, so you can use it in your UPDATE clause.

    But I don't want to show these columns anymore in the Audit table. In the Audit table I just want to see the ID of the batch (which used to be all the batch information in the Audit table)

    As I wrote above. So remove the columns. You can create a view which combines all data if needed.

  • Please try this as far as update statement is concerned.

    UPDATE Audit

    SET a.BatchID = b.Id

    FROM Audit a inner join Batch b on a.BatchCode = b.Name

    AND a.BestBeforeDate = b.BestBefore

    AND a.ProductionDate = b.ProductionDate

    As far as displaying the columns are concerned, you can create the view as mention above by Florian

Viewing 7 posts - 1 through 6 (of 6 total)

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