如果 SQL 服务器中的条件为空,则选择所有行
Selecting all the rows if where condition is null in SQL Server
当我没有在 where
子句中提供任何参数时,我想 select 来自临时 table 的所有行。
下面是我的存储过程; #ContactAddressDetails
是我的温度 table,最终数据可用。 @ZipCodeOrigin
、@ZipCodeDestination
为参数
- if
@ZipCodeOrigin = null
和 @ZipCodeDestination = null
那么我想获取所有记录,否则只有那些匹配 where
子句的记录
代码:
CREATE PROCEDURE [CR2].[spGetEmailAddressByZipsTypeahead]
@LoggedInUserId BIGINT
,@ZipCodeOrigin VARCHAR(10) = NULL
,@ZipCodeDestination VARCHAR(10) = NULL
,@searchText varchar(15)
AS
BEGIN
DECLARE @OfficeId INT, @AccountId INT
CREATE TABLE #ContactGroupList (ContactGroupId INT)
SELECT
@OfficeId = OfficeId,
@AccountId = AccountID
FROM CR2.vwAcgUserCustomer WITH (NOLOCK)
WHERE UserID = @LoggedInUserId
-- Find the all Contact Group associated to the User, his/her Office and Account
INSERT INTO #ContactGroupList
SELECT ContactGroupId
FROM CR2.ContactGroup WITH (NOLOCK)
WHERE ((OwnerType = 1 AND OwnerId = @AccountId)
OR (OwnerType = 2 AND OwnerId = @OfficeId)
OR (OwnerType = 3 AND OwnerId = @LoggedInUserId))
--Display all the addresses of the above contact groups.
SELECT
CA.ContactAddressId,
CA.Email AS [Email],
CA.AddressType,
CA.AddressCode
INTO
#ContactAddressDetails
FROM
CR2.ContactAddress AS CA WITH (NOLOCK)
INNER JOIN
#ContactGroupList list ON list.ContactGroupId = CA.ContactGroupId
LEFT JOIN
CR2.ContactAddressDefaultSettings AS CADS WITH (NOLOCK) ON CADS.ContactAddressId = CA.ContactAddressId
LEFT JOIN
CR2.ContactAddressDefault CAD WITH (NOLOCK) ON CAD.ContactAddressId = CA.ContactAddressId
AND CAD.UserId = @LoggedInUserId
WHERE
CA.ZipCode In (@ZipCodeOrigin, @ZipCodeDestination)
AND CA.Email LIKE @searchText + '%'
AND CA.IsDeleted = 0
AND CA.AddressType = 3
SELECT *
FROM #ContactAddressDetails
DROP TABLE #ContactAddressDetails
DROP TABLE #ContactGroupList
END
如果两个邮政编码都为空,您可以使用此谓词不过滤数据:
( (@ZipCodeOrigin is null and @ZipCodeDestination is null)
OR CA.ZipCode In (@ZipCodeOrigin, @ZipCodeDestination)
)
替换
WHERE CA.ZipCode IN (@ZipCodeOrigin, @ZipCodeDestination)
和
WHERE (CA.ZipCode In (@ZipCodeOrigin, @ZipCodeDestination))
OR (@ZipCodeOrigin IS NULL AND @ZipCodeDestination IS NULL)
当 @ZipCodeOrigin
和 @ZipCodeDestination
都为 null,或者当 CA.ZipCode
匹配其中一个或两个时,此条件为真。
当我没有在 where
子句中提供任何参数时,我想 select 来自临时 table 的所有行。
下面是我的存储过程; #ContactAddressDetails
是我的温度 table,最终数据可用。 @ZipCodeOrigin
、@ZipCodeDestination
为参数
- if
@ZipCodeOrigin = null
和@ZipCodeDestination = null
那么我想获取所有记录,否则只有那些匹配where
子句的记录
代码:
CREATE PROCEDURE [CR2].[spGetEmailAddressByZipsTypeahead]
@LoggedInUserId BIGINT
,@ZipCodeOrigin VARCHAR(10) = NULL
,@ZipCodeDestination VARCHAR(10) = NULL
,@searchText varchar(15)
AS
BEGIN
DECLARE @OfficeId INT, @AccountId INT
CREATE TABLE #ContactGroupList (ContactGroupId INT)
SELECT
@OfficeId = OfficeId,
@AccountId = AccountID
FROM CR2.vwAcgUserCustomer WITH (NOLOCK)
WHERE UserID = @LoggedInUserId
-- Find the all Contact Group associated to the User, his/her Office and Account
INSERT INTO #ContactGroupList
SELECT ContactGroupId
FROM CR2.ContactGroup WITH (NOLOCK)
WHERE ((OwnerType = 1 AND OwnerId = @AccountId)
OR (OwnerType = 2 AND OwnerId = @OfficeId)
OR (OwnerType = 3 AND OwnerId = @LoggedInUserId))
--Display all the addresses of the above contact groups.
SELECT
CA.ContactAddressId,
CA.Email AS [Email],
CA.AddressType,
CA.AddressCode
INTO
#ContactAddressDetails
FROM
CR2.ContactAddress AS CA WITH (NOLOCK)
INNER JOIN
#ContactGroupList list ON list.ContactGroupId = CA.ContactGroupId
LEFT JOIN
CR2.ContactAddressDefaultSettings AS CADS WITH (NOLOCK) ON CADS.ContactAddressId = CA.ContactAddressId
LEFT JOIN
CR2.ContactAddressDefault CAD WITH (NOLOCK) ON CAD.ContactAddressId = CA.ContactAddressId
AND CAD.UserId = @LoggedInUserId
WHERE
CA.ZipCode In (@ZipCodeOrigin, @ZipCodeDestination)
AND CA.Email LIKE @searchText + '%'
AND CA.IsDeleted = 0
AND CA.AddressType = 3
SELECT *
FROM #ContactAddressDetails
DROP TABLE #ContactAddressDetails
DROP TABLE #ContactGroupList
END
如果两个邮政编码都为空,您可以使用此谓词不过滤数据:
( (@ZipCodeOrigin is null and @ZipCodeDestination is null)
OR CA.ZipCode In (@ZipCodeOrigin, @ZipCodeDestination)
)
替换
WHERE CA.ZipCode IN (@ZipCodeOrigin, @ZipCodeDestination)
和
WHERE (CA.ZipCode In (@ZipCodeOrigin, @ZipCodeDestination))
OR (@ZipCodeOrigin IS NULL AND @ZipCodeDestination IS NULL)
当 @ZipCodeOrigin
和 @ZipCodeDestination
都为 null,或者当 CA.ZipCode
匹配其中一个或两个时,此条件为真。