Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Table insert on update trigger

Author  Topic 

Leonius
Starting Member

7 Posts

Posted - 2011-08-25 : 06:29:03
Two tables,

T1 holds Location, gamestocklevel, gamename, dateStocked,

T2 holds historical data from t1 so same columns,

UniqueID, Location, gamestocklevel, gamename, dateStocked,

The UniqueID column which is a seeded autonumber to keep records unique,

What I want to do is if the gamename, on table 1 is updated (say for instance the game name changes so its now different stock in that location)

I want to insert a new row on t2,

Location, gamestocklevel, gamename, dateStocked,

I cant seem to get this one to work.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-08-25 : 06:33:44
something like


CREATE TRIGGER Trg_Update ON T1
AFTER UPDATE
AS
BEGIN
INSERT INTO T2 (Location, gamestocklevel, gamename, dateStocked)
SELECT Location, gamestocklevel, gamename, dateStocked
FROM INSERTED i
JOIN DELETED d
ON d.PK = i.PK
WHERE d.gamename <> i.gamename
END


PK is primary key of T1

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -