2015-08-31 65 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

这正是我所需要的非常感谢! –