我在Excel中有一个SQL查询,如果我将日期硬编码到查询中,它将很好地工作。一旦我尝试将它们更改为?
,就会出现错误。我在大量报告中使用了这样的参数化查询,所以我不确定为什么这个突然无法使用。
完整错误为[Microsoft][ODBC SQL Server Driver][SQL Server]The multi-part identifier "cancel.arrival_date" could not be bound.
,它会弹出两次。
这是我的查询中带有?
的错误:
SELECT cancel.reservation_number, (client.last_name + ', ' + client.first_name) AS 'guest_name',
cancel.cancel_date_time, cancel.arrival_date,
DATEDIFF(DAY, cancel.cancel_date_time, cancel.arrival_date) AS 'Days Out', cancel.cancel_reason,
CASE
WHEN EXISTS (SELECT *
FROM gbfol_head head
LEFT JOIN gbfol_det det ON head.folio_number = det.folio_number
WHERE cancel.reservation_number = head.source_id
AND head.folio_type <> 'b' AND det.posting_code = 'admn') THEN
'ADMN Charged'
ELSE
'No ADMN Fee'
END AS 'ADMN',
cancel.amount, cancel.cancel_clerk_code, cancel.sba_text
FROM canceled cancel
LEFT JOIN reservation res ON cancel.reservation_number = res.reservation_number
LEFT JOIN clients client ON res.home_client_code = client.client_code
WHERE DATEDIFF(DAY, cancel.cancel_date_time, cancel.arrival_date) < 21
AND (cancel.arrival_date BETWEEN ? AND ?)
如果我将最后一行更改为AND (cancel.arrival_date BETWEEN '2019-12-01' AND '2019-12-10')
,则效果很好。
我还尝试过使用AND (cancel.arrival_date >= ? AND cancel.arrival_date <= ?)
,但同样不起作用,同样的错误。