Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Update multiple tables with view SQL Server 2008

Posted on 2009-06-30
5
Medium Priority
?
1,630 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

609 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