我試圖使用作爲客戶代碼的輸入變量來檢索數據。如果用戶輸入客戶代碼,查詢將檢索該客戶數據,但如果用戶將客戶代碼留空,我想檢索所有客戶數據。以下是我可以根據'3_customer'中的客戶代碼輸入檢索數據的代碼,但我無法弄清楚如何執行此IF-THEN-ELSE類型的查詢,我需要爲所有客戶獲取數據(如果輸入變量留空。SQL - 選擇基於一個特定輸入值的數據或全部輸入值(如果沒有輸入值)
謝謝, 唐
SELECT
open_item.order_id order_id,
convert(varchar(10),orders.ordered_date,101) order_date,
orders.revenue_code_id,
orders.operations_user,
open_item.customer_id cust_no,
customer.name cust_name,
convert(varchar(10),open_item.gl_date,101) adjust_date,
open_item.amount amount,
open_item.record_type type,
open_item.ar_reason_code_id,
ar_reason_code.descr reason
FROM
open_item
LEFT OUTER JOIN
customer ON customer.id = open_item.customer_id
LEFT OUTER JOIN
ar_reason_code ON open_item.ar_reason_code_id = ar_reason_code.id
LEFT OUTER JOIN
orders ON orders.id = open_item.order_id
WHERE
open_item.gl_date >= $1_sdate$ AND
open_item.gl_date <= $2_edate$ AND
open_item.source = 'C' AND
open_item.record_type = 'C' AND
open_item.customer_id = $3_customer$
這是什麼數據庫? – Ruslan
@Ruslan:看起來像SQL Server –
那麼customer_ID可以是「Blank」/ empty set而不是NULL嗎?或者customer.id中可能有非顯示字符? – xQbert