Solved

Update multiple tables with view SQL Server 2008

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
email about the whoisactive result 7 36
SQL Server Insert where not exists 24 41
SSRS  - Dropdown with Null 3 25
SQL Server 2012 r2 - Sum totals 2 25
In this article I will describe the Backup & Restore 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 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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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