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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
| PROCEDURE SP_ItemNmc_Update
---- Paramètres ---
@e_Libelle varchar(100) ,
@e_Fk_ItemNmc bigint ,
@e_Fk_Nomenclature bigint ,
@e_NoItem varchar(255) , /* objet selectionné dejé transformé par code et non objet hierarchisé sans modif */
@e_NiveauItem bigint ,
@e_Id bigint ,
@e_Type varchar(1) ,
@e_Actif bit ,
@e_UserMaj bigint ,
@e_Timestamp varchar(100) ,
@s_erreur varchar(128) output
AS
begin
Create Table #TMPPermutation
(
Tmp_Id bigint,
Tmp_Libelle varchar(255),
Tmp_Prefixe varchar(255),
Tmp_Corps varchar(2),
Tmp_Suffixe varchar(255),
Tmp_NiveauItem bigint,
Tmp_FkItem bigint,
Tmp_Source int,
)
Begin try
---- Requête SQL ---
Declare @e_NoItemSource varchar(255)
Declare @e_NoItemDest varchar(255)
Declare @Source int
Declare @Dest int
Select @e_NoItemSource = @e_NoItem
if (@e_Type='H') -- Modification : 1 cran vers le Haut
Begin
Select @e_NoItemDest = Substring(@e_NoItem,0,len(@e_NoItem)-2)
+ right(replicate('0',2)
+ rtrim(ltrim(Convert(varchar, convert(int, Substring(@e_NoItem,len(@e_NoItem)-1,2))+1))),2)
Select @Source = 1
Select @Dest = 0
End
if (@e_Type='B') -- Modification : 1 cran vers le Bas
Begin
Select @e_NoItemDest = Substring(@e_NoItem,0,len(@e_NoItem)-2)
+ right(replicate('0',2)
+ rtrim(ltrim(Convert(varchar, convert(int, Substring(@e_NoItem,len(@e_NoItem)-1,2))-1))),2)
Select @Source = 0
Select @Dest = 1
End
if (@e_Type='B') or (@e_Type='H')
Begin
Insert into #TMPPermutation (Tmp_Id,
Tmp_Libelle,
Tmp_Prefixe,
Tmp_Corps,
Tmp_Suffixe,
Tmp_NiveauItem,
Tmp_FkItem,
Tmp_Source)
Select Id,
Libelle,
Substring(NoItem,0,len(@e_NoItem)-1),
Substring(NoItem,len(@e_NoItem)-1,2),
Substring(NoItem,len(@e_NoItem)+1,len(NoItem)-len(@e_NoItem)),
NiveauItem ,
Fk_ItemNmc,
@Source
From ItemNmc
Where Id <> @e_Id
And NoItem like (@e_NoItemSource+'%')
Insert into #TMPPermutation (Tmp_Id,
Tmp_Libelle,
Tmp_Prefixe,
Tmp_Corps,
Tmp_Suffixe,
Tmp_NiveauItem,
Tmp_FkItem,
Tmp_Source)
Select Id,
Libelle,
Substring(NoItem,0,len(@e_NoItem)-1),
Substring(NoItem,len(@e_NoItem)-1,2),
Substring(NoItem,len(@e_NoItem)+1,len(NoItem)-len(@e_NoItem)),
NiveauItem ,
Fk_ItemNmc,
@Dest
From ItemNmc
Where Id <> @e_Id
And NoItem like (@e_NoItemDest+'%')
Update #TMPPermutation Set Tmp_Corps = convert(Varchar, Convert(int, Tmp_Corps) -1) where Tmp_Source = 0
Update #TMPPermutation Set Tmp_Corps = convert(Varchar, Convert(int, Tmp_Corps) +1) where Tmp_Source = 1
Update #TMPPermutation Set Tmp_Corps = right(replicate('0',2)+ rtrim(ltrim(Tmp_Corps)),2)
End
if (@e_Type='D') -- Modification : 1 cran vers la Droite (de Frere en Fils)
Begin
Insert into #TMPPermutation (Tmp_Id,
Tmp_Libelle,
Tmp_Prefixe,
Tmp_Corps,
Tmp_Suffixe,
Tmp_NiveauItem,
Tmp_FkItem,
Tmp_Source)
Select I.Id,
I.Libelle,
Substring(NoItem,0,len(@e_NoItem)-1),
rtrim(ltrim(Convert(varchar,
convert(int, (Select count (id)
From ItemNmc I2
Where I2.Id <> @e_Id
And I2.NoItem like (@e_NoItemSource+'%')
And I2.NiveauItem = I.NiveauItem)
)
)
)),
'.01'+Substring(NoItem,len(@e_NoItem)+1,len(NoItem)-len(@e_NoItem)),
I.NiveauItem +1,
I.Fk_ItemNmc,
1
From ItemNmc I
Where I.Id <> @e_Id
And I.NoItem like (@e_NoItemSource+'%')
And I.NiveauItem >= @e_NiveauItem
End
if (@e_Type='G') -- Modification : 1 cran vers la Gauche (de Fils en Frere)
Begin
Insert into #TMPPermutation (Tmp_Id,
Tmp_Libelle,
Tmp_Prefixe,
Tmp_Corps,
Tmp_Suffixe,
Tmp_NiveauItem,
Tmp_FkItem,
Tmp_Source)
Select I.Id,
I.Libelle,
Substring(NoItem,0,len(@e_NoItem)-2), --Prefixe
'', --Corps
Substring(NoItem,len(@e_NoItem)+1,len(NoItem)-len(@e_NoItem)), --Suffixe
I.NiveauItem -1,
I.Fk_ItemNmc,
1
From ItemNmc I
Where I.Id <> @e_Id
And I.NoItem like (@e_NoItemSource+'%')
And I.NiveauItem >= @e_NiveauItem
End
/* Modification de l'élément consacré */
/* On modifie, on base, on respire... */
UPDATE ItemNmc
SET Libelle = Tmp_Libelle,
Fk_ItemNmc = Tmp_FkItem,
Fk_Nomenclature = @e_Fk_Nomenclature,
NoItem = Tmp_Prefixe + Tmp_Corps + Tmp_Suffixe,
NiveauItem = Tmp_NiveauItem,
Actif = @e_Actif,
UserMaj = @e_UserMaj
From #TMPPermutation
WHERE Id = Tmp_Id
Drop Table #TMPPermutation
if (@e_Type='G') or (@e_Type='D')
Begin
if (@e_Type='D')
Begin
/* Modif cran vers le haut "H" des elements en dessous */
Exec SP_ItemNmc_Update (@e_Libelle, @e_Fk_ItemNmc, @e_Fk_Nomenclature, @e_NoItem, @e_NiveauItem, @e_Id, 'H', @e_Actif, @e_UserMaj, @e_Timestamp @s_erreur)
End
if (@e_Type='G')
Begin
/* Modif cran vers le Bas "B" des elements en dessous */
Exec SP_ItemNmc_Update (@e_Libelle, @e_Fk_ItemNmc, @e_Fk_Nomenclature, @e_NoItem, @e_NiveauItem, @e_Id, 'B', @e_Actif, @e_UserMaj, @e_Timestamp @s_erreur)
End
End
UPDATE ItemNmc
SET LIbelle = @e_Libelle,
Fk_ItemNmc = @e_Fk_ItemNmc,
Fk_Nomenclature = @e_Fk_Nomenclature,
NoItem = @e_NoItem,
NiveauItem = @e_NiveauItem,
Actif = @e_Actif,
UserMaj = @e_UserMaj
WHERE Id = @e_Id
And CAST(Timestamp AS VARCHAR) = @e_Timestamp
end try
begin catch
set @s_erreur = @@error
end catch
end |
Partager