?
Solved

Update multiple tables with view SQL Server 2008

Posted on 2009-06-30
5
Medium Priority
?
1,624 Views
Last Modified: 2012-05-07
I have a view which is connected to at least two tables. I need this view for data entry but whenever I try to enter a new record I get an error. Is there anyway to accomplish this?
Thanks.
0
Comment
Question by:Atouray
[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
  • 3
  • 2
5 Comments
 
LVL 18

Expert Comment

by:brejk
ID: 24748394
Why don't you create a stored procedure to perform inserts to multiple tables in a transaction?
0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 total points
ID: 24748403
An approach I know would be to add an INSTEAD OF UPDATE trigger on your view and then you can write individual update statements for each of the tables identifying which values should be updated to which table.

You can also join in the DELETED table to see if there was actual a change to a value to save on updating a column that didn't change, but hopefully you get the basic point.
-- example
CREATE TRIGGER {trigger name}
ON {schema name}.{view name}
INSTEAD OF UPDATE
AS
-- update first table
UPDATE t
SET t.column_name = i.column_name
FROM table_1_name t 
INNER JOIN inserted i ON t.id_column = i.id_column;
 
-- update second table
UPDATE t
SET t.other_column_name = i.other_column_name
FROM table_2_name t 
INNER JOIN inserted i ON t.id_column = i.id_column;

Open in new window

0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24748462
I forgot, you can also use the Update() function to test, like this:

IF UPDATE(column_name)
BEGIN
-- update table with column_name
UPDATE t
SET t.column_name = i.column_name
FROM table_1_name t
INNER JOIN inserted i ON t.id_column = i.id_column;
END
0
 
LVL 18

Expert Comment

by:brejk
ID: 24748538
And the trigger shouls start with:

IF @@ROWCOUNT = 0
RETURN
0
 
LVL 18

Expert Comment

by:brejk
ID: 24748562
Oh, please ignore my previous post :-) In case of triggers like mentioned here my suggestion is not very useful.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

764 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