当您不使用相关过滤器时,您将参数设置为 NULL,对吗?
所以你可以使用这样的查询:
WHERE isnull(@ts, [ts]) = [ts]
and isnull(@username, [username]) = [username]
and isnull(@ip, [ip]) = [ip]
and isnull(@my_category, [my_category]) = [my_category]
and isnull(@my_name, [my_name]) = [my_name]
and isnull(@nm1_name, [nm1_name]) = [nm1_name]
and isnull(@param, [param]) = [param]
and isnull(@short_descr, [short_descr]) = [short_descr]
option (recompile)
我主张在这种情况下使用选项(重新编译)
编辑:
用 '' 而不是 NULL
WHERE @ts in ('', [ts])
and @username in ('', [username])
and @ip in ('', [ip])
and @my_category in ('', [my_category])
and @my_name in ('', [my_name])
and @nm1_name in ('', [nm1_name])
and @param in ('', [param])
and @short_descr in ('', [short_descr])
(比每次都使用 OR 更具可读性,并且不需要重新编译*)
(*) 作为参数无论如何都不会被嗅探