locked
Beginner question RRS feed

  • Question

  • Hi Guys. I have a beginner issue

    I have this but I have a condition on 'AntiDemarreur': in order for it to be equal to 1 , AC has to be 1
    What do I do? 

    CREATE TABLE VEHICULES(
    NIV char(20) not null unique,
    Annee smallint not null,
    Kilometrage int not null,
    Valeur money not null,
    AC bit not null ,
    AntiDemarreur bit not null ,
    Nouveau bit not null ,
    ModeleID tinyint FOREIGN KEY REFERENCES MODELES (ModeleID) not null,
    CouleurID tinyint FOREIGN KEY REFERENCES COULEURS (CouleurID) not null,
    TypeID tinyint FOREIGN KEY REFERENCES TYPEES (TypeID) not null,
    Duree tinyint not null,
    constraint pk_VEHICULES primary key(NIV)
    Thursday, October 27, 2016 6:28 PM

Answers

  • Use a trigger.


     create trigger test on VEHICULES for insert, update as
     set nocount on
     update VEHICULES set AntiDemarreur = case when i.AC=1 then 1 else VEHICULES.AntiDemarreur end
     from VEHICULES join inserted i on i.NIV=VEHICULES.NIV
     GO
    insert into VEHICULES (NIV, AC, AntiDemarreur,Duree,Annee,Kilometrage,Valeur,Nouveau,ModeleID,CouleurID,TypeID) values(1,1,1,1,2016,1,1,1,1,1,1)
    insert into VEHICULES (NIV, AC, AntiDemarreur,Duree,Annee,Kilometrage,Valeur,Nouveau,ModeleID,CouleurID,TypeID) values(2,2,1,1,2016,1,1,1,1,1,1)
    insert into VEHICULES (NIV, AC, AntiDemarreur,Duree,Annee,Kilometrage,Valeur,Nouveau,ModeleID,CouleurID,TypeID) values(3,2,1,1,2016,1,1,1,1,1,1)
    insert into VEHICULES (NIV, AC, AntiDemarreur,Duree,Annee,Kilometrage,Valeur,Nouveau,ModeleID,CouleurID,TypeID) values(4,2,2,1,2016,1,1,1,1,1,1)

    select * from VEHICULES

    Thursday, October 27, 2016 6:45 PM