Using original record ID within trigger process - sql-server

I have table question
id int
answers int
With every insert, delete call on this table I want to update answers column. I want to use trigger on this. I don't know how I can read question.id within trigger for insert, delete.
Any help appreciated.
Thanks.

In triggers that happen after insert, you can select from the inserted table to get the id of any inserted rows. Likewise for delete, you can select from the deleted table.

In BEFORE/AFTER INSERT trigger you can use OLD & NEW with "id" (OLD.id/NEW.id) and "answer" (OLD.answer/NEW.answer) columns. You will able to get OLD & NEW value of each column.
Similarly in AFTER DELETE trigger you can get OLD.id & OLD.answer, which in turn will give the deleted row's values

Related

Get trigger inserted row id's in stored procedure

I have a stored procedure which updates multiple rows in a column as mentioned below:
update tableA
set isactive = 0
where email = ''
TableA has update trigger to insert into tableALog if isactive is changed.
Now in my stored procedure after update statement is executed I need to get tableALog primary key values.
I have used following ways bu no luck.
##identity - failed because it returns last updated value. But I need list of primary key values inserted in log table
OUTPUT inserted. - cannot use this because it always works on current scope in stored procedure. But I need table values which are inserted from trigger.
Please let me know if you have any suggestions.
This started as a comment but it's getting too long, so...
Sharing data between stored procedures and the triggers they activate is tricky,
Since triggers can't take parameters not can they return values.
From my experience, The best way to achieve such a thing involve adding a column to the table the trigger is set on to identify the records the trigger is working on, and adding a table (could be a temporary table) for the trigger to output the data and the stored procedure to read from.

SQL Server - How to update only the inserted row of table through trigger?

The problem is I have inserted a new row in a table through procedure which have a AFTER INSERT trigger on it, what I want to do is to update only this new inserted row though trigger, is there any way to do this?
Yes.
Have you tried reading the documentation? I mean, at least once.
THe part where it tells you the trigger has a virtual table (inserted) with all the rows it processes (may be more than one actually, an insert can cover more than one row) and you can then use standard SQL on this (i.e. to find the updated rows in the real table and then update them)?
Your question is the textbook example of a simple standard tutorial trigger chapter 1.
A little google fu - "tsql trigger update inserted table".
Item 1: http://technet.microsoft.com/en-us/library/ms191300.aspx, named "Use the inserted and deleted Tables"
You could also just use search here instead of asking.
SQL Insert trigger to update INSERTED table values

SQL Server Trigger - Insert the last updated date into table linked with foreign key

I am using SQL Server 2008 R2.
I have two tables, ShoppingList and ShoppingListFood. ShoppingListFood is linked to ShoppingList via a foreign key on ShopListID. I wish to update the LastModifiedDate column in the ShoppingList table whenever there is any update / insert done at the ShoppingListFood table. Is it possible??
I googled and found that I need create trigger in stored procedure, but i know nothing about it, can anyone help??
Sure - no problem - try something like this:
CREATE TRIGGER trg_UpdateShoppingListFood
ON dbo.ShoppingListFood
AFTER INSERT, UPDATE
AS
UPDATE dbo.ShoppingList
SET LastModifiedDate = GETDATE()
WHERE ShopListID IN (SELECT DISTINCT ShopListID FROM Inserted)
Basically, this creates a trigger that will fire after INSERT and UPDATE on ShoppingListFood - it will update the LastModifiedDate to "right now" for all rows that have been inserted and/or updated, based on the pseudo table Inserted that's available inside a trigger.
This pseudo table contains all rows that have been freshly inserted (with all their columns), as well as the new values for all rows that have been updated (again: all columns).

Will a trigger be able to copy a primary key that's an identity id?

I want to do after I an INSERT in table X to copy that record into another History table immediately.
Now the table has the primary key as an Identity column, so the record won't have an primary key id until it is actually inserted.
My question is if I do a trigger on this record will I get the identity id for that record or will it still be blank?
Yes the identity is available in the trigger but make sure you get that id correctly.
##identity, SCOPE_IDENTITY etc are NOT what you want to do in a trigger!
SELECT #id = id FROM inserted
Is also a bad idea.
Always write your triggers to expect multiple changes being made simultaneously. The above approaches will all cause subtle but important errors when you insert more than one record into the table at a time.
The correct approach is to insert into your audit table FROM the inserted table
i.e.
INSERT INTO myAuditTable(Id, Datetime, user)
SELECT id, GETDATE(), USER_NAME())
FROM inserted
if you do the 'after insert' trigger, the record is already there with a value for the identity column.
Just make sure you declare the trigger as "AFTER" insert, not "FOR" or "INSTEAD OF" (guess you wouldn't use the last one... ;)
http://msdn.microsoft.com/en-us/library/ms189799.aspx

What should be the table contain updated row for update trigger

A while ago i read the article for Trigger in SQL Server, and it said that i can use the Logical Table "Updated" for updated rows... And i got error:
System.Data.SqlClient.SqlException: Invalid object name 'Updated'.
After a while of google, i found out some more post that said only 2 logical tables available are: Inserted and Deleted...
I'm confused... What should i use since my Trigger rely on the Updated table that contain the updated row, and use it to insert to another table or the same table with new PK...
Thank you very much
The two dummy tables are called Inserted (available in INSERT and UPDATE triggers) and Deleted (available in DELETE and UPDATE triggers).
There is no Updated dummy table in SQL Server triggers.
For an FOR UPDATE trigger, the Deleted table contains the old values, while the Inserted table contains the new ones.
Marc

Resources