Solved

Update multiple tables with view SQL Server 2008

Posted on 2009-06-30
5
1,615 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
  • 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 59

Accepted Solution

by:
Kevin Cross earned 500 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 59

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

Industry Leaders: 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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

713 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