473,668 Members | 2,415 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem regarding update after in a trigger for automatic update of date

3 New Member
i have a table of the format

CREATE TABLE testing
(
emp_name varchar(50),
emp_id int,
rec_up datetime DEFAULT (getdate())
)

and on the event of an update to any column in the table i want to
update the "rec_up" field with the current date by firing the following trigger

CREATE TRIGGER trig
ON testing
AFTER UPDATE
AS
IF(UPDATE(emp_n ame) OR UPDATE(emp_id))
BEGIN
UPDATE testing
SET rec_up = getdate()
END

My problem is that this trigger updates all the records with the getdate function
but i want only the record that is updated its 'rec_up' field be updated

Please help
Oct 31 '07 #1
5 2622
ck9663
2,878 Recognized Expert Specialist
i have a table of the format

CREATE TABLE testing
(
emp_name varchar(50),
emp_id int,
rec_up datetime DEFAULT (getdate())
)

and on the event of an update to any column in the table i want to
update the "rec_up" field with the current date by firing the following trigger

CREATE TRIGGER trig
ON testing
AFTER UPDATE
AS
IF(UPDATE(emp_n ame) OR UPDATE(emp_id))
BEGIN
UPDATE testing
SET rec_up = getdate()
END

My problem is that this trigger updates all the records with the getdate function
but i want only the record that is updated its 'rec_up' field be updated

Please help
why don't you just include the rec_up field on your update statement and save the use of trigger. i think i'll be more efficient that way
Oct 31 '07 #2
danishahmed2007
3 New Member
why don't you just include the rec_up field on your update statement and save the use of trigger. i think i'll be more efficient that way
Actually my application is related to the database containing this kind of tables
Here the user does not know about the "rec_up" field and only updates the other fields. I want the "rec_up" field to get updated automatically with the current datetime whenever their is an update or insert in the table without the data entry person knowing about it.
So can you please give a solution to the trigger problem
Nov 1 '07 #3
amitpatel66
2,367 Recognized Expert Top Contributor
Actually my application is related to the database containing this kind of tables
Here the user does not know about the "rec_up" field and only updates the other fields. I want the "rec_up" field to get updated automatically with the current datetime whenever their is an update or insert in the table without the data entry person knowing about it.
So can you please give a solution to the trigger problem
Create a ROW LEVEL trigger and for each row, when any update or insert happens, set rec_up to current time.

Your trigger seems to be statement level. make it row level and include one more WHERE condition in your update statement, something like this:

Expand|Select|Wrap|Line Numbers
  1. UPDATE <table_name> SET rec_up = current_date()
  2. WHERE empno = :new.empname OR empid:= new.empid
  3.  
Nov 1 '07 #4
danishahmed2007
3 New Member
Create a ROW LEVEL trigger and for each row, when any update or insert happens, set rec_up to current time.

Your trigger seems to be statement level. make it row level and include one more WHERE condition in your update statement, something like this:

Expand|Select|Wrap|Line Numbers
  1. UPDATE <table_name> SET rec_up = current_date()
  2. WHERE empno = :new.empname OR empid:= new.empid
  3.  

Hi there thanks for the help but i think ms sql server does not have row level trigger support like in oracle.
But in the meantime i tried the following query . But still i could not get the desired result. Here i have used the inserted and deleted temporary tables
Expand|Select|Wrap|Line Numbers
  1. CREATE TRIGGER trig
  2. ON testing
  3. AFTER UPDATE
  4. AS
  5. IF(UPDATE(emp_id) or UPDATE(emp_name))
  6.    UPDATE testing
  7.    SET rec_up =( SELECT getdate() 
  8.                        FROM INSERTED i,DELETED d
  9.                        WHERE i.emp_id<>d.emp_id OR i.emp_name<>d.emp_name)
  10.  
Nov 1 '07 #5
amitpatel66
2,367 Recognized Expert Top Contributor
Hi there thanks for the help but i think ms sql server does not have row level trigger support like in oracle.
But in the meantime i tried the following query . But still i could not get the desired result. Here i have used the inserted and deleted temporary tables
Expand|Select|Wrap|Line Numbers
  1. CREATE TRIGGER trig
  2. ON testing
  3. AFTER UPDATE
  4. AS
  5. IF(UPDATE(emp_id) or UPDATE(emp_name))
  6.    UPDATE testing
  7.    SET rec_up =( SELECT getdate() 
  8.                        FROM INSERTED i,DELETED d
  9.                        WHERE i.emp_id<>d.emp_id OR i.emp_name<>d.emp_name)
  10.  
Oh ok. Then when you do an update on any record, then does it get stored in INSERTED virtual table?

When you do an insert use the below query for updating rec_up:
Check whether to select system date ,it is current_date() or currentdate()

Expand|Select|Wrap|Line Numbers
  1.  
  2. UPDATE testing
  3.    SET rec_up =current_date() 
  4.  WHERE emp_id = (SELECT emp_id FROM INSERTED);
  5.  
  6.  
Nov 2 '07 #6

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

Similar topics

25
4055
by: Neo Geshel | last post by:
This works: <form> <asp:TextBox id="name" /> <%= name.ClientID %> </form> But this DOES NOT work: <form>
4
1846
by: Jeremy | last post by:
My app is throwing an exception regarding trying to post a null to a required field. Examining the datarow in question, I see a valid date value. The row's state is "added" I'm relying on an oledbcommandbuilder to supply the missing insert or update commands. The following shows how I'm creating my components: Private mdaCertByID As New OleDbDataAdapter 'selects a single cert by ID. .... mdaCertByID.SelectCommand = New OleDbCommand
1
1545
by: rdraider | last post by:
Hi all, I know squat about triggers so was hoping somebody could point me in the right direction. I wanted to copy an email address field from a salesman table to a note field in a customer table. Seems easy enough for a one time update. But I would like to add a trigger to auto-update the customer table anytime an email address changes in the saleman table or a new salesman record is added. Here's my update script (this copies the...
2
4967
by: mob1012 via DBMonster.com | last post by:
Hi All, I wrote last week about a trigger problem I was having. I want a trigger to produce a unique id to be used as a primary key for my table. I used the advice I received, but the trigger is still not working correctly. Here is my code: create trigger emp_update_id BEFORE update on emp_update REFERENCING NEW AS N for each row SET unique_id = Generate_unique();
5
5346
by: wpellett | last post by:
I can not get the SQL compiler to rewrite my SQL UPDATE statement to include columns being SET in a Stored Procedure being called from a BEFORE UPDATE trigger. Example: create table schema1.emp ( fname varchar(15) not null, lname varchar(15) not null, dob date,
1
3801
by: Reshmi Jacob | last post by:
Hello, Can any one help me in creating a trigger to update system date into a table while inserting a record into that table. I tried it like this, it is showing error !!! The following error has occurred: ORA-04091: table ACG.CENTREMST is mutating, trigger/function may not see it ORA-06512: at "ACG.CENTREMST_INSERT", line 5 ORA-04088: error during execution of trigger 'ACG.CENTREMST_INSERT'
1
2914
by: quill | last post by:
Hi I am making a chatroom script and it appears that the problem seems to be that my setTimeout's are conflicting. The logic is as follows: Run a login check every x seconds Run a trigger check every x seconds
11
4071
by: Ed Dror | last post by:
Hi there, I'm using ASP.NET 2.0 and SQL Server 2005 with VS 2005 Pro. I have a Price page (my website require login) with GridView with the following columns PriceID, Amount, Approved, CrtdUser and Date And Edit and Delete buttons
1
1490
by: overlordqd | last post by:
hi all, i want to do something like that; if someone tries to update a table, the trigger will fire and add a new record that contains the field name, old value and new value. i have already done a small part of it. the code is below. set ANSI_NULLS ON
0
8459
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8371
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8889
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8790
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8572
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7391
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2782
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.