SQL Server after update trigger - sql
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
Related
Alternative to Iteration for INSERT SELECT UPDATE in a sequence
I have a table with around 17k unique rows for which I need to run these set of statements in sequence INSERT INTO TABLE1 using MASTERTABLE data (MASTERTABLE have 6 column) SELECT value of column ID (Primary Key) of newly inserted row from TABLE1 Update that ID value in TABLE2 using a Stored Procedure I have tried: while loop: took around 3 hours to complete the execution cursor: cancelled the query after executing it overnight In my understanding I can not use JOIN as I need to execute the statements in a sequence
The questions is not detailed enough. The general idea I would like to use something like this -- create a output table to hold new id, and key columns to join later DECLARE #OutputTbl TABLE (ID INT, key_Columns in MASTERTABLE) INSERT INTO TABLE1 OUTPUT INSERTED.ID, MASTERTABLE.key_columns INTO #OutputTbl SELECT * FROM MASTERTABLE UPDATE T2 SET ID = o.ID FROM TABLE2 t2 INNER JOIN OutputTbl o ON t2.key_column = o.key_column
Maybe you can consider a TRIGGER on TABLE1 from which to call the stored procedure on TABLE2, and then you can call your INSERT as you wish/need.. one by one or in blocks.. DROP TRIGGER TR_UPD_TABLE2 GO CREATE TRIGGER TR_UPD_TABLE2 ON TABLE1 AFTER INSERT AS BEGIN SET NOCOUNT ON DECLARE #columnID INT = NULL IF (SELECT COUNT(*) FROM INSERTED)=1 BEGIN -- SINGLE INSERT SET #columnID = (SELECT columnID FROM INSERTED) EXEC TableTwoUpdateProcedure #columnID END ELSE BEGIN -- MASSIVE INSERT (IF NEEDED) SET #columnID = 0 WHILE #columnID IS NOT NULL BEGIN SET #columnID = (SELECT MIN(columnID) FROM INSERTED WHERE columnID > #columnID) IF #columnID IS NOT NULL BEGIN EXEC TableTwoUpdateProcedure #columnID END END END END
Error on create trigger: Column name or number of supplied values does not match table definition
I have been doing this trigger to do a merge when inserted on the same table but apparently I keep getting this error : Column name or number of supplied values does not match table definition. My trigger: create trigger [dbo].[Updated_table1] on [dbo].[table1] instead of INSERT AS BEGIN set nocount on; IF(SELECT COUNT(*) FROM INSERTED) = 0 RETURN; merge into [dbo].[table1] with (HOLDLOCK) as d using INSERTED as i on (i.[primarykey] = d.[primarykey]) when not matched then insert values ('...','...'); END Any ideas?
Explicitly mention the Target and Source column names in your insert statement.... Something like this ........ create trigger [dbo].[Updated_table1] on [dbo].[table1] instead of INSERT AS BEGIN set nocount on; IF(SELECT COUNT(*) FROM INSERTED) = 0 RETURN; merge into [dbo].[table1] with (HOLDLOCK) as d using INSERTED as i on (i.[primarykey] = d.[primarykey]) when not matched then insert (d.Column1,d.Column2,d.Column3,d.Column4,...) values (i.Column1,i.Column2,i.Column3,i.Column4,...) END
ALTER TRIGGER tr_Emp_forInsert On Emp For Insert AS BEGIN Declare #Id int select #Id = ID from inserted insert into EmpAudit values('New employee with ID = ' + cast (#Id as nvarchar(5)) + ' is added at ' + cast (Getdate () as nvarchar(20))) end Column name or number of supplied values does not match table definition.
How can I update the data row by row by the loop after insert those records into another table in mssql?
Does anyone know how can I update the data row by row by the loop after insert those records into another table in mssql? Example: I have the following table (tableA) ID Name is_Feeded 1 Alvin 0 2 Ben 0 3 Lee 1 4 David 0 I want to insert those table from tableA to tableB then update the column is_Feeded to 1 in tableA through a loop? Anyone know how can I do it in mssql?
Assuming SQL Server 2005 or higher, you can do this in a single statement. UPDATE A OUTPUT inserted.ID, inserted.Name INTO dbo.TableB (ID, Name) SET A.is_Feeded = 1 -- is fed? FROM dbo.tableA A WHERE A.is_Feeded = 0 ; A trigger is also possible, but I don't recommend using one if you can avoid it. If you must to use a trigger (such as perhaps a case where you can't control updates to tableA) then: CREATE TRIGGER TableA_U ON dbo.TableA FOR UPDATE AS INSERT dbo.tableB (ID, Name) SELECT I.ID, I.Name FROM inserted I ; To me it is more natural to insert to tableB based on an update to tableA than to update tableA in response to an insert to tableB.
I would write a trigger for tableB. After you insert a row there, the trigger can update the specific value in tableA
First copy data from tableA to tableB INSERT INTO tableB SELECT Name, id FROM tableA; then set is feeded: UPDATE tableA SET is_feeded = true and finally you should do this in one transaction (syntax depends on your DB system, e.g. MySQL: http://dev.mysql.com/doc/refman/5.0/en/commit.html)
you should directly add update field is_feeded when inserting data to TABLEB.
CREATE PROCEDURE xxxxx AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON ; DECLARE #iOK INT ; SET #iOK = 0 ; BEGIN TRY BEGIN TRANSACTION -- Start the transaction --start Inserting -- INSERT INTO tableB SELECT Name , id FROM tableA ; UPDATE tableA SET is_feeded = true -- If we reach here, success! COMMIT SET #iOK = 1 ; END TRY BEGIN CATCH -- Whoops, there was an error IF ##TRANCOUNT > 0 ROLLBACK -- Raise an error with the details of the exception DECLARE #ErrMsg NVARCHAR(4000) , #ErrSeverity INT SELECT #ErrMsg = ERROR_MESSAGE() , #ErrSeverity = ERROR_SEVERITY() RAISERROR(#ErrMsg, #ErrSeverity, 1) END CATCH SELECT #iOK ; END
Update trigger insert Null
I am trying to create trigger on SQL Server 2008. I want that if i update field in tabele log that the new value update field in another table Doc. This is the code for trigger: Create TRIGGER dbo.DocSt ON dbo.log AFTER UPDATE IF (SELECT COUNT(*) FROM inserted) > 0 BEGIN IF (SELECT COUNT(*) FROM deleted) > 0 BEGIN UPDATE [dbo].[Doc] SET [ID_user] = (select ID_user from inserted) WHERE IDN= (select id_doc from inserted) END END When I update field in table log triger update table Doc but it insert NULL. What i am doing wrong? Thanks!
This code won't ever work - what happens in your UPDATE statement updates 10 rows?? What does this select give you: SET [ID_user] = (select ID_user from inserted) You're trying to set a single value to a whole return set from a SELECT statement - that won't work, obviously. You need to create an UPDATE statement that joins with the Inserted pseudo-table: CREATE TRIGGER dbo.DocSt ON dbo.log AFTER UPDATE UPDATE [dbo].[Doc] FROM Inserted i SET [ID_user] = i.ID_User WHERE IDN = i.id_doc That way, for each entry in Inserted, you're joining your table dbo.Doc to it and update the ID_user column.
Check if a row exists, otherwise insert
I need to write a T-SQL stored procedure that updates a row in a table. If the row doesn't exist, insert it. All this steps wrapped by a transaction. This is for a booking system, so it must be atomic and reliable. It must return true if the transaction was committed and the flight booked. I'm sure on how to use ##rowcount. This is what I've written until now. Am I on the right road? -- BEGIN TRANSACTION (HOW TO DO?) UPDATE Bookings SET TicketsBooked = TicketsBooked + #TicketsToBook WHERE FlightId = #Id AND TicketsMax < (TicketsBooked + #TicketsToBook) -- Here I need to insert only if the row doesn't exists. -- If the row exists but the condition TicketsMax is violated, I must not insert -- the row and return FALSE IF ##ROWCOUNT = 0 BEGIN INSERT INTO Bookings ... (omitted) END -- END TRANSACTION (HOW TO DO?) -- Return TRUE (How to do?)
I assume a single row for each flight? If so: IF EXISTS (SELECT * FROM Bookings WHERE FLightID = #Id) BEGIN --UPDATE HERE END ELSE BEGIN -- INSERT HERE END I assume what I said, as your way of doing things can overbook a flight, as it will insert a new row when there are 10 tickets max and you are booking 20.
Take a look at MERGE command. You can do UPDATE, INSERT & DELETE in one statement. Here is a working implementation on using MERGE - It checks whether flight is full before doing an update, else does an insert. if exists(select 1 from INFORMATION_SCHEMA.TABLES T where T.TABLE_NAME = 'Bookings') begin drop table Bookings end GO create table Bookings( FlightID int identity(1, 1) primary key, TicketsMax int not null, TicketsBooked int not null ) GO insert Bookings(TicketsMax, TicketsBooked) select 1, 0 insert Bookings(TicketsMax, TicketsBooked) select 2, 2 insert Bookings(TicketsMax, TicketsBooked) select 3, 1 GO select * from Bookings And then ... declare #FlightID int = 1 declare #TicketsToBook int = 2 --; This should add a new record merge Bookings as T using (select #FlightID as FlightID, #TicketsToBook as TicketsToBook) as S on T.FlightID = S.FlightID and T.TicketsMax > (T.TicketsBooked + S.TicketsToBook) when matched then update set T.TicketsBooked = T.TicketsBooked + S.TicketsToBook when not matched then insert (TicketsMax, TicketsBooked) values(S.TicketsToBook, S.TicketsToBook); select * from Bookings
Pass updlock, rowlock, holdlock hints when testing for existence of the row. begin tran /* default read committed isolation level is fine */ if not exists (select * from Table with (updlock, rowlock, holdlock) where ...) /* insert */ else /* update */ commit /* locks are released here */ The updlock hint forces the query to take an update lock on the row if it already exists, preventing other transactions from modifying it until you commit or roll back. The holdlock hint forces the query to take a range lock, preventing other transactions from adding a row matching your filter criteria until you commit or roll back. The rowlock hint forces lock granularity to row level instead of the default page level, so your transaction won't block other transactions trying to update unrelated rows in the same page (but be aware of the trade-off between reduced contention and the increase in locking overhead - you should avoid taking large numbers of row-level locks in a single transaction). See http://msdn.microsoft.com/en-us/library/ms187373.aspx for more information. Note that locks are taken as the statements which take them are executed - invoking begin tran doesn't give you immunity against another transaction pinching locks on something before you get to it. You should try and factor your SQL to hold locks for the shortest possible time by committing the transaction as soon as possible (acquire late, release early). Note that row-level locks may be less effective if your PK is a bigint, as the internal hashing on SQL Server is degenerate for 64-bit values (different key values may hash to the same lock id).
i'm writing my solution. my method doesn't stand 'if' or 'merge'. my method is easy. INSERT INTO TableName (col1,col2) SELECT #par1, #par2 WHERE NOT EXISTS (SELECT col1,col2 FROM TableName WHERE col1=#par1 AND col2=#par2) For Example: INSERT INTO Members (username) SELECT 'Cem' WHERE NOT EXISTS (SELECT username FROM Members WHERE username='Cem') Explanation: (1) SELECT col1,col2 FROM TableName WHERE col1=#par1 AND col2=#par2 It selects from TableName searched values (2) SELECT #par1, #par2 WHERE NOT EXISTS It takes if not exists from (1) subquery (3) Inserts into TableName (2) step values
I finally was able to insert a row, on the condition that it didn't already exist, using the following model: INSERT INTO table ( column1, column2, column3 ) ( SELECT $column1, $column2, $column3 WHERE NOT EXISTS ( SELECT 1 FROM table WHERE column1 = $column1 AND column2 = $column2 AND column3 = $column3 ) ) which I found at: http://www.postgresql.org/message-id/87hdow4ld1.fsf#stark.xeocode.com
This is something I just recently had to do: set ANSI_NULLS ON set QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[cjso_UpdateCustomerLogin] ( #CustomerID AS INT, #UserName AS VARCHAR(25), #Password AS BINARY(16) ) AS BEGIN IF ISNULL((SELECT CustomerID FROM tblOnline_CustomerAccount WHERE CustomerID = #CustomerID), 0) = 0 BEGIN INSERT INTO [tblOnline_CustomerAccount] ( [CustomerID], [UserName], [Password], [LastLogin] ) VALUES ( /* CustomerID - int */ #CustomerID, /* UserName - varchar(25) */ #UserName, /* Password - binary(16) */ #Password, /* LastLogin - datetime */ NULL ) END ELSE BEGIN UPDATE [tblOnline_CustomerAccount] SET UserName = #UserName, Password = #Password WHERE CustomerID = #CustomerID END END
You could use the Merge Functionality to achieve. Otherwise you can do: declare #rowCount int select #rowCount=##RowCount if #rowCount=0 begin --insert....
INSERT INTO [DatabaseName1].dbo.[TableName1] SELECT * FROM [DatabaseName2].dbo.[TableName2] WHERE [YourPK] not in (select [YourPK] from [DatabaseName1].dbo.[TableName1])
Full solution is below (including cursor structure). Many thanks to Cassius Porcus for the begin trans ... commit code from posting above. declare #mystat6 bigint declare #mystat6p varchar(50) declare #mystat6b bigint DECLARE mycur1 CURSOR for select result1,picture,bittot from all_Tempnogos2results11 OPEN mycur1 FETCH NEXT FROM mycur1 INTO #mystat6, #mystat6p , #mystat6b WHILE ##Fetch_Status = 0 BEGIN begin tran /* default read committed isolation level is fine */ if not exists (select * from all_Tempnogos2results11_uniq with (updlock, rowlock, holdlock) where all_Tempnogos2results11_uniq.result1 = #mystat6 and all_Tempnogos2results11_uniq.bittot = #mystat6b ) insert all_Tempnogos2results11_uniq values (#mystat6 , #mystat6p , #mystat6b) --else -- /* update */ commit /* locks are released here */ FETCH NEXT FROM mycur1 INTO #mystat6 , #mystat6p , #mystat6b END CLOSE mycur1 DEALLOCATE mycur1 go
Simple way to copy data from T1 to T2 and avoid duplicate in T2 --Insert a new record INSERT INTO dbo.Table2(NoEtu, FirstName, LastName) SELECT t1.NoEtuDos, t1.FName, t1.LName FROM dbo.Table1 as t1 WHERE NOT EXISTS (SELECT (1) FROM dbo.Table2 AS t2 WHERE t1.FName = t2.FirstName AND t1.LName = t2.LastName AND t1.NoEtuDos = t2.NoEtu)
INSERT INTO table ( column1, column2, column3 ) SELECT $column1, $column2, $column3 EXCEPT SELECT column1, column2, column3 FROM table
The best approach to this problem is first making the database column UNIQUE ALTER TABLE table_name ADD UNIQUE KEY THEN INSERT IGNORE INTO table_name ,the value won't be inserted if it results in a duplicate key/already exists in the table.