请用sql查询纠正我

时间:2010-08-17 04:44:00

标签: sql

这个查询出了什么问题请在这个语法上纠正我的错误请纠正我这个如何连接这个

string cmd = "SELECT * 
                FROM [tbl_students] 
               WHERE course_id=@courseId 
                 AND branch_id IN ("+branchId+") 
                 AND (@passoutYear is null or passout_year>=@passoutYear) 
                 AND (@currentBacklog is null or current_backlog<=@currentBacklog)
                 AND gender=@sex 
                 AND (@eGap is null or gapin_education<=@egap)
                 AND (@firstYrPercent is null or first_yea_percent>=@firstYrPercent
                 AND (@secondYrpercent is null or second_year_percent>=@secondYrPercent)
                 AND (@thirdYrPercent is null or third_year_percent>=@thirdYrPercent)
                 AND (@finalYrpercent is null or final_year_percent>=@finalYrpercent)
                 AND (@currentDegreePercentage is null current_degree_percent>=@currentDegreePercentage)
                 AND (@highSchoolPercentage is null high_school_percentage>=@highSchoolPercentage)
                 AND (@higherSchoolPercentage is null higher_school_percentage>=@higherSchoolPercentage)
                 AND (@graduationPercent is null graduation_percent>=@graduationPercentage)
                 AND (@diplomaPercentage is null diploma_percentage>=@diplomaPercenage)
                 AND (@noOfAtkt is null or no_of_atkt<=@noOfAtkt)
                 AND (@date is null or date_of_birth>=@date)";

2 个答案:

答案 0 :(得分:2)

首先,如果branchId是一个字符串,那么你需要这些单引号:

branch_id IN('"+branchId+"') AND 

虽然我不明白为什么它没有像这样参数化:

branch_id = @branchId AND 

其次,您可能在此字段名称中拼错了“年份”一词:

AND  (@firstYrPercent is null or first_year_percent>=@firstYrPercent

最后,你在这里错过了一些OR:

AND  (@currentDegreePercentage is null OR current_degree_percent>=@currentDegreePercentage)
AND  (@highSchoolPercentage is null OR high_school_percentage>=@highSchoolPercentage)
AND  (@higherSchoolPercentage is null OR higher_school_percentage>=@higherSchoolPercentage)
AND  (@graduationPercent is null OR graduation_percent>=@graduationPercentage)
AND  (@diplomaPercentage is null OR diploma_percentage>=@diplomaPercenage)

答案 1 :(得分:0)

我想我明白了。

在尝试编译代码时是否收到错误,而不是运行SQL查询?

我敢打赌你正在使用C#,你应该在“-symbol之前用@ -symbol做多行字符串。例如:

string blabla = @"
     hello
     there
";

当然,当您在branchId

之后打开字符串时,您也需要它