Hi I have the following trigger.
ALTER TRIGGER [dbo].[DivisionLastModified] ON [dbo].[tb_Division]
WITH EXECUTE AS CALLER
FOR INSERT, UPDATE
AS
BEGIN
UPDATE tb_Division SET LastModified = GetDate()
END
This updates all rows however I only wish to modify the update/added row.
Is this achievable?
This is Because you update all Rows
UPDATE tb_Division SET LastModified = GetDate()
You must specify the last inserted or updated row id,
so you can specify that in Where condition some thing like this
UPDATE tb_Division SET LastModified = GetDate() where id=4
To just update LastModified on the rows that were affected by the insert or update you need to use the inserted table which is only available in triggers.
ALTER TRIGGER [dbo].[DivisionLastModified]
ON [dbo].[tb_Division]
WITH EXECUTE AS CALLER FOR INSERT, UPDATE AS
BEGIN
UPDATE tb_Division
SET LastModified = GetDate()
WHERE tb_Division.<Primary Key Column Name Here> IN (
SELECT <Primary Key Column Name Here> FROM inserted
)
END
Related
How to update date Column DateMod to today's date when Column CustomerProductID is updated (not inserted) using an after update trigger in T-SQL?
Some background info: Table already contains list of products (key column Itemcode), once the CustomerProductID is received it changes the column for that particular row (product) from NULL to integer value. This update is the trigger to update column DateMod to todays date for the row (product).
I am using SSMS 2008 and have something like the following code which changes the whole date column, not the particular date field for the updated row:
CREATE TRIGGER trigger_name
ON Table1
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON;
IF UPDATE (CustomerProductID) BEGIN
Update Table1
SET DateMod=GETDATE()
END
END
I have read some solutions using old.value and new.value or using where exists (select from inserted/updated), but how does that work? If both methods work, which one is the most beneficial in this case?
Thanks a lot!
I prefer do this as a before update trigger (a logical thing . . . doing updates in after update triggers suggests infinite loops and is not allowed in some databases). But SQL Server doesn't support that.
In any case, the right syntax is to use inserted to join back to the original table:
CREATE TRIGGER trigger_name
ON Table1
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON;
IF UPDATE(CustomerProductID) BEGIN
Update t1
SET DateMod = GETDATE()
FROM Table1 t1 join
Inserted i
ON Table1.PrimaryKeyColumn = i.PrimaryKeyColumn
END
END
Change the code so PrimaryKeyColumn is the right primary key column.
I am using the following trigger to track the last modified date in a table:
CREATE TRIGGER trg_UpdateTimeEntry
ON dbo.TimeEntry
AFTER UPDATE
AS
UPDATE dbo.TimeEntry
SET ModDate = GETDATE()
WHERE ID IN (SELECT DISTINCT ID FROM Inserted)
It says "AFTER UPDATE", but even when I insert a row, it sets the ModDate column to the same as the entry date. How can I stop this from happening? I only want it to change when I make a chance to a row, not when a new row is added (it should stay NULL in this case).
Thanks!
CREATE TRIGGER trg_update_my_table on my_table
FOR UPDATE AS
BEGIN
UPDATE my_table
SET modified_on=getdate()
FROM my_table INNER JOIN deleted d
on my_table.id = d.id
END
GO
OR you can change definition of or existing trigger to
CREATE TRIGGER trg_UpdateTimeEntry
ON dbo.TimeEntry
AFTER UPDATE
AS
UPDATE dbo.TimeEntry
SET ModDate = GETDATE()
WHERE ID IN (SELECT DISTINCT ID FROM deleted)
inserted table will have all the newly inserted records and new values for any updated records, deleted table will have only old values of the records that were updated.
I wonder if it is possible to create a table that has a created date and updated date every time a record is created or updated.
For example, when I insert a record into that table, the created date will auto generated in the table same with the update date.
When I modify this record, the create date won't change but the update date will change according to the date.
Many thanks
CREATE TABLE dbo.foo
(
ID INT IDENTITY(1,1),
CreatedDate DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
UpdatedDate DATETIME NULL
);
GO
CREATE TRIGGER dbo.foo_ForUpdate
ON dbo.foo
FOR UPDATE
AS
BEGIN
SET NOCOUNT ON;
UPDATE f SET UpdatedDate = CURRENT_TIMESTAMP
FROM dbo.foo AS f
INNER JOIN inserted AS i
ON f.ID = i.ID;
END
GO
You can set the default value for the column to be equal to GetDate() and this will set the Created Date to the time when the record was created. This will not work for UpdatedDate because default values will be used when the record is created. For this column you can use after update trigger. Here is a link that shows how to create one :
http://www.sqlservercurry.com/2010/09/after-update-trigger-in-sql-server.html
Trigger is a most suitable option. You can refer the sample for trigger as follows.
CREATE TRIGGER dbo.TableTriggerName
ON dbo.TableName
AFTER INSERT, UPDATE, DELETE
AS
BEGIN
SET NOCOUNT ON;
--
-- Check if this is an INSERT, UPDATE or DELETE Action.
-- Set Action to Insert by default.
IF EXISTS(SELECT * FROM DELETED)
BEGIN
END
ELSE
IF NOT EXISTS(SELECT * FROM INSERTED) RETURN; -- Nothing updated or inserted.
...
END
Trigger Reference
i have simple question regarding Triggers in sql.
I am completely new and i do not know how to handle it.
I have one table myshift with shiftid, starttime stoptime and lastupdate.
create trigger ShiftTriggerr on myshift for update as
if update(stoptime)
update myshift set lastupdated = getdate()
what i want is when stoptime will update, the lastupdate field will update with getdate().
but when i run this it does not update one row but updated all rows. I do not know how to apply check on this trigger
You need to use the INSERTED virtual table:
CREATE TRIGGER dbo.ShiftTriggerr
ON dbo.myshift AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON;
IF UPDATE(stoptime)
BEGIN
UPDATE A
SET lastupdated = getdate()
FROM dbo.myshift A
INNER JOIN INSERTED B
ON A.shiftid = B.shiftid
END
END
myshift should have a primary key defined, if that is shiftId, then,
create trigger ShiftTriggerr on myshift for update as
update m
set lastupdated = getdate()
From myshift m Join inserted i
on i.shiftId = m.shiftId
I have written a trigger that I want to use for adding the date to a column in a record so that I can keep track of the insert of the item.
There are a large amount of inserts being called (about 20000) and I have noticed that the trigger will update all of the InsertDate columns associated with each item every time a new item is added. How can I make sure this happens to an item being inserted only one time.
My trigger is as follows:
SET ANSI_NULLS ON
SET QUOTED_INDENTIFIER ON
GO
CREATE TRIGGER [InsertDate_Item]
ON [dbo].[ItemHolder]
AFTER INSERT
NOT FOR REPLICATION
AS
UPDATE ItemHolder SET InsertDate = GETDATE()
Any help will be much appreciated.
Thanks
You need to restrict rows to those inserted... using the virtual trigger table INSERTED
CREATE TRIGGER [InsertDate_Item]
ON [dbo].[ItemHolder]
AFTER INSERT
NOT FOR REPLICATION
AS
SET NOCOUNT ON
UPDATE IH
SET InsertDate = GETDATE()
FROM
ItemHolder IH
JOIN
INSERTED INS ON IH.keycol = INS.keycol
Go
One thing: You'd be better adding a default to the table instead. No need for a trigger
ALTER TABLE ItemHolder ADD
CONSTRAINT DF_ItemHolder_InsertDate DEFAULT (GETDATE()) FOR InsertDate
If you are inserting records into a table, why don't you make the InsertDate field have a default value of GetDate()? That avoids the trigger altogether.
I'd go about what you're trying to do without a trigger. Just set the default value of the column to GetDate().
update ih
set ih.insertdate = GetDate()
from itemholder ih inner join inserted i
on ih.itemholderid = i.itemholderid
change:
UPDATE ItemHolder SET InsertDate = GETDATE()
to:
UPDATE ItemHolder SET InsertDate = GETDATE() WHERE InsertDate IS NULL
The above will only set InsertDate if it is null and you still want to use the trigger. Of course, this is assuming the default value of InsertDate is null. If this is not the case let me know.