IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

MS SQL Server Discussion :

IF THEN ELSE en SQL


Sujet :

MS SQL Server

  1. #1
    Membre habitué
    Inscrit en
    Février 2008
    Messages
    11
    Détails du profil
    Informations forums :
    Inscription : Février 2008
    Messages : 11
    Par défaut IF THEN ELSE en SQL
    Bonjour,

    Les boucles If...THEN ne marchent-elles pas en SQL ?
    Ma requete veut que, à chaque fois qu'on insère une cotisation, la réserve est automatiquement recalculée en prenant la réserve de l'année précédente + la nouvelle cotisation. Cependant, lorsqu'une personne cotise pour la première fois, il ne sait pas aller chercher la réserve de l'année passée, vu qu'elle n'existe pas. Donc j'aimerais introduire un if (voir fin du code), mais cela ne marche pas.

    Merci d'avance pour votre aide précieuse !

    Christophe


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    ALTER TRIGGER [dbo].[Insert_ASS_Cotisation]
       ON  [dbo].[ASS_COTISATION]
       AFTER INSERT
    AS 
    BEGIN
    	-- SET NOCOUNT ON added to prevent extra result sets from
    	-- interfering with SELECT statements.
    	SET NOCOUNT ON;
     
    	--Déclaration des variables.
    	DECLARE @CodeSabam char(10)
    	DECLARE @Year char(4)
    	DECLARE @montantcot float
    	DECLARE @PBforf float
    	DECLARE @PBprop float
    	DECLARE @ResCot float
    	DECLARE @ResPBforf float
    	DECLARE @ResPBprop float
     
    	--Assignation d'une valeur aux variables.
    	SET @CodeSabam = (SELECT CodeSabam FROM INSERTED)
    	SET @Year = (SELECT Year FROM INSERTED)
    	SET @montantcot=(select [Cotisation] from dbo.[ASS_COTISATION] where ([CodeSabam] = @CodeSabam AND [Year] = @Year))
    	SET @PBforf=(select [PBForfaitaire] from dbo.[ASS_COTISATION] where ([CodeSabam] = @CodeSabam AND [Year] = @Year))
    	SET @PBprop=(select [PBProportionnel] from dbo.[ASS_COTISATION] where ([CodeSabam] = @CodeSabam AND [Year] = @Year))
    	SET @ResCot=(select [Cotisation] from dbo.[ASS_RESERVE] where ([CodeSabam] = @CodeSabam AND [Year] = @Year - 1))
    	SET @ResPBforf=(select [PBForfaitaire] from dbo.[ASS_RESERVE] where ([CodeSabam] = @CodeSabam AND [Year] = @Year - 1))
    	SET @ResPBprop=(select [PBProportionnel] from dbo.[ASS_RESERVE] where ([CodeSabam] = @CodeSabam AND [Year] = @Year - 1))
     
    	--Insertion du nouveau montant de réserve.
    	IF @ResCot IS NULL THEN
    	INSERT INTO [dbo].[ASS_RESERVE] ([CodeSabam], [Year] ,[Cotisation],[PBForfaitaire],[PBProportionnel]) VALUES(@CodeSabam ,@Year ,@montantcot,@pbforf,@pbprop)
    	ELSE
    	INSERT INTO [dbo].[ASS_RESERVE] ([CodeSabam], [Year] ,[Cotisation],[PBForfaitaire],[PBProportionnel]) VALUES(@CodeSabam ,@Year ,@ResCot + @montantcot,@ResPBforf + @pbforf,@ResPBprop + @pbprop)
    	END IF
     
    END
    Voici le message d'erreur:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    Msg 156, Level 15, State 1, Procedure Insert_ASS_Cotisation, Line 35
    Incorrect syntax near the keyword 'THEN'.
    Msg 156, Level 15, State 1, Procedure Insert_ASS_Cotisation, Line 37
    Incorrect syntax near the keyword 'ELSE'.
    Msg 156, Level 15, State 1, Procedure Insert_ASS_Cotisation, Line 41
    Incorrect syntax near the keyword 'END'.

  2. #2
    Membre Expert
    Avatar de cavo789
    Homme Profil pro
    Développeur Web
    Inscrit en
    Mai 2004
    Messages
    1 797
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Mai 2004
    Messages : 1 797
    Par défaut
    Bonjour

    Le IF THEN ELSE existe seulement la syntaxe est différente de p.e. Visual Basic. As-tu compulsé l'aide en ligne de T-SQL ?


    IF Condition
    SELECT .....
    ELSE
    SELECT ...


    Il ne faut pas le mot THEN qui est présumé et il n'y a pas de END IF non plus.

  3. #3
    Membre expérimenté
    Homme Profil pro
    Administrateur de base de données
    Inscrit en
    Juillet 2007
    Messages
    193
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Administrateur de base de données
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2007
    Messages : 193
    Par défaut
    if (machin machin)
    (begin)
    traitement
    (end)
    else
    (begin)
    traitement
    (end)

    Begin et end sont obligatoire si il y a plus d'une instruction a exécuter
    Il ne faut pas de then mais placer la condition entre ()
    et le end if est inutile vu que ça utilise la syntaxe C des {} remplacer par begin end

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    IF (@ResCot IS NULL)
    	INSERT INTO [dbo].[ASS_RESERVE] ([CodeSabam], [Year] ,[Cotisation],[PBForfaitaire],[PBProportionnel]) VALUES(@CodeSabam ,@Year ,@montantcot,@pbforf,@pbprop)
    	ELSE
    	INSERT INTO [dbo].[ASS_RESERVE] ([CodeSabam], [Year] ,[Cotisation],[PBForfaitaire],[PBProportionnel]) VALUES(@CodeSabam ,@Year ,@ResCot + @montantcot,@ResPBforf + @pbforf,@ResPBprop + @pbprop)

  4. #4
    Membre habitué
    Inscrit en
    Février 2008
    Messages
    11
    Détails du profil
    Informations forums :
    Inscription : Février 2008
    Messages : 11
    Par défaut
    Ok un grand merci ça marche maintenant

  5. #5
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Novembre 2007
    Messages
    125
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Japon

    Informations forums :
    Inscription : Novembre 2007
    Messages : 125
    Par défaut
    bonjour,
    juste pour info


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    SELECT @montantcot=[Cotisation] ,@PBforf= PBForfaitaire
    FROM dbo.[ASS_COTISATION] 
    WHERE ([CodeSabam] = @CodeSabam AND [Year] = @Year)
    bien à vous

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Débutant] if...then...else
    Par jive dans le forum ASP
    Réponses: 5
    Dernier message: 16/09/2005, 14h11
  2. [TagLib] Structure if then else
    Par mush_H dans le forum Taglibs
    Réponses: 5
    Dernier message: 19/07/2005, 15h31
  3. If Then Else
    Par Piout dans le forum MS SQL Server
    Réponses: 13
    Dernier message: 24/02/2005, 16h09
  4. IF THEN ELSE imbriqués
    Par nuke_y dans le forum Oracle
    Réponses: 2
    Dernier message: 15/11/2004, 14h57
  5. [CR 7] [débutante] pb avec if then else
    Par xs_nady dans le forum Formules
    Réponses: 8
    Dernier message: 28/05/2004, 15h36

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo