473,405 Members | 2,373 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,405 software developers and data experts.

Any Column Updated / Inserted Trigger

Hi,

I'm a SQL Server newbie, so I'd appreciate if someone would tell me if
this is possible. I'm running SQL Server 2000 on Win2k Server

I have one table with a large number of columns. I have two pieces of
logic that I'd like to execute depending upon whether an insert or an
update statement was executed on that table. I'd prefer this
execution to occur from within a single trigger. If a row is
inserted, then I would like to execute logic A. If ANY column in the
table is updated, then I'd like logic B to be executed.

Is it possible to just determine if only "insert" or an "update"
ocurred from within the a single Trigger, without specifying each
individual column name? (I.E. not saying IF udpate(col1) or
update(col2) or ect...) Is it possible to just perform a check on the
process that occurred, irregardless of column? Like If INSERTED =
TRUE then execute insert logic. If UPDATED = TRUE, then run the
updated logic. I would like for all of this code to be stored within
the same trigger.

If anyone can provide some sample code on how to do this, if at all
possible, I would be much appreciative.

Thanks,
-Rigs

PS I know I could do this with 2 seperate triggers, but I'm trying to
avoid that.
Jul 20 '05 #1
2 7153
>Hi,

I'm a SQL Server newbie, so I'd appreciate if someone would tell me if
this is possible. I'm running SQL Server 2000 on Win2k Server

I have one table with a large number of columns. I have two pieces of
logic that I'd like to execute depending upon whether an insert or an
update statement was executed on that table. I'd prefer this
execution to occur from within a single trigger. If a row is
inserted, then I would like to execute logic A. If ANY column in the
table is updated, then I'd like logic B to be executed.

Is it possible to just determine if only "insert" or an "update"
ocurred from within the a single Trigger, without specifying each
individual column name? (I.E. not saying IF udpate(col1) or
update(col2) or ect...) Is it possible to just perform a check on the
process that occurred, irregardless of column? Like If INSERTED =
TRUE then execute insert logic. If UPDATED = TRUE, then run the
updated logic. I would like for all of this code to be stored within
the same trigger.

If anyone can provide some sample code on how to do this, if at all
possible, I would be much appreciative.

Thanks,
-Rigs

PS I know I could do this with 2 seperate triggers, but I'm trying to
avoid that.


Heres a real quick and dirty trigger example

CREATE TRIGGER trgtblClients ON tblClients
FOR INSERT, DELETE, UPDATE AS

DECLARE @ChgInsert CHAR(1)
DECLARE @ChgDelete CHAR(1)
DECLARE @ChgCode CHAR(1)
SET @ChgInsert = 'N'
SET @ChgDelete = 'N'
SET @ChgCode = 'N'
IF exists(select top 1 FROM inserted)
SET @ChgInsert = 'Y'

If exists(select top 1 from deleted)
SET @ChgDelete = 'Y'

/* Check for a insert */
IF @ChgInsert = 'Y' AND @ChgDelete = 'N'
Begin
SET @ChgCode = 'I'
End

/* Check for a change */
IF @ChgInsert = 'Y'AND @ChgDelete = 'Y'
Begin
SET @ChgCode = 'C'
End

/* Check for a delete */
IF @ChgInsert = 'N' AND @ChgDelete = 'Y'
Begin
SET @ChgCode = 'D'
End

IF @ChgCode = 'C'
BEGIN
/* DO YOUR CHANGE PROCESSING HERE */
END
IF @ChgCode = 'I'
BEGIN
/* DO YOUR INSERT PROCESSING HERE */

END
IF @ChgCode = 'D'
BEGIN
/* DO YOUR DELETE PROCESSING HERE */
END


Randy
http://members.aol.com/rsmeiner
Jul 20 '05 #2
On 15 Apr 2004 13:01:40 -0700, Rigs wrote:
PS I know I could do this with 2 seperate triggers, but I'm trying to
avoid that.


Hi Rigs,

Why are you trying to avoid using 2 seperate triggers?

I see a case for combining insert and update triggers if use need to
execute the SAME code on insert and on update. But since you have to
execute different code for each case, I'd think that using two
triggers provides a better documented system that's easier to
understand and easier to maintain. And your performance will improve
as well (allthough so little that you won't notice).

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: R. Santiago | last post by:
I have a table cc_rd_user_questions with the following columns: NUM NUMBER (25) DEFAULT 1 NOT NULL, COMPANY_ID NUMBER (15) NOT NULL, PROJ_ID NUMBER (15) NOT NULL, SEQ ...
1
by: Lisa Tang | last post by:
Hi, I have a table t1 with a long raw column, and I need to create a view v1 with a long raw column being a function ff1 of the long raw column in t1. And I need to update t1 with reverse...
5
by: Leonardo Almeida | last post by:
This is the case.... I would like to learn the statement that make the relation between these tables. Why? Cos these are separated in two different databases and if a user make an update in a...
1
by: Leonardo Almeida | last post by:
This is the case.... I would like to learn the statement that make the relation between these tables. Why? Cos these are separated in two different databases and if a user make an update in a...
4
by: Rigs | last post by:
Hi, I'm a SQL Server newbie, so I'd appreciate if someone would tell me if this is possible. I'm running SQL Server 2000 on Win2k Server I have one table with a large number of columns. I...
3
by: Jim Archer | last post by:
Hi All... I'm been fighting this problem for a few days now, and it seems like it should be simple. But the solution has eluded me so far... I need to flag a record when it is updated or when...
5
by: bsandell | last post by:
Hi, I have a view that looks something like this - CREATE VIEW myview AS SELECT myudf(col1) as col1, col2, col3 FROM mytable The view has an 'INSTEAD OF' trigger on it to do the correct...
3
by: Shestine | last post by:
I am trying to add a column to a current table, with data in it. I am only learning, and i have no idea how to change this to make it work. Here is the script I have right now it, but what it does is...
1
by: rverghese | last post by:
I have a BEFORE UPDATE trigger on a table that calls the tsvector_update_trigger() built in function to update a tsvector field. Since this trigger is called for every update to the table, the...
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: 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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.