馬上就蝙蝠 - 我很新''時''。我讀了以下內容:How do I perform an IF...THEN in an SQL SELECT?但是它並沒有真正回答我的問題。使用案例 - 這可能嗎?
基本上我想要做的是沿着以下線的東西:
select
section_name, *
from
property.lease_period lp
where
lp.lease_current_stop_date < getdate() and (lp.lease_status = 'Active' or lp.lease_status = 'Overholding')
and lp.period_id = @period_id
and lp.building_id = @building_id
and not exists
(
select 1
from lease_deal.lease
where lp.suite_name = tenancy_reference
and lp.building_id = building_id
)
case when(@section_name <> 'ALL')
then(and upper(section_name) = upper(@section_name))
end
order by period_id desc
這可能嗎?如果是這樣我做錯了什麼?
鉈;博士:
基本上我想:
and upper(section_name) = upper(@section_name)
要只適用於我的where子句時@section_name不等於 'ALL'
千萬不要使用select *。在這裏,您將返回section_name兩次,這是浪費的服務器資源。同樣的事情發生,如果你有一個連接。另外選擇*本身更慢。只使用您實際需要的列。 – HLGEM
但是我同意這個SQL用於SSRS報告 - 我的正常調用點是從選擇所有列開始,然後在我知道報告將使用的所有內容(客戶端往往是要求在整個開發過程中添加額外的數據)。 我確實承認,我應該在這裏使用lp。*這裏 - 宣稱*本身就是不好的做法。 – Codingo