[Debutant] Trigger d'insert
Bonjour a tous.
Voila je decouvre les trigger et je souhaiterai réaliser la chose suivante :
Tout d'abords je dispose de 3 tables
INTERFACE_RESEAU [AdresseMAC, AdresseIP, ID_Machine_FK]
MACHINE [ID_Machine, xxxx, xxxx, xxxx ...]
recep_ip_new_machine [ip, mac]
Mon but etant d'inserer pour chaque ligne de ma table recep_ip_new_machine les données dans ma table INTERFACE_RESEAU (donc au fur et a mesure que ma table recep_ip se remplit, executer le trigger d'insertion qui suit pour remplir la table INTERFACE)
Pour cela je suis obligé de faire un insert dans la table MACHINE afin d'incrémenter un ID_Machine pour le recuperer puis l'inserer avec ip et mac dans la table INTERFACE_RESEAU (car champ obligatoire).
Voila le trigger que j'ai réalisé en suivant les infos glanées sur le net mais ca ne fonctionne pas.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| USE [test_2_IDHI8430]
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER testouille
ON recep_ip_new_machine
FOR INSERT
AS
BEGIN
DECLARE @IP NCHAR(15)
DECLARE @MAC NCHAR(17)
DECLARE @max_id INT
SET @IP = (SELECT ip FROM recep_ip_new_machine)
SET @MAC = (SELECT mac FROM recep_ip_new_machine)
INSERT INTO MACHINE (NomDNS, NomNetBIOS, NomHote, Role, DateIntegration_FK,
DateDerniereMAJ_FK, ID_Infrastructure_FK, ID_ResponsableMachine_FK, ID_SystemeExploitation_FK)
VALUES ('temp', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
SET @max_id = (SELECT MAX(ID_MACHINE) FROM MACHINE)
INSERT INTO INTERFACE_RESEAU (AdresseMAC, AdresseIP, ID_Machine_FK)
values (@MAC, @IP, @max_id)
END |
Si vous pouviez m'aider ce serait formidable :)