Trigger to insert values in table2, when values are inserted in table1 - sql

I want to write trigger, when values are inserted in table1, same values must get added in table2.

create trigger TestTrigger
on table1
after insert
as
insert into table2
select *
from inserted
go
The assumption here is that table2 and table1 have the same amount of columns with compatible field types (that is needed when you do a "select star" with implicit fields).
If you want to put inserted values that were inserted into table1, you can utilize the inserted table (and deleted as well for DELETE and UPDATE statements).

Related

Could insert statements match data from another table

I am trying to do an insert on a table from another table with matched values and IDs.
lets say on table1 there are values of names and IDS. Like John and 55.
I am trying to only insert the 55 into table2 since John is already on table2 but just missing his ID.
I know I can do update statement to update the value for John to 55, but my tables have over 3000 values, and it will be hard to do one at a time:
Anyway I can write a query to enter a value into the other table as long as the names match together?
what I tried so far:
insert into desired_table (id,version,source_id,description,r_id)
SELECT HI_SEQUENCE.nextval,'0', select min (id)
from table
where name in (select name from table2 where table2_name is not null),
table2_name,
table2.r_id from table2 where name is not null;
Issue with this statement is it inserts multiple values, but it only inserts it into where the min ID is.
Anyway I can adjust this and have it pull more than one ID?
Use Merge statement (https://learn.microsoft.com/en-us/sql/t-sql/statements/merge-transact-sql?view=sql-server-ver15)
Merge into Table1 d
Using Table 2 s
on d.name=s.name
when matching then update
age=s.age
when not matching then insert
(col1, col2)
values (s.col1, s.col2);
You might want a trigger to automate the above task.
Create Trigger sample after insert on
Table1 for each row
Begin
Update table2 set table2.age = :NEW.AGE where
table2.id=:NEW.Id
END;
Got this working by generating insert statements and running them with insert all

An insert query inserts only one row

When I try to use this :
insert into mytable1
(whatever)
values
(select whatever from mytable2 where accomplished=True);
Only one row gets inserted though there should be 3.
Why is this ?
Depending on the implementation, the statement you have above inserts into one row as many values as you have fields in the (whatever) part.
You should try this instead:
INSERT INTO table2
SELECT * FROM table1;
Check this site out for more information:
http://www.w3schools.com/sql/sql_insert_into_select.asp

Apache Derby Insert Trigger - Retrieving inserted record

I have a insert trigger for my Derby Database. The point of this trigger is to take everything that is inserted into table_1 and copy that information into table_2. My code works and adds a row to table_2 when I input static numbers, but I cannot figure a way to simply get the value of the row that was just inserted into table_1.
create trigger insert_new_row
after insert on table_1
referencing new as insertedrow
for each row
insert (a,b,c)
values(a,b,c);
In the values part I need the values that were just inserted into table_1 (newsest values). I have also tried- (insertedrow.a) (:insertedrow.a) (new.a) (select a from table_1) and (select a from table_1 where a = insertedrow.a) but none of those work.
Example- if the insert statement reads-
insert into table_1 (a,b,c)
values(1,2,3);
I need to the trigger to insert a row into table_2 with the same values (1,2,3). The collumn names are the same so a trigger of
create trigger insert_new_row
after insert on table_1
referencing new as insertedrow
for each row
insert (a,b,c)
values(1,2,3);
will work but is not practical since I need this to hapen behind the scene and need the values to change with the origional insert statement. Any ideas?
You are missing an into clause for the insert.
The following works for me:
create trigger insert_new_row
after insert on table_1
referencing new as insertedrow
for each row
insert into table_2 (a,b,c)
values( insertedrow.a, insertedrow.b, insertedrow.c);

Triggers in SQL Server 2008

I have created triggers for INSERT and UPDATE separately. The trigger is going to insert a row in Schema2 when an insert is made in Schema1. Tables:
Schema1.Temp1
Schema2.Temp2
The trigger creation is successful.
But when I am inserting data in Temp1, it is giving me error for Temp2 -- duplicate key. Temp2 has constraints for two other tables. What can be causing this, and how can it be resolved?
When your trigger is called try to write on Table2 (as you've told).
Peraphs you haven't written INSERT query using a condition on existence of your Temp1 row in Temp2.
Your query must be of this type:
INSERT INTO Table2 (field list)
SELECT field list
FROM inserted
WHERE NOT EXISTS(SELECT 'key' in Table2 t2 where t2.id = inserted.field_of_key)
In this way you prevent duplicate key, so if you want to update your table2 in insert too, you can write an UPDATE stament when that key is already existing.
Tell me if it's ok

sql server trigger referencing last row

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