Solved

SQL Server Triggers

Posted on 2010-09-07
3
350 Views
Last Modified: 2012-08-14
Is there an example of a trigger in SQL Server that updates a different table. For example, I need to update the history table by inserting a new record when a field is updated in the parent table. Any suggestions or example?
0
Comment
Question by:dodgerfan
[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 Comments
 
LVL 60

Accepted Solution

by:
chapmandew earned 250 total points
ID: 33620096
create trigger mytrigger
on mytablename
after update
as
begin
insert into anotherdb.dbo.tablename(fieldlist)
select fieldlist
from inserted
end
0
 
LVL 26

Assisted Solution

by:Shaun Kline
Shaun Kline earned 250 total points
ID: 33620179
Assuming you want to put the old values into history:
CREATE TRIGGER <trigger name> ON <your table>
INSTEAD OF UPDATE
AS 
BEGIN
    SET NOCOUNT ON;

    INSERT INTO <history table>
    SELECT <field values>
    FROM <your table>
        INNER JOIN Inserted ON <your table>.<key field> = Inserted.<key field>

    UPDATE <your table>
    SET <field list> = Inserted.<field list>
    FROM <your table>
        INNER JOIN Inserted ON <your table>.<key field>= Inserted.<key field>
END

Open in new window

0
 

Author Comment

by:dodgerfan
ID: 33620628
This is what I have so far, and I'm getting an error:

create trigger updatehistory
on booking
after update
as
begin
insert into bookinghistory(hotelno,guestno,datefrom,dateto,roomno)
select hotelno,guestno,datefrom,dateto,roomno
from inserted
end

The error says:The row value(s) updated or deleted do not make the row unique or they alter mulitple rows(2 rows).
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

In this article I will describe the Detach & Attach 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.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

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