У меня был предыдущий скрипт, который делает много обращений к базе данных с кодом «for», я меняю logi c, сохраняю все во временной таблице, и я присоединяю данные к "#cellInfoToDelete"
, у меня эти ошибки в новом скрипте:
Msg 8133, Level 16, State 1, Line 14 По крайней мере, одно из выражений результата в спецификации CASE должно быть выражением, отличным от константы NULL
Сообщение 207, уровень 16, состояние 1, строка 47 Недопустимое имя столбца "SchedID"
Старый сценарий:
UPDATE
_PS_SCHEDULES_Details_CellInfo2
SET
EmpNumDay1 = CASE WHEN @WeekDay = 1 THEN NULL ELSE EmpNumDay1 END,
EmpNumDay2 = CASE WHEN @WeekDay = 2 THEN NULL ELSE EmpNumDay2 END,
EmpNumDay3 = CASE WHEN @WeekDay = 3 THEN NULL ELSE EmpNumDay3 END,
EmpNumDay4 = CASE WHEN @WeekDay = 4 THEN NULL ELSE EmpNumDay4 END,
EmpNumDay5 = CASE WHEN @WeekDay = 5 THEN NULL ELSE EmpNumDay5 END,
EmpNumDay6 = CASE WHEN @WeekDay = 6 THEN NULL ELSE EmpNumDay6 END,
EmpNumDay7 = CASE WHEN @WeekDay = 7 THEN NULL ELSE EmpNumDay7 END,
NotUsed1 = CASE WHEN @WeekDay = 1 THEN 1 ELSE NotUsed1 END,
NotUsed2 = CASE WHEN @WeekDay = 2 THEN 1 ELSE NotUsed2 END,
NotUsed3 = CASE WHEN @WeekDay = 3 THEN 1 ELSE NotUsed3 END,
NotUsed4 = CASE WHEN @WeekDay = 4 THEN 1 ELSE NotUsed4 END,
NotUsed5 = CASE WHEN @WeekDay = 5 THEN 1 ELSE NotUsed5 END,
NotUsed6 = CASE WHEN @WeekDay = 6 THEN 1 ELSE NotUsed6 END,
NotUsed7 = CASE WHEN @WeekDay = 7 THEN 1 ELSE NotUsed7 END
WHERE
ID = @SchedID
Новый сценарий:
UPDATE
PSDetails
SET
EmpNumDay1 = COALESCE(PSDetails.EmpNumDay1, cellToDel.EmpNumDay1),
EmpNumDay2 = COALESCE(PSDetails.EmpNumDay2, cellToDel.EmpNumDay2),
EmpNumDay3 = COALESCE(PSDetails.EmpNumDay3, cellToDel.EmpNumDay3),
EmpNumDay4 = COALESCE(PSDetails.EmpNumDay4, cellToDel.EmpNumDay4),
EmpNumDay5 = COALESCE(PSDetails.EmpNumDay5, cellToDel.EmpNumDay5),
EmpNumDay6 = COALESCE(PSDetails.EmpNumDay6, cellToDel.EmpNumDay6),
EmpNumDay7 = COALESCE(PSDetails.EmpNumDay7, cellToDel.EmpNumDay7),
NotUsed1 = COALESCE(PSDetails.NotUsed1, cellToDel.NotUsed1),
NotUsed2 = COALESCE(PSDetails.NotUsed2, cellToDel.NotUsed2),
NotUsed3 = COALESCE(PSDetails.NotUsed3, cellToDel.NotUsed3),
NotUsed4 = COALESCE(PSDetails.NotUsed4, cellToDel.NotUsed4),
NotUsed5 = COALESCE(PSDetails.NotUsed5, cellToDel.NotUsed5),
NotUsed6 = COALESCE(PSDetails.NotUsed6, cellToDel.NotUsed6),
NotUsed7 = COALESCE(PSDetails.NotUsed7, cellToDel.NotUsed7)
FROM
_PS_SCHEDULES_Details_CellInfo2 AS PSDetails
INNER JOIN
(SELECT
CASE WHEN cellToDel.WeekDay = 1 THEN NULL END as EmpNumDay1,
CASE WHEN cellToDel.WeekDay = 2 THEN NULL END as EmpNumDay2,
CASE WHEN cellToDel.WeekDay = 3 THEN NULL END as EmpNumDay3,
CASE WHEN cellToDel.WeekDay = 4 THEN NULL END as EmpNumDay4,
CASE WHEN cellToDel.WeekDay = 5 THEN NULL END as EmpNumDay5,
CASE WHEN cellToDel.WeekDay = 6 THEN NULL END as EmpNumDay6,
CASE WHEN cellToDel.WeekDay = 7 THEN NULL END as EmpNumDay7,
MAX(CASE WHEN cellToDel.WeekDay = 1 THEN 1 END) as NotUsed1,
MAX(CASE WHEN cellToDel.WeekDay = 2 THEN 1 END) as NotUsed2,
MAX(CASE WHEN cellToDel.WeekDay = 3 THEN 1 END) as NotUsed3,
MAX(CASE WHEN cellToDel.WeekDay = 4 THEN 1 END) as NotUsed4,
MAX(CASE WHEN cellToDel.WeekDay = 5 THEN 1 END) as NotUsed5,
MAX(CASE WHEN cellToDel.WeekDay = 6 THEN 1 END) as NotUsed6,
MAX(CASE WHEN cellToDel.WeekDay = 7 THEN 1 END) as NotUsed7
FROM
#cellInfoToDelete cellToDel) cellToDel ON PSDetails.ID = cellToDel.SchedID