Apache Derby Insert Trigger - Retrieving inserted record - sql

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);

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

Creating New GUID automatically while inserting a new row to an existing table Not Working

I have an existing table in MS SQL called myTab.
It has the following fields
empno(PK) nchar(10),
age int
Now, i want to add a myGUID column and fill it up with a GUID whenever i insert a new row as well as Updating existing rows.
So i added the statement
ALTER TABLE myTab ADD myGUID uniqueidentifier DEFAULT NewId() NOT NULL;
Updating existing rows works correctly.
But, when i try to insert values,
INSERT INTO myTab VALUES ( 1000, 22 );
It fails, and gives the following message
**Column name or number of supplied values does not match table definition.**
When i do
insert into sourav_test2 values (20055711,23,NEWID());
The above statement works.
I want a GUID to be filled without changing the insert statement. Is it possible via a Trigger or a Function?
Always list the columns you are inserting!
INSERT INTO myTab (empno, age)
VALUES ('1000', 22);
Also use correct types for the values. Unmentioned columns will be assigned their default values, or NULL if there is no explicit default.
Your table has three columns, so if you leave out the column list, then the insert expects three values. You can still set a default, if you want by using the DEFAULT keyword in the VALUES clause:
INSERT INTO myTab (empno, age, myGUID)
VALUES ('1000', 22, DEFAULT);
Sourav's question about triggers got me thinking, so I tried a little test. Why?
Imagine a scenario where an application has already been written with thousands of INSERT statements that leave off the column list. In this case, if you could write an INSTEAD OF INSERT trigger that provides the column list, you could hopefully save yourself from correcting thousands of INSERT statements due to a newly added column.
Off the top of my head, I admittedly did not know if this could work.
So I wrote this little test:
CREATE TABLE tt (ColA varchar(1));
INSERT INTO tt VALUES ('a');
ALTER TABLE tt
ADD ColB uniqueidentifier DEFAULT NEWID();
GO
CREATE TRIGGER tr_tt
ON tt
INSTEAD OF INSERT
AS
INSERT INTO tt (ColA)
SELECT ColA FROM inserted;
GO
INSERT INTO tt VALUES ('a');
SELECT * FROM tt;
DROP TABLE tt;
I also tried a variation of the TRIGGER with the following INSERT just to be thorough:
INSERT INTO tt (ColA, ColB)
SELECT ColA, NEWID() FROM inserted;
The result was the same in both cases: The same error as reported in the question. So to answer the question:
Can't we use a trigger here which can do it?
The answer is NO. Even if you put an INSTEAD OF INSERT TRIGGER on the table, the parser will still not let you write an INSERT..VALUES() statement unless the number and order of VALUES exactly matches the definition of the table. A TRIGGER cannot be used to get around it.
Sooner or later, lazy coding exacts its price.

Copy only certain columns from one table to other and insert defa

I couldn't get any answer googling this question.
I have 2 sql tables, `
Table1 (Id1, Name) and
Table2(Id2, Number)
I would like to copy only one column 'Id1' from table1 to 'Id2' column of table2, while inserting constant value to the Number column.
How can I modify my sql query to achieve this
INSERT INTO Table2
(Id2)
SELECT id1 FROM Table1;
Is this what you want?
INSERT INTO Table2 (Id2, Number)
SELECT id1, <constant value goes here>
FROM Table1;
Taking this a step further an option here would be add a trigger on Table1 to insert into Table2 after insert.
CREATE TRIGGER [dbo].[Table1_CopyId1ColToId2Table2] ON [dbo].[Table1]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO dbo.Table2(ID2, Number)
SELECT ID1, <const value> FROM inserted
END
You could also take advantage of after update of the row as well using a trigger. This guarantees even if the row is edited outside the app the trigger still fires.

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

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).

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