简体   繁体   English

SQL Server 2008中子句的大小写查询

[英]Case Query in Where Clause SQL Server 2008

I have the following SQL Query ( I know its probably pretty ugly and inefficient). 我有以下SQL查询(我知道它可能很丑陋且效率低下)。 I need to be able to query where 我需要能够查询哪里

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

I'm at a bit of a loss as to how to accomplish this 我对如何做到这一点有点茫然

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;";

You can try like this using a composite condition 您可以使用复合条件尝试这样

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM