如何短路空参数(SQL)

时间:2010-08-17 10:08:29

标签: c# sql

你好朋友我有以下查询,这打败了这个目的。

前一天我问过建议将电路参数归零,因为我得到了建议使用
(@firstYrPercent is null OR first_year_percent>=@firstYrPercent)这样拒绝null参数,但今天当我运行实际查询时,我认为上面提到的想法是没有用的。

我得到所有那些null的参数的sql异常,并且这些错误的要求值是这个Sql异常被捕获的:

  

“参数化查询”(@courseId int,@ passoutYear int,@ currentBacklog int,@ sex int,@ eG'需要参数@currentDegeePercentage,这是未提供的。“

以下是查询,请建议我另类:

string cmd = @"SELECT * FROM [tbl_students] WHERE course_id=@courseId 
          AND  branch_id IN(" + branchId + @") 
          AND (@firstYrPercent is null OR first_year_percent>=@firstYrPercent)
          AND (@secondYrpercent is null OR second_year_percent>=@secondYrPercent)
          AND (@thirdYrPercent is null OR third_year_percent>=@thirdYrPercent)
          AND (@finalYearpercent is null OR final_year_percent>=@finalYearpercent)
          AND (@currentDegeePercentage is null OR current_degree_percent>=@currentDegeePercentage)
          AND (@passoutYear is null OR passing_year>=@passoutYear) 
          AND (@currentBacklog is null OR current_backlog<=@currentBacklog)
          AND gender=@sex 
          AND (@eGap is null OR gapin_education<=@eGap)
          AND (@highSchoolPercentge is null OR highschool_percentage>=@highSchoolPercentge)
          AND (@higherSchoolPercentage is null OR ssc_percentage>=@higherSchoolPercentage)
          AND (@grauationPercentage is null OR graduation_percentage>=@grauationPercentage)
          AND (@diplomaPercentage is null OR diploma_percentage>=@diplomaPercentage)
          AND (@noOfAtkt is null OR number_of_ATKT<=@noOfAtkt)
          AND (@date is null OR DOB>=@date)";

2 个答案:

答案 0 :(得分:1)

您需要确保在正在执行的Command上定义@currentDegeePercentage参数 - 具体来说,如果要将该参数作为null传递,请确保将参数添加到命令中,其值为:{{1 }}

如果你没有将参数添加到Command.Parameters集合中,或者添加它但将值设置为null(而不是DBNull.Value),那么你将得到你看到的错误。

答案 1 :(得分:0)

问题不在于参数是null,而是缺少参数。检查拼写错误(它是“度”,而不是“degee”[原文])。