SQL Server 2008 before inserting a trigger

I have the following query:

Insert into tblTest (ID, Name, Age) VALUES (1, 'TestName', 20);

In my trigger, I want to check - if the request identifier is 1, send another request:

Insert into tblTest (ID, Name, Age) VALUES (2, 'TestName', 21);

Otherwise, do nothing.

The problem is that I don’t know how to save the parameters as they are and just change the age, so basically I want to send a SAME request and change a certain parameter (in this case, its age parameter).

+3
source share
1 answer

The rows to be inserted can be found in the special table inserted. Here is an example:

if object_id('tblTest') is not null 
    drop table tblTest
create table tblTest (id int, name varchar(50), age int)
go
create trigger trg_tblTest_BeforeInsert
on tblTest
after insert
as begin
    insert  tblTest
    select  id + 1
    ,       name
    ,       age + 1
    from    inserted
    where   id = 1 -- Only for rows with id=1
end
go
insert tblTest (id, name, age) values (1, 'TestName', 20)
select * from dbo.tblTest

Fingerprints:

id  name      age
1   TestName  20
2   TestName  21
+3
source

All Articles