Solved

Update multiple tables with view SQL Server 2008

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

A quick step-by-step overview of installing and configuring Carbonite Server Backup.
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

707 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

19 Experts available now in Live!

Get 1:1 Help Now