|
-
Apr 18th, 2007, 04:52 AM
#1
Thread Starter
Fanatic Member
SQL Server Tigger Update
Hi all,
im trying to figure out how to do the following.
I have a table orders, tbl_Orders, and a backup orders table tbl_Orders_bak.
Each time a record is updated from the tbl_Orders i want to take a copy of the entire row before the update occurs and put it into the tbl_Orders_bak
what the best to do this. I would have thought a BEFORE UPDATE trigger but it doesnt exist in SQL Server
Barry
Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
.NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0
SQL Server 2005/2000/SQL Server CE 2.0
If you like, rate this post
Compact Framework for Beginners
-
Apr 18th, 2007, 05:17 AM
#2
Thread Starter
Fanatic Member
Re: SQL Server Tigger Update
I used the following:
FOR UPDATE
AS
BEGIN
INSERT INTO tbl_Orders_bak
SELECT * FROM DELECTED
END
dunno if this is a good way or not... but it works.....
Barry
Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
.NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0
SQL Server 2005/2000/SQL Server CE 2.0
If you like, rate this post
Compact Framework for Beginners
-
Apr 18th, 2007, 07:32 AM
#3
Re: SQL Server Tigger Update
FOR without AFTER or INSTEAD of defaults to AFTER - which is good - this trigger only fires AFTER all cascades and ref-integrity is checked.
Then basically your trigger is quite simple - it's taking the contents of the DELETED temp table and inserting into another table.
Seems fine to me!
-
Apr 18th, 2007, 05:32 PM
#4
Thread Starter
Fanatic Member
Re: SQL Server Tigger Update
the only problem is that it doesnt work when using image or ntext fields....
not an issue for my current project... but may be of use again
Barry
Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
.NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0
SQL Server 2005/2000/SQL Server CE 2.0
If you like, rate this post
Compact Framework for Beginners
-
Apr 18th, 2007, 05:37 PM
#5
Re: SQL Server Tigger Update
From MS BOL
SQL Server 2000 does not allow text, ntext, or image column references in the inserted and deleted tables for AFTER triggers; however, these column references are allowed for INSTEAD OF triggers
-
Apr 18th, 2007, 05:45 PM
#6
Thread Starter
Fanatic Member
Re: SQL Server Tigger Update
so would this work for an after trigger?
SET @iId = SELECT iId FROM INSERTED
INSERT INTO tbl_Orders_bak
SELECT * FROM tbl_Orders WHERE iId = @iId
DELETE FROM tbl_Orders WHERE iId = @iId
INSERT INTO tbl_Orders
SELECT * FROM INSERTED
Barry
Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
.NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0
SQL Server 2005/2000/SQL Server CE 2.0
If you like, rate this post
Compact Framework for Beginners
-
Apr 18th, 2007, 05:48 PM
#7
Re: SQL Server Tigger Update
I don't use triggers enough (at all actually!) - I would have to scour books online to get a handle on the difference between AFTER and INSTEAD OF...
INSTEAD OF still has the DELETED and INSERTED tables - it's just when the trigger fires that matters - and the fact you can adjust what ACTION occurs in an INSTEAD OF trigger.
-
Apr 18th, 2007, 05:51 PM
#8
Thread Starter
Fanatic Member
Re: SQL Server Tigger Update
ah ok... cheers for the pointers...
Barry
Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
.NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0
SQL Server 2005/2000/SQL Server CE 2.0
If you like, rate this post
Compact Framework for Beginners
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|