Update existing rows in a table from a view SQL Server 2005
Posted on 2008-06-23
I am trying to:
Update a table from a View FOR EXISTING RECORDS IN THE TABLE that have changed in the view from the last update (or Insert)
note. The table and View have identical columns. There is a primary key in the table that is an Identity column.
I am updating the table like this:
update dbo.Tbl1 set
Rec5 = dbo.View1.Rec5
where dbo.Tbl1.REC1 = dbo.View1.REC1 and dbo.Tbl1.Rec2 = dbo.View1.Rec2 and dbo.Tbl1.Rec3 = dbo.View1.Rec3
This seems to work but I am not convinced that the correct records will be updated.
Rec1 will be similar in many records
Rec2 will be similar in many records
Rec3 will be unique for for Rec2. In other words, There may be several Rec2s but there will only be One Rec3 of each value for each Rec2.
Is there a better way to do this to maintain the integrety of the table and update the correct records??