将操作数作为sql参数传递

ase*_*eng 2 c# asp.net sql-server-2008

我目前正在开发一个以sql server 2008作为后端的asp.net应用程序.我想让用户能够在SQL语句中指定他们想要过滤的内容.在界面上,我给他们选择以下选项作为下拉列表:等于大于小于等

我想将此作为参数传递给要执行的sql查询.我怎样才能做到最好?

例如;

Select amount, deduction, month from loan where amount @operant 10000;
Run Code Online (Sandbox Code Playgroud)

@operand是上面下拉列表的返回值= < > <= >=

Aar*_*and 7

假设所有正整数<20亿,此解决方案避免了多个查询和动态SQL.OPTION (RECOMPILE)有助于阻止参数嗅探,但这可能不是必需的,具体取决于表的大小,参数设置和"针对ad hoc工作负载优化"设置.

WHERE [Amount] BETWEEN 
CASE WHEN @operand LIKE '<%' THEN 0
     WHEN @operand = '>' THEN @operant + 1
     ELSE @operant END
AND
CASE WHEN @operand LIKE '>%' THEN 2147483647
     WHEN @operand = '<' THEN @operant - 1
     ELSE @operant END
OPTION (RECOMPILE);
Run Code Online (Sandbox Code Playgroud)