我有兩個連接表。表1有客戶數據,表2有它們的狀態。SQL表加入通配符變量
我需要一個通配符名稱搜索,但我只希望它返回基於表2的狀態結果。這工作時,我實際上輸入一些數據在@ClientName變量,但當它留空它將返回所有記錄並忽略@ClientStatus變量。有想法該怎麼解決這個嗎?
DECLARE @ClientName NVARCHAR (100) = ''
DECLARE @ClientStatus INT = 1
SELECT DBO.CLIENTS.CLIENTID,
DBO.CLIENTS.FIRST_NAME,
DBO.CLIENTS.LAST_NAME,
DBO.CLIENTS.PHONE_NUMBER,
DBO.CLIENTS.MOBILE_NUMBER,
DBO.CLIENTS.HOUSE_ADDRESS_NUMBER,
DBO.CLIENTS.STREET_NAME,
DBO.CLIENTS.TOWN_CITY,
DBO.CLIENTS.POST_CODE,
DBO.CLIENTS.EMAIL_ADDRESS,
DBO.CLIENTS.CLIENT_SOURCE,
DBO.CLIENTS.CLIENT_STATUS,
DBO.CLIENTS.DATE_ADDED,
DBO.CLIENTS.CALL_DATE,
DBO.CLIENTS.CALL_TIME,
DBO.CLIENT_STATUS.CLINIC_STATUS
FROM DBO.CLIENTS
INNER JOIN DBO.CLIENT_STATUS
ON DBO.CLIENTS.CLIENT_STATUS = DBO.CLIENT_STATUS.STATUSID
WHERE (DBO.CLIENT_STATUS.CLINIC_STATUS = @ClientStatus)
AND (DBO.CLIENTS.LAST_NAME LIKE @ClientName + '%')
OR (DBO.CLIENTS.MOBILE_NUMBER LIKE @ClientName + '%')
這就是偉大的諾亞 - 完美的作品 – gillers322