I want to insert the current datetime whenever a new row is inserted or updated.
The getdate() gives the datetime whenever a row is inserted. But it doesn’t update itself at the time of row update.
Is there any way to do this?
Edit: I don't want to use Triggers.
This is the Trigger you need for update:
CREATE TRIGGER Update ON TABLE1
FOR UPDATE
AS
BEGIN
SET NOCOUNT ON
UPDATE TABLE1
SET UpdatedOn = GETDATE()
FROM TABLE1 A
INNER JOIN Inserted INS ON (A.Id = INS.Id)
SET NOCOUNT OFF
END
A stored procedure may help you then, but then an ad-hoc update operation will lead to inconsistent data.
Related
In MySQL, I want to fire a trigger when update on all columns except one column update.
In my table row I have 40 columns. I want trigger to update the column update_time whenever there is an update happens on any field except update_time field.
CREATE TRIGGER `UpdateDateTrigger`
BEFORE UPDATE ON `users`
FOR EACH ROW
IF NOT UPDATE(`update_time`) BEGIN
SET new.update_timestamp = now()
END
But it is not working as expected.
By looking at MySQL automatic update for specific column instead of whole record , I've got the solution to exclude two columns update in trigger
IF !((NEW.last_visited <> OLD.last_visited) ||
(NEW.update_time <> OLD.update_time)) THEN
SET new.update_time = now();
END IF
Thank you!
Instead of having a trigger function I would like to suggest you to create the table wisely so that the update_time field gets updated automatically when something is changed associated with that row.
Please look into the documentation here for automatic update on time for each update of the row. The create syntax is simple and effective.
CREATE TABLE t1 (
// .... Other columns
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
dt DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
If you want a specific control over each of your column, then writing a trigger is the best idea I think. Please check the answer here.
I'm fairly new to triggers and have already tried searching for a solution to my question with little results. I want to update a single row's start time column whenever it's active column is set to 1.
I have two columns ACTIVE (number) and START_TIME (timestamp) in my_table. I would like to create a PL/SQL trigger that updates the START_TIME column to current_timestamp whenever an update statement has been applied to the ACTIVE column - setting it to 1.
So far I have only seen examples for inserting new rows or updating entire tables which isn't what I'm looking to do. I'd have thought there would be a fairly simple solution to my problem.
This is what I've got so far from other examples. I know the structure of my solution is poor and I'm asking for any input to modify my trigger to achieve my desired result.
CREATE OR REPLACE TRIGGER routine_active
AFTER UPDATE ON my_table
FOR EACH ROW
WHEN (my_table.ACTIVE = 1)
begin
insert my_table.start_time = current_timestamp;
end;
\
you can use like this .it may help you
write the update query instead of insert query
CREATE OR REPLACE TRIGGER routine_active
AFTER UPDATE ON my_table
FOR EACH ROW
WHEN (new.ACTIVE = 1)
begin
update my_table set start_time =current_timestamp;
end;
I think it should be a BEFORE UPDATE, not AFTER UPDATE, so it saves both changes with a single action. Then you don't need the INSERT or UPDATE statements. I also added the "OF active" clause, so it will only start this trigger if that column was updated, which may reduce the workload if other columns get updated.
CREATE OR REPLACE TRIGGER routine_active
BEFORE UPDATE OF active ON my_table
FOR EACH ROW
BEGIN
IF active = 1
THEN
:NEW.start_time = current_timestamp;
END IF;
END;
I have a delete trigger on a table in my DB that is supposed to be inserting rows into an "audit" table, but when I delete rows from the trigger table, they do not show up in the audit table. I have insert and update triggers following this same format that work fine but I can't seem to get this one to work. Here is the trigger:
USE [OutageDev]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[tr_audOutageSummaryDelete]
ON [dbo].[Outage Summary]
AFTER DELETE
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
INSERT INTO dbo.[Outage Summary Audit]
(OutageSummaryID,
Dispatcher,
/*Removed columns for brevity*/
StateEmergency,
Comment,
AuditDateTime,
AuditUser,
AuditAction)
SELECT d.OutageSummaryID,
d.Dispatcher,
/*Removed columns for brevity*/
d.StateEmergency,
t.Comment,
GETDATE(),
SUSER_SNAME(),
'DELETE'
FROM deleted d
JOIN [Outage Summary] t
ON d.OutageSummaryID = t.OutageSummaryID
END
For future reference, I was joining the deleted table with the trigger table in order to select columns that were of type ntext because this was how it worked for my insert and update triggers. Since this is an after delete trigger, the row I was trying to select was already deleted. I converted columns from ntext to nvarchar(max) and got rid of the join and it works fine now.
Total novice at triggers... all the docs don't bother with beginner stuff.
I just want to update the row(s) that has/have been updated. My trigger below updates the entire table. The trigger below just tests two columns for changes.
How do I restrict this update trigger to the update only the updated rows and not the entire table?
ALTER TRIGGER [dbo].[geog_update] ON [dbo].[Site]
FOR UPDATE
AS
SET NOCOUNT ON
IF (UPDATE(Latitude) OR UPDATE(Longitude))
BEGIN
UPDATE Site
SET geog = geography::Point([Latitude], [Longitude], 4326)
WHERE Latitude is not null and Longitude is not null
END
Can I use the same trigger for inserted rows by just using FOR UPDATE, INSERT? Probably not since the IF checks for UPDATE() on a column, unless INSERT implies UPDATE.
Ok first, you never under any circumstances design a trigger to update only one row in SQL Server. You design it to update the rows that were inserted, deleted or updated. Triggers operate on batches and you cannot assume that you will never change more than one record in code that hits the database.
You do that by joining to one of two pseudotables that are available only in triggers, inserted or deleted. Inserted contains the new records or the values after an update, delted contains the values for the records deleted or the values before an update occurred.
You could try something like:
ALTER TRIGGER [dbo].[geog_update] ON [dbo].[Site]
FOR UPDATE
AS
SET NOCOUNT ON
UPDATE S
SET geog = geography::Point(i.[Latitude], i.[Longitude], 4326)
FROM Site s
JOIN Inserted I on s.id = i.id
WHERE Latitude is not null and Longitude is not null
Here is the final UPDATE trigger. I made an INSERT trigger that is almost identical.
CREATE TRIGGER [dbo].[geog_update] ON [dbo].[Site]
FOR UPDATE
AS
SET NOCOUNT ON
IF (UPDATE(Latitude) OR UPDATE(Longitude))
BEGIN
UPDATE t1
SET t1.geog = geography::Point(t1.Latitude, t1.Longitude, 4326)
FROM Site AS t1 INNER JOIN Inserted AS t2 ON t1.Site_ID = t2.Site_ID
WHERE (t1.Latitude IS NOT NULL)
AND (t1.Longitude IS NOT NULL)
AND (t1.Record_Archive_Date IS NULL)
END
I'm trying to use a trigger to update a column on my database when a row gets updated.
This is the trigger
CREATE
DEFINER=`root`#`localhost`
TRIGGER `mysql_development`.`update_translated_position`
BEFORE UPDATE ON `mysql_development`.`players_to_teams`
FOR EACH ROW
BEGIN
UPDATE players_to_teams
INNER JOIN position_translator
ON NEW.position = position_translator.RawPosition
SET NEW.translated_position = position_translator.NCAAposAbbrev1;
END$$
I need to "calculate" the translated_position from the raw position input (in case someone gives me a non-standard position).
I think this is locking the row, because I am getting a 1096, no table used error.
I need to update the players_to_teams row being updated using the external position_translator table.
Use SET directly rather than UPDATE (and therefore avoid the join altogether):
SET NEW.translated_position := (
SELECT NCAAposAbbrev1 FROM position_translator WHERE RawPosition = NEW.position
);