0
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

也试过了 IsEmployee = CONVERT(bit,@EmpOrfreeLance)

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

无论如何,它都会以相同的结果返回相同的列表

不应该很简单吗???

IsEmployeecol-数据类型是(bit,null)

我的开发 SQL 服务器是 2008 ..在线服务器是 2005 应该是一个问题...

4

3 回答 3

2

比较空值将始终返回 false。您已经说过 IsEmployee 可以为 null,这可能是您的情况。

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

尝试使用类似的东西进行比较:

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

或者

ISNULL(IsEmployee, 0) = ISNULL(@EmpOrfreeLance, 0)
于 2012-10-29T22:08:52.393 回答
2

我可能遗漏了一些东西,但你为什么不这样写查询呢?

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

此外,您将不得不决定当IsEmployee为空时要做什么。(是否为 Employee)例如通过使用ISNULL(IsEmployee, CAST(0 As bit))来处理NULL值 a false

于 2012-10-29T22:16:15.557 回答
0

我错了,因为我错过了一件事

    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 )

我不想显示所有数据,我没有注意到在我发出的其他行上有​​一个 OR

所以我也不得不将它添加到那里

于 2012-10-29T22:26:39.917 回答