Solved

Update existing rows in a table from a view SQL Server 2005

Posted on 2008-06-23
4
1,780 Views
Last Modified: 2010-04-21
Hello Experts,

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
from dbo.View1
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.
note...
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??

Thanks!
0
Comment
Question by:Saxitalis
  • 2
4 Comments
 
LVL 77

Expert Comment

by:arnold
ID: 21851644
Several things do not seem to make sense.
Could you post an example of the data?

I would think a view was setup to expedite the querying/extraction of data.
A view by its nature aggregates data from several tables and I believe, will reflect a change in any underlying tables' field.

Updating a table based on a content of a view is somewhat puzzling.

What is the relationship between the tables of the database and the view?  Are you updating a table that is not an underlying source of data for the view?

Your database lost data integrity from the time the data insertion did not account for the database design.

How certain are you that the view is reflecting the correct data?  The update you are running might overwrite good data with bad.



0
 

Author Comment

by:Saxitalis
ID: 21851813
Thanks for the reply,

I will send an example...
0
 
LVL 24

Accepted Solution

by:
mankowitz earned 500 total points
ID: 21851864
Also, assuming that the view has different data, why not just update based on the primary key? you shouldn't need to look at the other records. Finally, Don't worry about selecting the CHANGED records. If you update all the records, the changed records will pass through and the unchanged records will be ignored.
0
 

Author Closing Comment

by:Saxitalis
ID: 31470009
Thank You!
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now