我是sql的新手,我有where子句,像这样:
首次查询(生产)
WHERE [D].[IsLocked] = 0
AND ((@FilterEmpKey IS NULL
AND [TA].[EmpKey] = @CurrentEmpKey)
OR (ISNULL([TA].[ModifiedAssignedBy] , [TA].[AssignatedBy]) = @FilterEmpKey
AND [TA].[EmpKey] = @CurrentEmpKey))
第二个查询(测试)
WHERE [D].[IsLocked] = 0
AND [TA].EmpKey = @CurrentEmpKey
OR (ISNULL([TA].[ModifiedAssignedBy] , [TA].[AssignatedBy]) = @FilterEmpKey
AND [TA].[EmpKey] = @CurrentEmpKey)
我想知道第一查询和第二查询之间的区别是什么,因为结果不一样,我也不知道为什么?问候
注意:我的期望结果是获得测试查询的价值
答案 0 :(得分:1)
这是一个例子。当@FilterEmpKey不为空且@CurrentEmpKey与TA.EmpKey匹配时,查询将返回不同的结果集。
CREATE TABLE TA (EmpKey VARCHAR(25), ModifiedAssignedBy VARCHAR(25), AssignatedBy VARCHAR(25))
INSERT INTO TA VALUES ('test', 'test_mod', 'test_assign')
DECLARE @FilterEmpKey VARCHAR(25) = 'banana'
DECLARE @CurrentEmpKey VARCHAR(25) = 'test'
-- Returns nothing; both conditions are false.
SELECT * FROM TA
WHERE ((@FilterEmpKey IS NULL
AND [TA].[EmpKey] = @CurrentEmpKey)
OR (ISNULL([TA].[ModifiedAssignedBy] , [TA].[AssignatedBy]) = @FilterEmpKey
AND [TA].[EmpKey] = @CurrentEmpKey))
-- Returns a row; first condition is true.
SELECT * FROM TA
WHERE [TA].EmpKey = @CurrentEmpKey
OR (ISNULL([TA].[ModifiedAssignedBy] , [TA].[AssignatedBy]) = @FilterEmpKey
AND [TA].[EmpKey] = @CurrentEmpKey)