sql server trigger referencing last row - sql

I want to write a trigger in SQL Server to insert 2 rows into table2 when a row is inserted into table1. I want to use a column value from table1.
So my trigger looks like this
create trigger triggername on table1
as
begin
insert into
insert into
end
How do I get the value of any column from the last inserted row (the row insertion which fires the trigger). I.e. the equivalent of 'referencing row' in oracle

Triggers in SQL Server fire per statement not per row. There are two pseudo tables inserted and deleted that you can use (for an insert trigger the only one of interest is inserted)
CREATE TRIGGER YourTrigger ON Table1
FOR INSERT
AS
INSERT INTO Table2
SELECT * from inserted /*This will contain multiple rows if it is a multi-row insert*/

hi
i have the solution myself. i missed the alias
select #patient_no=fldL1BasCode from inserted
should be
select #patient_no=i.fldL1BasCode from inserted i

Related

Compound Trigger is Triggering Only Once

I have an ORDS enabled schema which accepts bulk JSON records and splits them one by one, and inserts them one by one into UEM table.
I've tried to create a trigger which fetches the last inserted row's id and use that value to insert into another table. The problem is, the trigger below only fetches and inserts the last inserted row's id, and it only does 1 insert.
To be more specific:
ORDS gets a bulk JSON payload which consists of 4 records.
POST handler starts a Procedure which splits these 4 records by line break, and immediately inserts these to CLOB columns of UEM table as 4 separate rows by using "connect by level". There is also the ID column which is automatically created and incremented.
In the parallel I also would like to get the ID of these rows and use it in another table insert. I've created the compound trigger below, but this trigger only retrieves the ID of the last record, and inserts only one row.
Why do you think it behaves like this? In the end, the procedure "inserted" 4 records.
CREATE OR REPLACE TRIGGER TEST_TRIGGER5
FOR INSERT ON UEM
COMPOUND TRIGGER
lastid NUMBER;
AFTER STATEMENT IS
BEGIN
SELECT MAX(ID) INTO lastid FROM UEM;
INSERT INTO SPRINT1 (tenantid, usersessionid, newuser, totalerrorcount, userid) VALUES ('deneme', 'testsessionid', 'yes', lastid, 'asdasfqwqwe');
END AFTER STATEMENT;
END TEST_TRIGGER5;
inserts these to CLOB columns of UEM table as 4 separate rows by using "connect by level".
You have 1 INSERT statement that is inserting 4 rows.
In the parallel I also would like to get the ID of these rows and use it in another table insert. I've created the compound trigger below, but this trigger only retrieves the ID of the last record, and inserts only one row.
Why do you think it behaves like this? In the end, the procedure "inserted" 4 records.
It may have inserted 4 ROWS but there was only 1 STATEMENT and you are using an AFTER STATEMENT trigger. If you want it to run for each row then you need to use a row-level trigger.
CREATE OR REPLACE TRIGGER TEST_TRIGGER5
AFTER INSERT ON UEM
FOR EACH ROW
BEGIN
INSERT INTO SPRINT1 (tenantid, usersessionid, newuser, totalerrorcount, userid)
VALUES ('deneme', 'testsessionid', 'yes', :NEW.id, 'asdasfqwqwe');
END TEST_TRIGGER5;
/
db<>fiddle here
Why? Because it is a statement level trigger. If you wanted it to fire for each row, you'd - obviously - use a row level trigger which has the
for each row
clause.

Trigger: How does the inserted table work? How to access its rows?

I have the following table
Data --Table name
ID -- Identity column
PCode -- Postal Code
I created the following trigger:
CREATE TRIGGER Trig
ON Data
FOR INSERT
AS
BEGIN
Select * from inserted
END
And inserted the following values
INSERT INTO Data VALUES (125)
INSERT INTO Data VALUES (126)
INSERT INTO Data VALUES (127)
It shows this:
But I was expecting something like this:
After the 1st insertion, the trigger is executed -> one row is shown in the inserted table.
After the 2nd insertion, the trigger is executed -> two rows are shown in the inserted table.
After the 3rd insertion, the trigger is executed -> three rows are shown in the inserted table.
According to msdn.microsoft all the rows inserted are in this table.
How can I access the inserted table so that I can see all the expected rows and not separately?
You can not. From the Use the inserted and deleted Tables article on microsoft.com, you can read:
The inserted table stores copies of the affected rows during INSERT and UPDATE statements.
That means that the inserted table will only contain rows for the current INSERT or UPDATE statement.
If you do want to see all rows for several such INSERT or UPDATE statements, you will have to store these rows in a table you created yourself.
There are 2 table available in a trigger, the inserted and the deleted. Each update on table XXX is actually a delete row X from XXX then an insert of row X in table XXX. So the inserted inside the trigger is a copy of what got inserted. You can do a lot with a trigger, but triggers are dangerous.
For example, on a performance gig, I found a huge SP being run by a trigger, we dropped it and the database came back online. Or another example, if you do a trigger wrong to audit logins, you can down the server.
As TT mentioned, if you want to see all the inserted records then you need to change your Trigger to something like this:
CREATE TRIGGER Trig
ON Data
FOR INSERT
AS
BEGIN
Select * into "tablename"
from
(Select * from inserted) Ins
END

SQL Server triggers on certain column and value

I want to write a trigger when there is certain value inserted in certain column instead of whole table, for example I want my trigger to act when the value NewName is inserted in the column Names on students table.
CREATE TRIGGER newtrg
ON students
AFTER INSERT
AS BEGIN
PRINT 'Trigger fired!'
END
You have to understand how triggers work in SQL Server - they fire on a per statement level - not per row - so you cannot just check to see if the row being inserted contains NewName in the Names column - an INSERT statement could insert dozens of rows at once, and your trigger will only fire once for the whole statement.
Therefore, you cannot prevent a single row from being inserted - if your INSERT statement inserted 57 rows, and one of them contains NewName in the Names column, you can either just change that value on that particular row, or you can rollback the transaction and stop the whole INSERT process - NONE of the 57 rows will be inserted.
So what do you really want to do, in the end? If your INSERT of n rows contains a single row with a value of NewNames in the Names column - do you want to stop the whole INSERT process and rollback the transaction? In that case - use something like this:
CREATE TRIGGER newtrg
ON students
AFTER INSERT
AS
IF EXISTS (SELECT * FROM Inserted WHERE Names = 'NewName')
-- any of of the rows being inserted contains `NewName`
-- in this case, ABORT the whole INSERT
ROLLBACK TRANSACTION
Or if you want to update those rows after they've been inserted to get a new value (instead of NewNames) - try something like this:
CREATE TRIGGER newtrg
ON students
AFTER INSERT
AS
UPDATE s
SET Names = 'SomeOtherValue'
FROM dbo.Students s
INNER JOIN Inserted i ON s.StudentID = i.StudentID -- use the primary key here
WHERE i.Names = 'NewName'

SQL Server INSERT trigger how to get data of current row

I've never created a trigger before and I'm trying to read online but am a little confused.
I want to create a trigger on a table that on insert, it will grab some data from different columns and insert it into a few different other tables.
I'm not sure how to write the T-SQL to get the data from the columns..
insert into [othetTable]
values ([col1 from row that was inserted], [col5 from row that was inserted])
What would the syntax be to get those values?
thanks
Use the inserted virtual table that is available to triggers. Note that there could be multiple rows in this table - your trigger could be processing multiple inserts at once.
Therefore, you need to use something like the following syntax:
insert into othertable
select col1, col5
from inserted
This will insert a row into othertable for each inserted row.

Sql Server trigger insert values from new row into another table

I have a site using the asp.net membership schema. I'd like to set up a trigger on the aspnet_users table that inserted the user_id and the user_name of the new row into another table.
How do I go about getting the values from the last insert?
I can select by the last date_created but that seems smelly. Is there a better way?
try this for sql server
CREATE TRIGGER yourNewTrigger ON yourSourcetable
FOR INSERT
AS
INSERT INTO yourDestinationTable
(col1, col2 , col3, user_id, user_name)
SELECT
'a' , default , null, user_id, user_name
FROM inserted
go
You use an insert trigger - inside the trigger, inserted row items will be exposed as a logical table INSERTED, which has the same column layout as the table the trigger is defined on.
Delete triggers have access to a similar logical table called DELETED.
Update triggers have access to both an INSERTED table that contains the updated values and a DELETED table that contains the values to be updated.
You can use OLDand NEW in the trigger to access those values which had changed in that trigger. Mysql Ref
In a SQL Server trigger you have available two psdeuotables called inserted and deleted. These contain the old and new values of the record.
So within the trigger (you can look up the create trigger parts easily) you would do something like this:
Insert table2 (user_id, user_name)
select user_id, user_name from inserted i
left join table2 t on i.user_id = t.userid
where t.user_id is null
When writing triggers remember they act once on the whole batch of information, they do not process row-by-row. So account for multiple row inserts in your code.
When you are in the context of a trigger you have access to the logical table INSERTED which contains all the rows that have just been inserted to the table. You can build your insert to the other table based on a select from Inserted.
Create
trigger `[dbo].[mytrigger]` on `[dbo].[Patients]` after update , insert as
begin
--Sql logic
print 'Hello world'
end