2012-10-29 141 views
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)SQL Server查询与布尔问题

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

同一切retuns具有相同的结果相同的列表无论什么

它不应该是简单的? ??

IsEmployee COL-数据类型为(bit,null)

我开发的SQL服务器2008 ..online服务器是2005年它应该是一个问题...

+0

您可以在'IN'中删除select中的'distinct'关键字,因为它不会执行任何操作。 – Magnus

+0

@Magnus谢谢,已完成,但我将保留它,因为它与您的评论一起作为一个没有效果的例子。 – LoneXcoder

回答

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) 
+0

我应该注意到,这是SQL Server允许您使用数据库和/或查询的ANSI_Nulls选项来控制它的方式。 – jtimperley

+0

什么是那些实际上,我完全忽略了他们:'SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ' – LoneXcoder

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为空。 (是否是员工)例如,通过使用ISNULL(IsEmployee, CAST(0 As bit))来处理NULL值a false

0

我错了CAU的我错过了的事

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) 

我不想显示所有的数据,我不认为其他线路我emited有一个或

笔记,所以我不得不将其添加到那里

+1

您可以优化与地方:'... AND isActive = 1 AND isActiveAgent = 1 AND sivug LIKE“% ,35%'(custType = 1 OR custType = 3或5至12之间的custType)' – Magnus