1
我有一個查詢,我一直在修補以用作報告的來源。 由於某些奇怪的原因,當我嘗試將datepart(dw,GetDate())的輸出與一個整數(週一爲2)進行比較時,它給了我一個錯誤,告訴我不正確的語法。然後我開始研究爲什麼我無法比較iif聲明中的兩個整數,並且我發現絕對沒有答案。 這裏是完整的代碼(我去來回如何做到這一點,這是本次迭代的嘗試)將datepart的輸出與整數進行比較的錯誤
DECLARE @dp integer,@d integer
set @dp =DATEpart(dw,GETDATE())
set @d = 2
SELECT TOP 100 PERCENT CUST_ORDER_LINE.PRODUCT_CODE
,CUSTOMER_ORDER.ID
,CUSTOMER_ORDER.CUSTOMER_ID
,CUST_ORDER_LINE.PART_ID
,CUST_ORDER_LINE.MISC_REFERENCE
,CUST_ORDER_LINE.COMMODITY_CODE
,CUSTOMER_ORDER.SALESREP_ID
,CUSTOMER_ORDER.TERRITORY
,CUST_ORDER_LINE.ORDER_QTY * CUST_ORDER_LINE.UNIT_PRICE AS Amount
,CUSTOMER_ORDER.ORDER_DATE
FROM CUST_ORDER_LINE
INNER JOIN CUSTOMER_ORDER ON CUST_ORDER_LINE.CUST_ORDER_ID = CUSTOMER_ORDER.ID
WHERE (
iif(@dp = @d,CUSTOMER_ORDER.ORDER_DATE BETWEEN (dateadd(day, datediff(day, 0, getdate()), 0) - 3) AND (dateadd(day, datediff(day, 0, getdate()),0))),
CUSTOMER_ORDER.ORDER_DATE BETWEEN (dateadd(day, datediff(day, 0, getdate()), 0) - 1)) AND (dateadd(day, datediff(day, 0, getdate()), + 1))))
)
ORDER BY CUSTOMER_ORDER.ORDER_DATE DESC
此查詢的一點是,從昨天拉的數據,以及週五如果是週一。編號:錯誤代碼消息170,級別15,狀態1,行19 行19:'='附近語法錯誤。
'iif' not in 2008. https://docs.microsoft.com/en-us/sql/t-sql/functions/logical-functions-iif-transact-sql。 – SQLChao