简体   繁体   English

具有布尔问题的SQL Server查询

[英]SQL Server Query with boolean issue

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

alter PROCEDURE [dbo].[TCCPAUsersAndNamesByJobPosition] @EmpOrfreeLance bit

AS
BEGIN
    SET NOCOUNT ON;

    SELECT distinct t1.UserId , tblCustomers.name 
    FROM tblTime t1
    inner join tblCustomers on t1.UserId=tblCustomers.custID
    where (t1.userid in (select distinct custID from tblCustomers where sectionCat Like '%,35%') )
    AND (t1.UserId in (select distinct custID from tblCustomers where IsEmployee = @EmpOrfreeLance))

end

tried it also with IsEmployee = CONVERT(bit,@EmpOrfreeLance) 也尝试过IsEmployee = CONVERT(bit,@EmpOrfreeLance)

and SET @EmpOrfreeLance= CASE @EmpOrfreeLance WHEN 1 THEN 0 ELSE 0 END SET @EmpOrfreeLance= CASE @EmpOrfreeLance WHEN 1 THEN 0 ELSE 0 END

same all it retuns same list with same results no matter what 相同,无论如何,它都会重新调谐具有相同结果的相同列表

Shouldn't it be simple ??? 不应该很简单吗?

IsEmployee col-datatype is (bit,null) IsEmployee col数据类型为(bit,null)

my dev SQL server is 2008 ..online server is 2005 should it be a matter ... 我的开发SQL服务器是2008 ..online服务器是2005,如果要解决的话...

Comparing null values will always return false. 比较null值将始终返回false。 You've stated that IsEmployee can be null which is probably your case. 您已声明IsEmployee可以为null,这可能是您的情况。

1 == NULL => False
0 == NULL => False
NULL == NULL => False

Try using something like this for your comparison: 尝试使用类似以下内容进行比较:

(@EmpOrfreeLance IS NULL
    OR IsEmployee IS NULL
    OR IsEmployee = @EmpOrfreeLance)

Or 要么

ISNULL(IsEmployee, 0) = ISNULL(@EmpOrfreeLance, 0)

I might be missing something, but why dont you write the query like this? 我可能会丢失一些东西,但是为什么不这样编写查询呢?

SELECT distinct t1.UserId, tblCustomers.name 
FROM tblTime t1
inner join tblCustomers on t1.UserId=tblCustomers.custID
where sectionCat Like '%,35%' AND 
      ISNULL(IsEmployee, CAST(0 As bit)) = @EmpOrfreeLance

Also you are going to have to decide what to do when IsEmployee is null. 另外,当IsEmployee为null时,您将必须决定要做什么。 (is it an Employee or not) For example by using ISNULL(IsEmployee, CAST(0 As bit)) to treat NULL values a false . (是否为Employee)例如,通过使用ISNULL(IsEmployee, CAST(0 As bit))NULL值视为false

i was wrong caus i missed a thing 我错了原因,我错过了一件事情

    SELECT distinct t1.UserId , tblCustomers.name 
    FROM tblTime t1
    inner join tblCustomers on t1.UserId=tblCustomers.custID
    where (t1.userid in (select distinct custID from tblCustomers where sectionCat Like '%,35%') )
    AND (t1.UserId in (select distinct custID from tblCustomers where IsEmployee = @EmpOrfreeLance))
AND (isActive = 1 AND isActiveAgent  = 1 AND sivug Like '%,35%' AND custType = 1) 
or (isActive  = 1 AND isActiveAgent  = 1 AND sivug Like '%,35%' AND custType = 3) 
or (isActive  = 1 AND isActiveAgent  = 1 AND sivug Like '%,35%' AND custType  between  5 AND 12 )

i did not want to show all data and i did not notes that on other lines i emited there was an OR 我不想显示所有数据,也没有注意到在发出的其他行上有​​一个OR

so i had to add it to there too 所以我也必须将它添加到那里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM