I have table with 3 primary keys : CODE_TARIF, UNITE, and MODE_LIV.
I write a stored procedure to copy and paste but with different MODE_LIV.
ex: if I already have 2 rows in table T_TARIF with MODE_LIV = 2, when I run this stored procedure with input MODE_LIV =3, I will have 4 rows .
ALTER PROCEDURE [dbo].[Copy_Tarif]
-- Add the parameters for the stored procedure here
@MODE_LIV int
AS
BEGIN
DECLARE @CODE_TARIF varchar(15)
DECLARE @ZONE int
DECLARE @UNITE int
DECLARE @LIBELLE varchar(30)
DECLARE @TR_DEB int
DECLARE @TR_FIN int
DECLARE @MONTANT decimal(18,2)
DECLARE tarif_cursor CURSOR FOR
SELECT CODE_TARIF, ZONE, UNITE, LIBELLE, TR_DEBUT, TR_FIN, MONTANT
FROM T_TARIF
OPEN tarif_cursor;
FETCH NEXT FROM tarif_cursor
INTO @CODE_TARIF, @ZONE, @UNITE, @LIBELLE, @TR_DEB, @TR_FIN, @MONTANT;
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO [T_TARIF]
([CODE_TARIF]
,[ZONE]
,[UNITE]
,[MODE_LIV]
,[LIBELLE]
,[TR_DEBUT]
,[TR_FIN]
,[MONTANT]
)
VALUES
(@CODE_TARIF
,@ZONE
,@UNITE
,@MODE_LIV
,@LIBELLE
,@TR_DEB
,@TR_FIN
,@MONTANT
)
FETCH NEXT FROM tarif_cursor
INTO @CODE_TARIF, @ZONE, @UNITE, @LIBELLE, @TR_DEB, @TR_FIN, @MONTANT;
END
END
It works, but gives an error see Video : Strange Stored Procedure
Thanks you in advance, Stev
MODE_LIVis already equal to the target value of@MODE_LIV. Or there might be rows with identicalCODE_TARIF&UNITEbut differentMODE_LIV. So my point is, you should add some filtering to the cursor SELECT (or, better still, to the SELECT part of the INSERT statement as suggested by @gbn)