错误消息206,Niveau 16,État2,Ligne 91 d'opérandes的类型冲突:日期和日期不兼容avec tinyint

问题描述

我遇到错误

错误消息206,Niveau 16,État2,Ligne 91
Conféde type d'opérandes:日期est不兼容avec tinyint

代码

CREATE TABLE reservation 
(
    codres int PRIMARY KEY IDENTITY(1,1),numcl int FOREIGN KEY REFERENCES client,numt int FOREIGN KEY REFERENCES typesejour,codho int FOREIGN KEY REFERENCES hotel,datedebut date,datefin date,montant float DEFAULT 0,CONSTRAINT ck CHECK (datefin > datedebut)
)

ALTER TABLE reservation 
    ADD CONSTRAINT ch1 DEFAULT 0 FOR montant;

UPDATE reservation 
SET montant = 0.9 * montant
WHERE MONTH(datedebut) IN (9,10) 
  AND (datefin) IN (9,10);

解决方法

这是您的where语句的update子句:

where month(datedebut) in (9,10) and (datefin) in (9,10)

第二个条件引发错误,因为您试图将datedatefin)与整数列表(910)进行比较。 / p>

大概是您的意思:

where month(datedebut) in (9,10) and month(datefin) in (9,10)

请注意,比较这样的日期效率很低。尽可能使用直接日期比较会更好。假设您想要去年的9月和10月,则可以这样写:

where 
        datedebut >= '20190901'
    and datedebut <  '20191101'
    and datefin   >= '20190901'
    and datefin   <  '20191101'

当然,如果您希望每年的9月和10月,那么表达是正确的选择。