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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
create trigger TI_CommandeFournisseur
on CommandeFournisseurs
instead of Insert
as
Begin
Declare @max int
Declare @prefix varchar(10);
Declare @inserted_year varchar(2)
Declare @lastcommande_year varchar(2)
Declare @res varchar(11)
select @max=max(cast((substring(NumeroCommande,6,len(NumeroCommande)-5))as int)) from CommandeFournisseurs;
select @lastcommande_year=right(Year((select top 1 DateCommande from CommandeFournisseurs order by NumeroCommande desc)),2);
select @inserted_year=right(year(CURRENT_TIMESTAMP),2);
if((@inserted_year<>@lastcommande_year) OR (@max is null))
begin
select @max=0;
end
else
begin
select @max=@max+1;
end
select @prefix=
case
when @max<10 then '000'
when @max<100 then '00'
when @max<1000 then '0'
else ''
end
insert into CommandeFournisseurs with (TABLOCK)
(ID,IDFournisseur,IDEtablissementLivraison,NumeroDevisFournisseur,CodeBonLivraisonRecu,CodeFactureRecu,RegleParDemandeur,
IDPersonneDemandeur,NumeroCommande,DateCommande,IDClient,IDDevise,Commentaire,CommentaireReception,CodeEtat,AutreFournisseur,
DelaiFournisseur,ReglementFournisseur,FaxFournisseur,TelephoneFournisseur,ContactFournisseur,LivraisonAdr1,LivraisonAdr2,LivraisonAdr3)
select ID,IDFournisseur,IDEtablissementLivraison,NumeroDevisFournisseur,CodeBonLivraisonRecu,CodeFactureRecu,RegleParDemandeur,
IDPersonneDemandeur,@res,DateCommande,IDClient,IDDevise,Commentaire,CommentaireReception,CodeEtat,AutreFournisseur,
DelaiFournisseur,ReglementFournisseur,FaxFournisseur,TelephoneFournisseur,ContactFournisseur,LivraisonAdr1,LivraisonAdr2,LivraisonAdr3 from inserted
end |
Partager