我有两个连接的表。表 1 有客户数据,表 2 有他们的状态。
我需要进行通配符名称搜索,但我只希望它根据表 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 + '%' )