Solved

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

Posted on 2008-06-23
4
1,778 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 76

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MS SQL Bulk load data error 5 33
separate column 24 20
Trouble connecting to SqlServer database 4 32
Slow SQL query 12 21
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

705 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

13 Experts available now in Live!

Get 1:1 Help Now