Hello i created a SQL Trigger which copies the edited row and set a new ID. There is another table which is connected by the ids. its hard for me to explain i give you an example:
Table 1 with the Products:
ID BEZEICHNUNG PREIS
45 Coffee 5,60
46 Tea 2,20
Table 2 with the connection from Room to the Products:
ProductID RoomID
45 11
45 46
46 48
46 41
When i update the price of the first row from Table 1, the Trigger creates a new row with a new ID like this:
ID BEZEICHNUNG PREIS
45 Coffee 5,60
46 Tea 2,20
47 Coffee 4
My goal is to insert to the table 2 the new id with all the room ids which belongs to the old row.
It should look like this:
ProductID RoomID
45 11
45 46
46 48
46 41
47 11
47 46
My current trigger look like this:
ALTER TRIGGER [dbo].[DIENSTLEISTUNG_UPDATE]
ON [dbo].[DIENSTLEISTUNG]
INSTEAD OF UPDATE
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO [DIENSTLEISTUNG] (BEZEICHNUNG, MENGENEINHEIT,
PREIS, BESCHREIBUNG, VORLAUFZEIT,
AZ_MO, AZ_DI,AZ_MI,AZ_DO,AZ_FR,
AZ_SA,AZ_SO,DIENSTLEISTUNGSART_ID,
UPDATE_USER, UPDATE_DATE, RUESTZEIT,
PERMISSIONS, KONTRAKTPOSITION,ARTIKELNUMMER,
ANZAHL, BUCHUNGSHINWEIS, SONDERWUNSCH,FLAG)
SELECT BEZEICHNUNG, MENGENEINHEIT,
PREIS, BESCHREIBUNG, VORLAUFZEIT,
AZ_MO, AZ_DI,AZ_MI,AZ_DO,AZ_FR,
AZ_SA,AZ_SO,DIENSTLEISTUNGSART_ID,
UPDATE_USER,GETDATE(),RUESTZEIT,
PERMISSIONS, KONTRAKTPOSITION,ARTIKELNUMMER,
ANZAHL, BUCHUNGSHINWEIS, SONDERWUNSCH,
0
FROM INSERTED
UPDATE DIENSTLEISTUNG
SET FLAG = 1
FROM DIENSTLEISTUNG
INNER JOIN INSERTED
ON INSERTED.ID = DIENSTLEISTUNG.ID
SET NOCOUNT OFF;
END
In the trigger, after the insert add
This gets the ID of the newly inserted product
This gets the products linked to the old product from the deleted, and copies them