473,385 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

MS SQL Trigger Handling Multiple Inserts Question

I am currently trying to write a trigger that when an insert or update is made to a table, it inserts a copy of the row into a log table. It currently works when one row is inserted, but it will error if you try to do multiple inserts or modifies to the same table. Do I need a loop to do this, or what do you guys suggest? My code is as follows. Thanks!

CREATE TRIGGER trgTestLogging
ON dbo.test
FOR INSERT, UPDATE
AS

DECLARE @productID int
SELECT @productID = (SELECT productid FROM INSERTED)
DECLARE @name VARCHAR(255)
SELECT @name = (SELECT name FROM INSERTED)
DECLARE @logdate datetime
SELECT @logdate = (SELECT logdate FROM INSERTED)
DECLARE @uptype varchar(50)
SELECT @uptype = (SELECT uptype FROM INSERTED)

INSERT INTO test_log(productid, name, logdate, uptype) VALUES(@productID, @name, @logdate, @uptype)
May 9 '07 #1
0 2334

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Jason | last post by:
I have a table that matches up Securities and Exchanges. Individual securities can belong on multiple exchanges. One of the columns, named PrimaryExchangeFlag, indicates if a particular exchange is...
4
by: Lauren Quantrell | last post by:
I have created the following trigger: CREATE TRIGGER ON OutputTable FOR INSERT AS Declare @filename nvarchar(35) Declare @filecontents nvarchar(2000) Declare @strcmdshell varchar(150)
6
by: Scott CM | last post by:
I have a multi-part question regarding trigger performance. First of all, is there performance gain from issuing the following within a trigger: SELECT PrimaryKeyColumn FROM INSERTED opposed...
7
by: ZRexRider | last post by:
Hi, I have trigger that enforces the creation of a sortorder that is always 1 digit higher than the current highest on Inserts. This trigger works great if I add one row at a time so I think...
33
by: coosa | last post by:
I have a table: ---------------------------------------------------- CREATE TABLE CATEGORY ( CATEGORY_ID INTEGER IDENTITY(1,1) NOT NULL, CATEGORY_NAME VARCHAR(40) NOT NULL,...
4
by: Paul Reddin | last post by:
Hi, Having just tested our database on V8.2 we get the following apparent incompatibility. A Trigger conatains the following line CREATE TRIGGER JABS.AU_CHNG_ENQUIRYITM AFTER UPDATE OF ..
1
by: Daniel Schuchardt | last post by:
Hi list, i'm a bit confused. I have a table with a trigger after insert. It is possible that this trigger will do inserts in the same table, but is the after insert trigger fired again in this...
1
by: julie | last post by:
Why might this trigger cause an 803 error message when only 1 record insert is ocurring? If I take the primary key off the FR_HACCP_EOR_RES table the insert occurs and only inserts 1 record. If...
10
by: JohnO | last post by:
Hi All, This question is related to iSeries V5R4 and db2. I want to implement an AFTER DELETE trigger to save the deleted rows to an archive table, I initially defined it as a FOR EACH...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.