Solved

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

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

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

752 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