Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-06-23
4
Medium Priority
?
1,784 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 79

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

721 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