I wrote the following trigger to update two columns in my table when any record is inserted or updated:
CREATE TRIGGER test
ON mytable
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON;
UPDATE t
SET UPDATE_TIMESTAMP = GETDATE(),
UPDATE_USER_NAME = SUSER_SNAME()
FROM mytable t
INNER JOIN inserted i ON t._ID = i._ID
END
GO
However, it does not update the columns UPDATE_TIMESTAMP and UPDATE_USER_NAME and still returns the default null. Could you please help me how to fix that? Thanks!
I think that you are facing a recursing call in your trigger
Try with this :
CREATE TRIGGER test
ON mytable
AFTER INSERT,UPDATE
AS
BEGIN
SET NOCOUNT ON;
IF TRIGGER_NESTLEVEL() > 1
RETURN;
UPDATE t
SET UPDATE_TIMESTAMP = GETDATE(),
UPDATE_USER_NAME = SUSER_SNAME()
FROM mytable t
INNER JOIN inserted i ON t._ID = i._ID
END
GO
Related
I am trying to execute a mass update on table based on a condition.
UPDATE POHeader
SET POStatus = 'Cancelled'
WHERE POStatus = 'Draft'
AND Created <= DATEADD(DD, DATEDIFF(DD, '', GETDATE())-29, '')
I even tried these which also return the same error:
UPDATE POHeader
SET POStatus = 'Cancelled'
WHERE POID in (18364, 26401, 27284, 28575, 30532, 30599, 33650,
33780, 33783, 33785, 33895, 35644, 35647, 35678,
37400, 37546, 38750, 38758, 40568, 42045, ......)
Please help as the amount to update is reaching thousands and doing it 1 by 1 is not an option.
UPDATE Trigger
ALTER TRIGGER [dbo].[POHeader_UPDATE]
ON [dbo].[POHeader]
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO [dbo].[POHeaderLog]
([ActionDate]
,[Action]
,[POID]
,[CustomerName]
,[CustomerCode]
, ...)
VALUES
(GETDATE()
,'Update'
,(select INSERTED.POID from INSERTED)
,(select INSERTED.CustomerName from INSERTED)
,(select INSERTED.CustomerCode from INSERTED)
, ...)
END
The problem is with the trigger code. SQL Server's trigger operate a statement level, so there will be multiple rows in inserted. if the update statement that fired the trigger affected more than one row - hence the error that you are getting.
You can rewrite the trigger code using the insert ... select syntax:
ALTER TRIGGER [dbo].[POHeader_UPDATE]
ON [dbo].[POHeader]
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO [dbo].[POHeaderLog]
([ActionDate]
,[Action]
,[POID]
,[CustomerName]
,[CustomerCode]
, ...)
SELECT
GETDATE()
,'Update'
,POID
,CustomerName
CustomerCode,
, ...
FROM inserted;
END
I have tested that trigger but it works only with one insert row.
The trigger fails with multiple-row insert and I didn't find the syntax to fix the trigger.
Any suggestion to fix that?
Thanks to the stackoverflow users that let me notice the problem.
USE [MY_DB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[tr_update_father]
ON [dbo].[PROD_IVR_CALL]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
DECLARE #tnumber nvarchar(50), #id_inserted int, #id_prod_file int;
select
#id_inserted = ins.id,
#tnumber = ins.tnumber ,
#id_prod_file = pf.ID_PROD_FILE
from inserted ins
inner join prod_failure pf on (ins.ID_PROD_FAILURE = pf.ID);
update prod_ivr_call
set id_father = sq.ID
from
(select min(pic.id) as ID
from prod_ivr_call pic
inner join prod_failure pf on (pic.ID_PROD_FAILURE = pf.ID)
where pic.tnumber = #tnumber
and pic.is_requested = 0
and pf.ID_PROD_FILE = #id_prod_file
group by pic.tnumber ) sq
END
Your UPDATE statement is not syntactically correct. You can actually merge the two statements of your trigger using a CTE, and then do the UPDATE on this CTE:
ALTER TRIGGER [dbo].[tr_update_father]
ON [dbo].[PROD_IVR_CALL]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
;WITH ToUpdate AS (
SELECT pic.id_father,
MIN(pic.id) OVER (PARTITION BY pic.tnumber) AS min_id
FROM prod_ivr_call pic
INNER JOIN join prod_failure pf ON pic.ID_PROD_FAILURE = pf.ID
JOIN inserted ins ON ins.ID_PROD_FAILURE = pf.ID
WHERE pic.tnumber = ins.tnumber AND pic.is_requested = 0
)
UPDATE ToUpdate
SET id_father = min_id
END
I have a problem with this trigger. I would like it to update the requested information
only to the row in question (the one I just updated) and not the entire table.
CREATE TRIGGER [dbo].[after_update]
ON [dbo].[MYTABLE]
AFTER UPDATE
AS
BEGIN
UPDATE MYTABLE
SET mytable.CHANGED_ON = GETDATE(),
CHANGED_BY=USER_NAME(USER_ID())
How do I tell the trigger that this applies only to the row in question?
Here is my example after a test
CREATE TRIGGER [dbo].UpdateTasadoresName
ON [dbo].Tasadores
FOR UPDATE
AS
UPDATE Tasadores
SET NombreCompleto = RTRIM( Tasadores.Nombre + ' ' + isnull(Tasadores.ApellidoPaterno,'') + ' ' + isnull(Tasadores.ApellidoMaterno,'') )
FROM Tasadores
INNER JOIN INSERTED i ON Tasadores.id = i.id
The inserted special table will have the information from the updated record.
Try this (update, not after update)
CREATE TRIGGER [dbo].[xxx_update] ON [dbo].[MYTABLE]
FOR UPDATE
AS
BEGIN
UPDATE MYTABLE
SET mytable.CHANGED_ON = GETDATE()
,CHANGED_BY = USER_NAME(USER_ID())
FROM inserted
WHERE MYTABLE.ID = inserted.ID
END
you can call INSERTED, SQL Server uses these tables to capture the data of the modified row before and after the event occurs.I assume in your table the name of the key is Id
I think the following code can help you
CREATE TRIGGER [dbo].[after_update]
ON [dbo].[MYTABLE]
AFTER UPDATE
AS
BEGIN
UPDATE dbo.[MYTABLE]
SET dbo.[MYTABLE].CHANGED_ON = GETDATE(),
dbo.[MYTABLE].CHANGED_BY = USER_NAME(USER_ID())
FROM INSERTED
WHERE INSERTED.Id = dbo.[MYTABLE].[Id]
END
You should be able to access the INSERTED table and retrieve ID or table's primary key. Something similar to this example ...
CREATE TRIGGER [dbo].[after_update] ON [dbo].[MYTABLE]
AFTER UPDATE AS
BEGIN
DECLARE #id AS INT
SELECT #id = [IdColumnName]
FROM INSERTED
UPDATE MYTABLE
SET mytable.CHANGED_ON = GETDATE(),
CHANGED_BY=USER_NAME(USER_ID())
WHERE [IdColumnName] = #id
Here's a link on MSDN on the INSERTED and DELETED tables available when using triggers: http://msdn.microsoft.com/en-au/library/ms191300.aspx
It is very simple to do that,
First create a copy of your table that your want keep the log for
For example you have Table dbo.SalesOrder with columns SalesOrderId, FirstName,LastName, LastModified
Your Version archieve table should be dbo.SalesOrderVersionArchieve with columns SalesOrderVersionArhieveId, SalesOrderId, FirstName,LastName, LastModified
Here is the how you will set up a trigger on SalesOrder table
USE [YOURDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Karan Dhanu
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE TRIGGER dbo.[CreateVersionArchiveRow]
ON dbo.[SalesOrder]
AFTER Update
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO dbo.SalesOrderVersionArchive
SELECT *
FROM deleted;
END
Now if you make any changes in saleOrder table it will show you the change in VersionArchieve table
try this solution.
DECLARE #Id INT
DECLARE #field VARCHAR(50)
SELECT #Id= INSERTED.CustomerId
FROM INSERTED
IF UPDATE(Name)
BEGIN
SET #field = 'Updated Name'
END
IF UPDATE(Country)
BEGIN
SET #field = 'Updated Country'
END
INSERT INTO CustomerLogs
VALUES(#Id, #field)
// OR
-- If you wish to update existing table records.
UPDATE YOUR_TABLE SET [FIELD]=[VALUE] WHERE {CONDITION}
I didn't checked this with older version of sql server but this will work with sql server 2012.
Try this script to create a temporary table TESTTEST and watch the order of precedence as the triggers are called in this order: 1) INSTEAD OF, 2) FOR, 3) AFTER
All of the logic is placed in INSTEAD OF trigger and I have 2 examples of how you might code some scenarios...
Good luck...
CREATE TABLE TESTTEST
(
ID INT,
Modified0 DATETIME,
Modified1 DATETIME
)
GO
CREATE TRIGGER [dbo].[tr_TESTTEST_0] ON [dbo].TESTTEST
INSTEAD OF INSERT,UPDATE,DELETE
AS
BEGIN
SELECT 'INSTEAD OF'
SELECT 'TT0.0'
SELECT * FROM TESTTEST
SELECT *, 'I' Mode
INTO #work
FROM INSERTED
UPDATE #work SET Mode='U' WHERE ID IN (SELECT ID FROM DELETED)
INSERT INTO #work (ID, Modified0, Modified1, Mode)
SELECT ID, Modified0, Modified1, 'D'
FROM DELETED WHERE ID NOT IN (SELECT ID FROM INSERTED)
--Check Security or any other logic to add and remove from #work before processing
DELETE FROM #work WHERE ID=9 -- because you don't want anyone to edit this id?!?!
DELETE FROM #work WHERE Mode='D' -- because you don't want anyone to delete any records
SELECT 'EV'
SELECT * FROM #work
IF(EXISTS(SELECT TOP 1 * FROM #work WHERE Mode='I'))
BEGIN
SELECT 'I0.0'
INSERT INTO dbo.TESTTEST (ID, Modified0, Modified1)
SELECT ID, Modified0, Modified1
FROM #work
WHERE Mode='I'
SELECT 'Cool stuff would happen here if you had FOR INSERT or AFTER INSERT triggers.'
SELECT 'I0.1'
END
IF(EXISTS(SELECT TOP 1 * FROM #work WHERE Mode='D'))
BEGIN
SELECT 'D0.0'
DELETE FROM TESTTEST WHERE ID IN (SELECT ID FROM #work WHERE Mode='D')
SELECT 'Cool stuff would happen here if you had FOR DELETE or AFTER DELETE triggers.'
SELECT 'D0.1'
END
IF(EXISTS(SELECT TOP 1 * FROM #work WHERE Mode='U'))
BEGIN
SELECT 'U0.0'
UPDATE t SET t.Modified0=e.Modified0, t.Modified1=e.Modified1
FROM dbo.TESTTEST t
INNER JOIN #work e ON e.ID = t.ID
WHERE e.Mode='U'
SELECT 'U0.1'
END
DROP TABLE #work
SELECT 'TT0.1'
SELECT * FROM TESTTEST
END
GO
CREATE TRIGGER [dbo].[tr_TESTTEST_1] ON [dbo].TESTTEST
FOR UPDATE
AS
BEGIN
SELECT 'FOR UPDATE'
SELECT 'TT1.0'
SELECT * FROM TESTTEST
SELECT 'I1'
SELECT * FROM INSERTED
SELECT 'D1'
SELECT * FROM DELETED
SELECT 'TT1.1'
SELECT * FROM TESTTEST
END
GO
CREATE TRIGGER [dbo].[tr_TESTTEST_2] ON [dbo].TESTTEST
AFTER UPDATE
AS
BEGIN
SELECT 'AFTER UPDATE'
SELECT 'TT2.0'
SELECT * FROM TESTTEST
SELECT 'I2'
SELECT * FROM INSERTED
SELECT 'D2'
SELECT * FROM DELETED
SELECT 'TT2.1'
SELECT * FROM TESTTEST
END
GO
SELECT 'Start'
INSERT INTO TESTTEST (ID, Modified0) VALUES (9, GETDATE())-- not going to insert
SELECT 'RESTART'
INSERT INTO TESTTEST (ID, Modified0) VALUES (10, GETDATE())--going to insert
SELECT 'RESTART'
UPDATE TESTTEST SET Modified1=GETDATE() WHERE ID=10-- gointo to update
SELECT 'RESTART'
DELETE FROM TESTTEST WHERE ID=10-- not going to DELETE
SELECT 'FINISHED'
SELECT * FROM TESTTEST
DROP TABLE TESTTEST
First off, your trigger as you already see is going to update every record in the table. There is no filtering done to accomplish jus the rows changed.
Secondly, you're assuming that only one row changes in the batch which is incorrect as multiple rows could change.
The way to do this properly is to use the virtual inserted and deleted tables: http://msdn.microsoft.com/en-us/library/ms191300.aspx
Trigger
special kind of stored procedure
automatically execured/fired when some event Insert/Update/Delete Occures
use when we want some event to happen automatically on certain desirable scenarios
triggers makes use of 2 tables inserted/deleted table in ssms(memory)
ONLY availabe in context of trigger(CANNOT ACCESS Outside the Trigger
when we insert/delete using trigger, a copy of row is maintained in the inserted/deleted table
inserted table - contains updated data |
deleted table - contains old data
Trigger to Update "ModifiedOn" Date Automatically when record in table is modified(UPDATED)
CREATE TRIGGER [dbo].[Trg_TableName_UpdateModifiedOn]
ON [dbo].[TableName]
AFTER UPDATE
AS
BEGIN
UPDATE [dbo].[TableName]
SET [ModifiedOn] = GetDate()
FROM [inserted]
WHERE [inserted].[PrimaryKey] = [dbo].[TableName].[PrimaryKey];
END;
CREATE TRIGGER [dbo].[after_update] ON [dbo].[MYTABLE]
AFTER UPDATE
AS
BEGIN
DECLARE #ID INT
SELECT #ID = D.ID
FROM inserted D
UPDATE MYTABLE
SET mytable.CHANGED_ON = GETDATE()
,CHANGED_BY = USER_NAME(USER_ID())
WHERE ID = #ID
END
I have this trigger I want to make it allow multiple row updates, currently it handling only single row update.when i update record, it says sub query returning more then 1 value..
GO
ALTER TRIGGER [dbo].[OnReceiptUpdate]
ON [dbo].[paymentReceipt]
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON;
Declare #correctdate VARCHAR(19);
Declare #receiptNo VARCHAR(50);
DECLARE #customerID NCHAR(50)
SET #customerID= (SELECT customerID FROM inserted)
set #correctdate = (SELECT CONVERT(VARCHAR(19),paymentDate,103) FROM inserted)
set #receiptNo = (SELECT receiptNo FROM inserted)
BEGIN
UPDATE Paymentreceipt
SET paymentDate = #correctdate
WHERE customerID = #customerID and receiptNo=#receiptNo
END
END
Update p
Set p.paymentDate = CONVERT(VARCHAR(19),i.paymentDate,103)
From Paymentreceipt p
inner join inserted i
On p.customerID = i.customerID and p.receiptNo = i.receiptNo
should do it I think.
PS why is p.paymentdate a string? That's asking for it.
The easiest way is to use an update statement such as the one below in the Trigger.
UPDATE Paymentreceipt
SET paymentDate = CONVERT(VARCHAR(19),paymentDate,103)
FROM inserted
WHERE Inserted.receiptNo = Paymentreceipt.receiptNo
AND Inserted.customerID = Paymentreceipt.customerID
Note I don't have SQL server in front of me so the syntax might not be 100% correct but that gives you the general idea.
In general I try and avoid triggers but if your really need the trigger then use it but it may be possible to address this issue through the use of a stored procedure.
I'd like to record the insert date and an update date on a table.
What is the best way to do this in SQL Server 2008?
For Insert Date you can use the following trigger:
CREATE TRIGGER INSERT_DATE ON TABLE1
FOR INSERT
AS
BEGIN
SET NOCOUNT ON
UPDATE TABLE1
SET CreatedOn = GETDATE()
FROM TABLE1 A
INNER JOIN Inserted INS ON (INS.Id = A.Id)
SET NOCOUNT OFF
END
and for Update you can use the following trigger:
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
For the insert date column, you can set the column default to GETDATE() (or GETUTCDATE()).
For the update date, you would need to use a trigger to set the column to the current date whenever there's an update.