Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-06-23
4
Medium Priority
?
1,786 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 80

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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

971 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