cs_Aldo38
Messages postés4Date d'inscriptionmercredi 24 juin 2009StatutMembreDernière intervention22 août 2009
-
2 juil. 2009 à 23:13
cs_Aldo38
Messages postés4Date d'inscriptionmercredi 24 juin 2009StatutMembreDernière intervention22 août 2009
-
7 juil. 2009 à 07:12
Bonjour,
Débutant un peu par force dans sql (sql erveur 2005), je me suis attaqué à la génération d'un trigger qui doit réaliser la mise à jour d'une colonne si cette dernière est NULL, en recherchant sa valeur dans une autre table (avec des conditions de validité).
Bien que l'excution du trigger se passe bien !!!, la colonne n'est jamais mis à jour. J'ai réalisé plusieurs variations de code, mais force est de reconnaître que je n'arrive pas au résultat escompté.
Je me permets de vous envoyer le code afin que quelqu'un puisse me dire où est l'erreur (ou l'oubli).
Par avance merci de votre aide.
Voici le code :
CREATE TRIGGER TR_TDR_TDSYN_UpdLABS_sql
ON LABS
FOR INSERT,UPDATE
as
begin
declare
@ActRef varchar(10),
@ActId int,
@ActNature tinyint,
@ActDate datetime
select @ActRef =Inserted.LABS.ACTREF,
@ActId = Inserted.LABS.ACTID,
@ActNature = Inserted.LABS.ACTNATURE,
@ActDate = Inserted.LABS.ACTDATE FROM Inserted.LABS
IF (@ActId is NULL) BEGIN
IF (@ActNature=0) BEGIN
select DICT_LABS.ACTID into Inserted.LABS.ACTID
from DICT_LABS
WHERE DICT_LABS.ACTCODE = @ActRef AND DICT_LABS.APPLICDATE IS NOT NULL
AND CONVERT(datetime,DICT_LABS.APPLICDATE,103)=(select MAX(DICT_LABS.APPLICDATE) FROM DICT_LABS
WHERE DICT_LABS.ACTCODE = @ActRef AND (DICT_LABS.EXPIRADATE IS NULL OR DICT_LABS.EXPIRADATE > CONVERT(datetime, @ActDate,103))
AND DICT_LABS.APPLICDATE < CONVERT(datetime, @ActDate,103))
end
end
END
nivsql
Messages postés159Date d'inscriptionlundi 22 juin 2009StatutMembreDernière intervention14 décembre 20101 3 juil. 2009 à 00:22
je le remet un peu en forme pour qu'on puisse t'aider : (et j'ai déplacer le premier begin)
CREATE TRIGGER TR_TDR_TDSYN_UpdLABS_sql
ON LABS
FOR INSERT,UPDATE as
declare @ActRef varchar(10),
@ActId int,
@ActNature tinyint,
@ActDate datetime
begin
select @ActRef =Inserted.LABS.ACTREF, @ActId = Inserted.LABS.ACTID, @ActNature = Inserted.LABS.ACTNATURE, @ActDate = Inserted.LABS.ACTDATE
FROM Inserted.LABS
IF (@ActId is NULL) BEGIN IF (@ActNature=0)
BEGIN
select DICT_LABS.ACTID into Inserted.LABS.ACTID
from DICT_LABS
WHERE DICT_LABS.ACTCODE = @ActRef
AND DICT_LABS.APPLICDATE IS NOT NULL
AND CONVERT(datetime,DICT_LABS.APPLICDATE,103)=(
select MAX(DICT_LABS.APPLICDATE)
FROM DICT_LABS
WHERE DICT_LABS.ACTCODE = @ActRef
AND (DICT_LABS.EXPIRADATE IS NULL OR DICT_LABS.EXPIRADATE > CONVERT(datetime, @ActDate,103))
AND DICT_LABS.APPLICDATE < CONVERT(datetime, @ActDate,103))
end
end
END
nivsql
Messages postés159Date d'inscriptionlundi 22 juin 2009StatutMembreDernière intervention14 décembre 20101 3 juil. 2009 à 15:33
Je pense avoir compris ce qui se passe.
En SQL Server tous les Trigger DML qu'ils soient déclaré en FOR ou AFTER sont des TRIGGER de type AFTER (il n'existe pas de trigger de type BEFOR comme en Oracle qui s'execute AVANT l'ordre lui meme).
Par ailleur inserted est une table, par conséquent : select DICT_LABS.ACTID into Inserted.LABS.ACTID me semble une abhération.
je modifirais la chose comme suit (je part du principe que ACTREF, ACTDATE est la clé primaire de ta table):
CREATE TRIGGER TR_TDR_TDSYN_UpdLABS_sql
ON LABS
FOR INSERT,UPDATE as
declare
@ActRef varchar(10),
@ActId int,
@ActNature tinyint,
@ActDate datetime
begin
select @ActRef =Inserted.LABS.ACTREF, @ActId = Inserted.LABS.ACTID, @ActNature = Inserted.LABS.ACTNATURE, @ActDate = Inserted.LABS.ACTDATE
FROM Inserted.LABS
IF (@ActId is NULL)
BEGIN
IF (@ActNature=0)
BEGIN
select DICT_LABS.ACTID into @ActId
from DICT_LABS
WHERE DICT_LABS.ACTCODE = @ActRef
AND DICT_LABS.APPLICDATE IS NOT NULL
AND CONVERT(datetime,DICT_LABS.APPLICDATE,103)=(
select MAX(DICT_LABS.APPLICDATE)
FROM DICT_LABS
WHERE DICT_LABS.ACTCODE = @ActRef
AND (DICT_LABS.EXPIRADATE IS NULL OR DICT_LABS.EXPIRADATE > CONVERT(datetime, @ActDate,103))
AND DICT_LABS.APPLICDATE < CONVERT(datetime, @ActDate,103)) UPDATE LABS SET ACTID @ActId where ACTREF @ActRef and ACTDATE = @ActDate
end
end
END
cs_Aldo38
Messages postés4Date d'inscriptionmercredi 24 juin 2009StatutMembreDernière intervention22 août 2009 4 juil. 2009 à 09:12
Bonjour,
Mes excuses pour le formattage de mon mail. J'espére que celui-ci ne posera pas le même problème. Merci, également pour le temps que vous avez consacré à mon problème.
J'ai pris en compte vos remarques. J'ai réalisé
l'exécution de ce trigger ce matin (je suis un
léve tôt) et j'ai encore un petit souci.
A l'exécution j'ai l'erreur suivante :
"Msg 102, Level 15, State 1, Procedure TR_TDR_TDSYN_UpdLABS_sql, Line 17 Incorrect syntax near '@ActdId'.".
Il s'agit de la ligne "select DICT_LABS.ACTID into @ActId from DICT_LABS ". J'ai contrôlé la syntaxe et j'avoue que je reste sec !.
J'ai remplacé "@ActId" par "Inserted.LABS.ACTID",
le trigger passe sans erreur, mais par contre j'ai une erreur à l'exécutiuon.
Je continue à investiguer le problème, mais si vous avez une idée elle sera la bien venue.
Dernière chose j'ai voulu désactiver le trigger sous sql server, mais les syntaxes que j'ai
utilisés ne fonctionnent pas. Si vou pouviez m'indiquer la syntaxe à utliser se serait sympa.
Je vous souhaite un bon week end
PS : Si ce mail est encore mal foramatté auriez-vous une idée sur l'origine du problème?
Vous n’avez pas trouvé la réponse que vous recherchez ?
cs_Aldo38
Messages postés4Date d'inscriptionmercredi 24 juin 2009StatutMembreDernière intervention22 août 2009 7 juil. 2009 à 07:12
Bonjour tous,
Malheureusement malgre beacoup d'essais de syntaxe, je n'arrive pas à faire fonctionner ce trigger correctement. Je pense qu'il ne doit pas manquer grand chose, mais je reste bloqué.
Alors si quelqu'un a une idée je suis preneur.
Par avance merci