2015-08-31 61 views
0

我有以下SQL查詢(我知道它可能相當醜陋和低效率)。我需要能夠查詢哪裏案例查詢Where Where子句SQL Server 2008

IF @techID = 0 
Then Technician.Tech_ID > 0 
Else @TechID <> 0 
Then Technician.Tech_ID = @TechID 

我在一個小的損失,作爲對如何做到這一點

sql = "Select Long_Call.Department,Technician.First_Name,Technician.Last_Name, Clinic_ID,Long_Call.Case_Number,Long_Call.Synopsis," + 
          "Long_Call.Version,Long_Call.Time_On_Call, RIGHT(CONVERT(VARCHAR,Long_Call.Call_DateTime,100),7) as Call_Time,CONVERT(VARCHAR(10),cast(Long_Call.Call_DateTime as date),101) as Call_Date,"+ 
          "Call_Tracking.Description as Description from Long_Call,Call_Tracking, " + 
          "Technician where Long_Call.Tech_ID = Technician.Tech_ID and Long_Call.Tracking_ID = Call_Tracking.Tracking_ID and CAST(Long_Call.Call_DateTime as Date)"+ 
          "BETWEEN @StartDate and @EndDate and Long_Call.Time_On_Call >= @Call_Length and Technician.Tech_ID = @TechID and Long_Call.Tracking_ID <> 2 and Long_Call.Tracking_ID <> 3;"; 
+0

你是指'@ techID = 0 AND Technician.Tech_ID> 0或者Technician.Tech_ID = @ TechID'? –

+0

我正在尋找基於TechID是否爲0的查詢。因此,如果它是0,我希望所有值大於它,如果它是!= 0,那麼我只需要該值。 –

回答

0

你可以嘗試這樣使用複合條件

WHERE (@techID = 0 AND Technician.Tech_ID > 0) 
OR Technician.Tech_ID = @TechID; 
+0

這正是我所需要的非常感謝! –