Avec quoi remplacer un Curseur

Kagami -  
 Kagami -
Bonjour,
s'il vous plait est ce que quelqu'un pourra m'aider sur cette procédure stockée parce ce vraiment jusqu'à maintenant personne n'a pu l faire , soit à cause de sa taille ou bien il ne sait pas comment faire .
le problème ce qu'avec cette procédure, mon application me génère une erreur de DEADLOCK ( La transaction (ID de processus 52) a été bloquée sur les ressources verrou par un autre processus et a été choisie comme victime. Ré exécutez la transaction.) et c'est à 100% de cette PS que vient l'erreur . je veux juste enlever ce curseur mais je n 'ai pas su comment l faire sans avoir d'autres erreurs

La question posée c'est comment l'exécuter sans avoir à faire au curseur ou même au transaction si possible, parce que avec ces derniers que le problème se régénère.

1 réponse

  1. Kagami
     
    voilà ma procédure :
     USE [Pharmatica]
    GO
    /****** Object:  StoredProcedure [dbo].[FinDel]    Script Date: 07/15/2013 15:11:57 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER PROCEDURE [dbo].[FinDel]
    @iIdVente int,
    @NewNumVenteErrorChek int=0
    AS
    declare @iIdArticle int,
    @dPrix decimal(16,2),
    @iStock int,
    @iQteReserve int,
    @iQteServie int,
    @sDate varchar(50),
    @iUnite int,
    @iUniteGratuiteVente int,
    @iIdTransVente int,
    @iMois int,
    @iAnnee int,
    @iNbreVente int,
    @iQteVendu int,
    @iSomme int,
    @iConsommation int,
    @iVenteMois int,
    @fPrixVente decimal(16,2),
    @iMAJQteStock int,
    @iStockTotal int,
    @iMAJUniteGratuite int,
    @iUniteGratuitePdt int,
    @QteReserve int, @StockTotal int
    begin transaction
    	SET @sDate = convert(varchar(50),getdate(),103)
    	declare encours cursor FOR 
    		SELECT id_trans_vente,transactions_lingne_ventes.id_article,prix_unit_ttc_article,stock_article_stock,qte_reserve_article_stock,qte_servie_ligne,unite_gratuite_article_stock
    		FROM transactions_lingne_ventes,article_stock
    		WHERE id_vente=@iIdVente
    		AND transactions_lingne_ventes.id_article=article_stock.id_article
    		AND transactions_lingne_ventes.prix_unit_ttc_article=article_stock.prix_vente_article_stock
    	OPEN Encours
    		FETCH NEXT FROM Encours
    		INTO @iIdTransVente,@iIdArticle,@dPrix,@iStock,@iQteReserve,@iQteServie,@iUnite
    		WHILE @@FETCH_STATUS = 0
    		BEGIN
    		IF(@iQteServie>0)
    		begin		
    			IF(@iUnite>=@iQteServie)
    			begin
    				SET @iUniteGratuiteVente = @iQteServie
    			end
    			else
    			begin
    				SET @iUniteGratuiteVente = @iUnite
    			end
    			SET @iUnite = @iUnite - @iUniteGratuiteVente
    			UPDATE transactions_lingne_ventes SET unite_gratuite_ligne=@iUniteGratuiteVente WHERE id_trans_vente=@iIdTransVente
    			SET @NewNumVenteErrorChek = @@ERROR
    			UPDATE article_stock SET unite_gratuite_article_stock = @iUnite WHERE id_article=@iIdArticle AND prix_vente_article_stock LIKE @dPrix
    			SET @NewNumVenteErrorChek = @@ERROR
    		end
    		else
    		begin
    			IF(@iUnite>0)
    			begin
    				SET @iUniteGratuiteVente = @iQteServie
    			end
    			else
    			begin
    				SET @iUniteGratuiteVente = 0
    			end
    			--set @iUnite = @iUnite - @iUniteGratuiteVente
    			UPDATE transactions_lingne_ventes SET unite_gratuite_ligne=@iUniteGratuiteVente WHERE id_trans_vente=@iIdTransVente
    			SET @NewNumVenteErrorChek = @@ERROR
    		end
     
     
     
     
     
    ------------------------------------------------------------------------------------------------------------------------------------------
    		--------------------MAJ historique article et nbre vente article
    		--exec MAJArticle @iIdArticle,@iQteServie,1,@sDate
     
    	SET @iNbreVente = (SELECT DISTINCT NBR_SORTIE_ARTICLE FROM ARTICLE WHERE ID_ARTICLE=@iIdArticle)
    	IF @iNbreVente= NULL
    		begin
    			SET @iNbreVente = 0
    		end
    	SET @iNbreVente = @iNbreVente	+ 1
     
    	SET @iQteVendu = (SELECT DISTINCT NBR_VENTE_ARTICLE FROM ARTICLE WHERE ID_ARTICLE=@iIdArticle)
    	IF @iQteVendu=NULL
    		begin
    			SET @iQteVendu = 0
    		end
    	SET @iQteVendu = @iQteVendu + @iQteServie
     
    	UPDATE ARTICLE
    	SET NBR_VENTE_ARTICLE = @iQteVendu,
    	NBR_SORTIE_ARTICLE= @iNbreVente,
    	DATE_DERN_SORTIE_ARTICLE=@sDate
    	WHERE ID_ARTICLE=@iIdArticle
    	SET @NewNumVenteErrorChek = @@ERROR
    --
    SET @iMois = month(getdate())
    SET @iAnnee = year(getdate())
    -----------
    --test si le pdt existe ds la table historique pr le mois et l'annee courante
     SET @iSomme =(SELECT count(*) FROM HISTORIQUE_ARTICLE WHERE ID_ARTICLE=@iIdArticle AND annee_historique_article=@iAnnee AND mois_historique_article=@iMois)
     IF(@iSomme>0)
    --si il existe on fait la MAJ
    	begin
    		UPDATE historique_article
    		SET NBR_VENTE_MOIS_ARTICLE = (SELECT NBR_VENTE_MOIS_ARTICLE FROM HISTORIQUE_ARTICLE WHERE ID_ARTICLE=@iIdArticle AND annee_historique_article=@iAnnee AND mois_historique_article=@iMois) + @iQteServie,
    		NBR_SORTIE_MOIS_ARTICLE=  (SELECT NBR_SORTIE_MOIS_ARTICLE FROM HISTORIQUE_ARTICLE WHERE ID_ARTICLE=@iIdArticle AND annee_historique_article=@iAnnee AND mois_historique_article=@iMois) + 1
    		WHERE ID_ARTICLE=@iIdArticle AND annee_historique_article=@iAnnee AND mois_historique_article=@iMois
    		SET @NewNumVenteErrorChek = @@ERROR
     
    		---MAJ VENTE_MOIS_ARTICLE et CONS_MOIS_ARTICLE de la table article
    		SET @iVenteMois = 0
    		SET @iConsommation = 0
    		SET @iVenteMois = (SELECT VENTE_MOIS_ARTICLE FROM article WHERE id_article=@iIdArticle)
    		IF(@iVenteMois IS NULL)
     
    		begin
    			SET @iVenteMois = 0
    		end
    		SET @iConsommation = (SELECT CONS_MOIS_ARTICLE FROM article WHERE id_article=@iIdArticle)
     
    		IF(@iConsommation IS NULL)
    		begin
    			SET @iConsommation = 0
    		end
    		SET @iConsommation = @iConsommation + @iQteServie
    		SET @iVenteMois = @iVenteMois + 1
    -------------------------------------------------------------------------
    		UPDATE article SET CONS_MOIS_ARTICLE=@iConsommation,VENTE_MOIS_ARTICLE=@iVenteMois WHERE ID_ARTICLE=@iIdArticle
    		SET @NewNumVenteErrorChek = @@ERROR
    -------------------------------------------------------------------------
    	end 
    else
    	begin
    		INSERT INTO historique_article(id_article,annee_historique_article,mois_historique_article,NBR_VENTE_MOIS_ARTICLE,NBR_ACHAT_MOIS_ARTICLE,NBR_SORTIE_MOIS_ARTICLE,NBR_ENTREE_MOIS_ARTICLE) VALUES(@iIdArticle,@iAnnee,@iMois,@iQteServie,0,1,0)
    		SET @NewNumVenteErrorChek = @@ERROR
    -------------------------------------------------------------------------
    UPDATE article SET CONS_MOIS_ARTICLE=@iQteServie,
    		VENTE_MOIS_ARTICLE=1,
    		NBR_SORTIE_ARTICLE=@iQteServie,
    		NBR_VENTE_ARTICLE=1
    		 WHERE ID_ARTICLE=@iIdArticle
     
    		SET @NewNumVenteErrorChek = @@ERROR
    -------------------------------------------------------------------------
    	end
     
    ----------------
    		SET @NewNumVenteErrorChek = @@ERROR
    		SET @iStock		= @iStock - @iQteServie
    		SET @iQteReserve	= @iQteReserve - @iQteServie
     
    ------------------------------------------------------------------------------------------------------------------------------------------
    		---------------------MAJ Stock
    		--exec MAJQteReserveArticle @iIdArticle,@iQteReserve,@dPrix,1,@iStock
    SET @iMAJQteStock=1
    SET @iStockTotal=0
    SET @iMAJUniteGratuite=0
     
    /*MAJ de la Qte Reserve ds ARTICLE_STOCK et ARTICLE*/
    UPDATE 
    	ARTICLE_STOCK
    SET
    	QTE_RESERVE_ARTICLE_STOCK= @iQteReserve
     
    WHERE 
    	ID_ARTICLE=@iIdArticle
    	AND
    	PRIX_VENTE_ARTICLE_STOCK LIKE @dPrix
    SET @NewNumVenteErrorChek = @@ERROR
     
    UPDATE 
    	ARTICLE
    SET
    	QTE_RESERVE_ARTICLE= (SELECT sum(QTE_RESERVE_ARTICLE_stock) FROM ARTICLE_stock WHERE ID_ARTICLE=@iIdArticle)
     
    WHERE 
    	ID_ARTICLE=@iIdArticle
    SET @NewNumVenteErrorChek = @@ERROR	
     
    /*MAJ STOCK ARTICLE STOCK*/
    IF(@iMAJQteStock>0)
    begin
    	---insertion ds la table mouvement article
    	declare @iStock1 int,
    	@iQte int
    		SET @iStock1 = (SELECT top 1 STOCK_ARTICLE_STOCK FROM ARTICLE_STOCK WHERE ID_ARTICLE=@iIdArticle AND PRIX_VENTE_ARTICLE_STOCK LIKE @dPrix)
    		SET @iQte     = @iStock - @iStock1
    		IF(@iQte!=0)
    		begin
    			INSERT INTO mouvement_article(date_mouvement,stock_ancien,nature_mouvement,qte_modifie,prix_vente_article,id_article) VALUES(GETUTCDATE(),@iStock1,'VENTE',@iQte,@dPrix,@iIdArticle)
    			SET @NewNumVenteErrorChek = @@ERROR
    		end
     
    	/*MAJ de stock  de l'article */
    		UPDATE
    			ARTICLE_STOCK
    		SET
    			STOCK_ARTICLE_STOCK=@iStock,
    			date_vente_article_stock = convert(varchar(50),getdate(),103)
    		WHERE
    			ID_ARTICLE=@iIdArticle
    			AND
    			PRIX_VENTE_ARTICLE_STOCK LIKE @dPrix
    		SET @NewNumVenteErrorChek = @@ERROR
    		IF(EXISTS(SELECT * FROM article_stock WHERE ID_ARTICLE=@iIdArticle AND PRIX_VENTE_ARTICLE_STOCK LIKE @dPrix AND suppression LIKE 'oui'))
    		begin
    			IF(@iStock!=0)
    			begin
    				UPDATE 	ARTICLE_STOCK SET suppression='NON' WHERE ID_ARTICLE=@iIdArticle AND PRIX_VENTE_ARTICLE_STOCK LIKE @dPrix
    				SET @NewNumVenteErrorChek = @@ERROR
    			end
    		end
    		UPDATE
    			ARTICLE
    		SET
    			STOCK_TOTAL_ARTICLE=(SELECT sum(STOCK_ARTICLE_STOCK) FROM ARTICLE_STOCK WHERE ID_ARTICLE=@iIdArticle)
    		WHERE
    			ID_ARTICLE=@iIdArticle
    		SET @NewNumVenteErrorChek = @@ERROR
    		IF(@iMAJUniteGratuite!=0)
    		begin
    			SET @iUniteGratuitePdt = (SELECT unite_gratuite_article_stock FROM article_stock  WHERE ID_ARTICLE=@iIdArticle AND PRIX_VENTE_ARTICLE_STOCK LIKE @dPrix)
    			IF(@iUniteGratuitePdt IS NULL)
    			begin
    				SET @iUniteGratuitePdt = 0
    			end
    			SET @iUniteGratuitePdt = @iUniteGratuitePdt+@iUnite
    			UPDATE article_stock SET unite_gratuite_article_stock=@iUniteGratuitePdt  WHERE ID_ARTICLE=@iIdArticle AND PRIX_VENTE_ARTICLE_STOCK LIKE @dPrix
    			SET @NewNumVenteErrorChek = @@ERROR
    		end
     
    end
    ------------------------------------------------------------------------------------------------------------------------------------------
    SET @NewNumVenteErrorChek = @@ERROR
    -----------------------------------
    FETCH NEXT FROM Encours 
    INTO @iIdTransVente,@iIdArticle,@dPrix,@iStock,@iQteReserve,@iQteServie,@iUnite
    END
    CLOSE Encours
    DEALLOCATE Encours
    --commit transaction
    IF @NewNumVenteErrorChek = 0 
    	BEGIN
    		COMMIT TRANSACTION
    	END
    else
    	BEGIN
    		ROLLBACK TRANSACTION
    	END
    0