2012-10-29 56 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