Firebird - using before insert trigger for a master table to sum detail records - sql

I have two tables a master and detail. the detail table records are created automatically using a trigger after insert new master record.
But I need to create Before Insert or Update trigger (call it T1) for the master table to do some calculations based on fields from master record and sum from its detail records.
My problem to be able to do my calculations in T1 I need to insert details records first but of course the detail records has foreign key constraint to the master table ID which prevent this action so what do you think the best approach to achieve this task ?

I think the best approach is to use a stored procedure that does all the work...
something like this:
create procedure insert_record(id integer, ...);
as
begin
/* this inserts master and through triggers creates detail */
insert into master (id, ... )
values (:id, ...);
/* calculate values */
select sum(...) from detail
where id = :id
into :calculation;
/* usa calculated value to update master table */
update master
set calculated_value = :calculation
here id = :id;
end

Related

PostgreSQL Master Details Transaction Handling using Npgsql

Supposing I Have these tables:
CREATE TABLE master
master_id serial,
master_desc character varying
CREATE TABLE details
details_masterrefid int,
details_desc character varying
CONSTRAINT master_detail_fkey(details_masterrefid)
REFERENCES master(master_id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION
Then I have a code in C# which basically will insert the header first to the master table and then loop through the details and insert it in the details table.
By doing this, I have to insert to the master table first then commit the transaction, if its successful, get the ID and use it to insert in the details table. Now, the problem is, if something went wrong in the details and the insertion is not successful, I want to rollback the data inserted on the master table. But since its already committed, I cant roll it back. It should be all or nothing. Both the master table and details table.
The only solution I can think of is by allowing the foreign key field in the details to be nullable then if everything is successful, update the foreign key field to its respective value. Any suggestion on how do it better? I dont know if its an efficient solution.
You could use single statement CTE:
WITH ins1 AS (
INSERT INTO master (cols...)
VALUES (..) -- insert into master
RETURNING master_id -- get generated id
)
INSERT INTO details (...)
SELECT master_id, ... -- insert into child table
FROM ins1;
Rextester Demo
Another method is to wrap everything with single transaction:
BEGIN TRAN
INSERT INTO master ...
INSERT INTO child ...
COMMIT;
That way you will get all-or-nothing behaviour.

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

Using Trigger to insert record in another table, AND write back the ID into original record ..?

I have a simple trigger setup, which is used to insert records into a RentJournal table, whenever there is a record inserted in the UnitAGA table.
The RentJournal table has a primary key ID column named RentJournalID, which is auto incrementing. The UnitAGA table also has a nullable foreign key column named RentJournalID, which links each UnitAGA entry, to its corresponding entry in RentJournal table (which is inserted through the Trigger below).
Problem is that currently this Trigger is only inserting values into RentJournal table. But now I want to also fetch the ID assigned for each RentJournal entry through this Trigger, and write that into the corresponding UnitAGA record, whose insert actually triggered the Trigger in the first place. How do I do this ?
The Trigger code as of right now is this:
USE [RentDB]
GO
ALTER TRIGGER [RTS].[InsertRentJournalEntry]
ON [RTS].[UnitAGA]
AFTER INSERT
AS
BEGIN
INSERT INTO RTS.RentJournal
(UnitId, AdjustmentType, EffectiveDate, ReferenceFormNo)
SELECT
UnitId, 'AGA', EffectiveDate, ReferenceFormNo FROM inserted
END
Have a look at the INSERT logical table that is available in insert triggers:
DML triggers use the deleted and inserted logical (conceptual) tables. They are structurally similar to the table on which the trigger is defined, that is, the table on which the user action is tried. The deleted and inserted tables hold the old values or new values of the rows that may be changed by the user action. For example, to retrieve all values in the deleted table, use: SELECT * FROM deleted
http://technet.microsoft.com/en-us/library/ms189799.aspx
Then use ##IDENTITY to get the value of the identity column on your RentJournal table.
So you should be able to do something like:
update INSERTED set RentJournalID = ##IDENTITY

How to Create Trigger to Keep Track of Last Changed Data

CREATE TABLE Member
(
memberID - PK
memberName
dateRegistered - one time process
);
CREATE TABLE MemberLastChanged
(
memberID
memberName
dateEntered
);
If by any chance a user changes his member name, i need to keep track of the currently changed memberName in a history table.
For example, current info is:
memberID: 5534 memberName: james
User changes it to:
memberID: 5534 memberName:
mark
By now, "Member" will hold current values:
5534 and mark
AND
"MemberLastChanged" will hold:
5534 and james
How can i achieve this in t-sql using trigger?
CREATE TRIGGER TRG_Member_U ON Member FOR UPDATE
AS
SET NOCOUNT ON
INSERT MemberLastChanged (memberID, memberName)
SELECT
D.memberID, D.memberName
FROM
DELETED D JOIN INSERTED I ON D.memberID = I.memberID
WHERE
D.memberName <> I.memberName
GO
Also, add a default of GETDATE to dateRegistered so it's recorded automatically.
This also filters out dummy updates by comparing new and old values (INSERTED vs DELETED).
INSERTED and DELETED are special tables available only in trigger.
You create an UPDATE trigger - triggers have access to two logical tables that have an identical structure to the table they are defined on:
INSERTED, which is the new data to go into the table
DELETED, which is the old data the is in the table
See this MDSN article on using these logical tables.
With this data you can populate your history table.
CREATE TRIGGER trg_Member_MemberUpdate
ON dbo.Member AFTER UPDATE
AS
INSERT INTO dbo.MemberLastChanged(memberID, memberName)
SELECT d.MemberID, d.MemberName
FROM DELETED d
You want to have an AFTER UPDATE trigger on your users table - something like:
CREATE TRIGGER trg_MemberUpdated
ON dbo.Member AFTER UPDATE
AS BEGIN
IF UPDATE(memberName)
INSERT INTO
dbo.MemberLastChanged(memberID, memberName, dateEntered)
SELECT
d.MemberID, d.MemberName, GETDATE()
FROM
Deleted d
END
Basically, this trigger checks to see whether the memberName property was updated; if so, a row with the old values (which are available in the Deleted pseudo table inside the UPDATE trigger) is inserted into MemberLastChanged

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