OVH Cloud OVH Cloud

TRigger

4 réponses
Avatar
C Beaugrand
Bonjour,

jai un trigger qui met a jour un champ dune table avec le
champ dune autre table quand jexecute sur un
enregistrement aucun pb par contre lorsque je fais un
update en masse il me met la meme valeur pour tout mes
enregistrements.
Voila le code

CREATE TRIGGER T_PRODUIT ON PRODUIT
FOR INSERT,UPDATE

as

DECLARE

@CODE_PRD char(10),
@des_w char(30)

BEGIN
select @code_prd=inserted.cod_prd from produit,inserted

update produit set chpar3=inserted.cod_prd
from produit,inserted
where produit.cod_prd=inserted.cod_prd

select @des_w=poste_de_travail.des_w
from postew_produit,inserted,poste_de_travail,produit
where poste_de_travail.code_w=postew_produit.code_w
and produit.cod_prd=inserted.cod_prd
and produit.cod_prd=postew_produit.cod_prd
and produit.code_ctg='0000000002'


update produit set chpar2=@des_w
from produit,inserted
where produit.cod_prd=inserted.cod_prd and
inserted.code_ctg='0000000002'


END
go

4 réponses

Avatar
Stéphane DOUSSIERE
Bonjour,

Sous SQL Server, le trigger s'exécute une seule fois pour l'instruction SQL
(update, insert, delete) et non pour chaque ligne. Pour faire une mise à
jour différente pour chaque ligne, vous devez faire un curseur sur la table
Inserted afin de traiter chaque ligne indépendamment les unes des autres.

--
Stéphane DOUSSIERE
"C Beaugrand" a écrit dans le message de
news:082301c371fa$599a1a20$
Bonjour,

jai un trigger qui met a jour un champ dune table avec le
champ dune autre table quand jexecute sur un
enregistrement aucun pb par contre lorsque je fais un
update en masse il me met la meme valeur pour tout mes
enregistrements.
Voila le code

CREATE TRIGGER T_PRODUIT ON PRODUIT
FOR INSERT,UPDATE

as

DECLARE

@CODE_PRD char(10),
@des_w char(30)

BEGIN
select @code_prd=inserted.cod_prd from produit,inserted

update produit set chpar3=inserted.cod_prd
from produit,inserted
where produit.cod_prd=inserted.cod_prd

select @des_w=poste_de_travail.des_w
from postew_produit,inserted,poste_de_travail,produit
where poste_de_travail.code_w=postew_produit.code_w
and produit.cod_prd=inserted.cod_prd
and produit.cod_prd=postew_produit.cod_prd
and produit.code_ctg='0000000002'


update produit set chpar2=@des_w
from produit,inserted
where produit.cod_prd=inserted.cod_prd and
inserted.code_ctg='0000000002'


END
go



Avatar
C beaugrand
Bonjour,
Auriez vous un exemple de code de creation dun curseur
dans un trigger.
Desole mais je connais bien le pl/sql mais sql server jai
un peu de mal


-----Message d'origine-----
Bonjour,

Sous SQL Server, le trigger s'exécute une seule fois pour


l'instruction SQL
(update, insert, delete) et non pour chaque ligne. Pour


faire une mise à
jour différente pour chaque ligne, vous devez faire un


curseur sur la table
Inserted afin de traiter chaque ligne indépendamment les


unes des autres.

--
Stéphane DOUSSIERE
"C Beaugrand" a écrit dans le


message de
news:082301c371fa$599a1a20$
Bonjour,

jai un trigger qui met a jour un champ dune table avec




le
champ dune autre table quand jexecute sur un
enregistrement aucun pb par contre lorsque je fais un
update en masse il me met la meme valeur pour tout mes
enregistrements.
Voila le code

CREATE TRIGGER T_PRODUIT ON PRODUIT
FOR INSERT,UPDATE

as

DECLARE

@CODE_PRD char(10),
@des_w char(30)

BEGIN
select @code_prd=inserted.cod_prd from produit,inserted

update produit set chpar3=inserted.cod_prd
from produit,inserted
where produit.cod_prd=inserted.cod_prd

select @des_w=poste_de_travail.des_w
from postew_produit,inserted,poste_de_travail,produit
where poste_de_travail.code_w=postew_produit.code_w
and produit.cod_prd=inserted.cod_prd
and produit.cod_prd=postew_produit.cod_prd
and produit.code_ctg='0000000002'


update produit set chpar2=@des_w
from produit,inserted
where produit.cod_prd=inserted.cod_prd and
inserted.code_ctg='0000000002'


END
go





.



Avatar
Med Bouchenafa[MVP]
USE PUBS
DECLARE @authorName varchar(30)
SELECT @authorName = au_lname FROM authors
SELECT @authorName

Comme tu le remarques sur l'exemple ci-dessus, La variable @authorName ne
contient au final que le dernier enregistrement alors que la table contient
en beaucoup.
Il faut donc faire attention à tes deux variables @CODE_PRD et @des_w qui
ne contiennent que le dernier enregistrement même si les tables Inserted et
Deleted contiennent tous les enregistrements modifiés

--
Salutations
Med Bouchenafa
TETRASET
75015 Paris




"C Beaugrand" wrote in message
news:082301c371fa$599a1a20$
Bonjour,

jai un trigger qui met a jour un champ dune table avec le
champ dune autre table quand jexecute sur un
enregistrement aucun pb par contre lorsque je fais un
update en masse il me met la meme valeur pour tout mes
enregistrements.
Voila le code

CREATE TRIGGER T_PRODUIT ON PRODUIT
FOR INSERT,UPDATE

as

DECLARE

@CODE_PRD char(10),
@des_w char(30)

BEGIN
select @code_prd=inserted.cod_prd from produit,inserted

update produit set chpar3=inserted.cod_prd
from produit,inserted
where produit.cod_prd=inserted.cod_prd

select @des_w=poste_de_travail.des_w
from postew_produit,inserted,poste_de_travail,produit
where poste_de_travail.code_w=postew_produit.code_w
and produit.cod_prd=inserted.cod_prd
and produit.cod_prd=postew_produit.cod_prd
and produit.code_ctg='0000000002'


update produit set chpar2=@des_w
from produit,inserted
where produit.cod_prd=inserted.cod_prd and
inserted.code_ctg='0000000002'


END
go



Avatar
Fred BROUARD
quelque chose comme :

CREATE TRIGGER T_PRODUIT ON PRODUIT
FOR INSERT, UPDATE

AS

UPDATE produit
SET P.chpar3 = I.cod_prd
FROM produit P
INNER JOIN inserted I
ON P.cod_prd = I.cod_prd
IF @@ERROR <> 0
GOTO LBL_ERROR


UPDATE produit
SET chpar2 = poste_de_travail.des_w
from inserted I
INNER JOIN postew_produit PP
ON P.cod_prd = I.cod_prd
INNER JOIN poste_de_travail PT
ON PT.code_w = PP.code_w
INNER JOIN produit P
ON PT.code_w = PP.code_w
WHERE I.code_ctg = '0000000002'
IF @@ERROR <> 0
GOTO LBL_ERROR


RETURN

LBL_ERROR:
ROLLBACK


A lire :
http://sqlpro.developpez.com/TransactSQL/SQL_MSTransactSQL.html
Paragraphe 4

A +


--
Frédéric BROUARD - expert SQL, spécialiste : SQL Server / Delphi / web
Livre SQL - col. Référence : http://sqlpro.developpez.com/bookSQL.html
Le site du SQL, pour débutants et pros : http://sqlpro.developpez.com
****************** mailto: ******************

C Beaugrand a écrit:
Bonjour,

jai un trigger qui met a jour un champ dune table avec le
champ dune autre table quand jexecute sur un
enregistrement aucun pb par contre lorsque je fais un
update en masse il me met la meme valeur pour tout mes
enregistrements.
Voila le code

CREATE TRIGGER T_PRODUIT ON PRODUIT
FOR INSERT,UPDATE

as

DECLARE

@CODE_PRD char(10),
@des_w char(30)

BEGIN
select @code_prd=inserted.cod_prd from produit,inserted

update produit set chpar3=inserted.cod_prd
from produit,inserted
where produit.cod_prd=inserted.cod_prd

select @des_w=poste_de_travail.des_w
from postew_produit,inserted,poste_de_travail,produit
where poste_de_travail.code_w=postew_produit.code_w
and produit.cod_prd=inserted.cod_prd
and produit.cod_prd=postew_produit.cod_prd
and produit.code_ctg='0000000002'


update produit set chpar2=@des_w
from produit,inserted
where produit.cod_prd=inserted.cod_prd and
inserted.code_ctg='0000000002'


END
go