简体   繁体   English

SQL-从X WHERE中选择*(如果值为数字,则为…)

[英]SQL - select * from X WHERE (if value is numeric, else …)

I would need to make a DB select that will behave differently when number or text is entered, just different condition should be used. 我需要做出一个DB选择,当输入数字或文本时,它的行为将有所不同,只是应使用不同的条件。 I think it should be done but not quite sure with the syntax (MSSQL). 我认为应该这样做,但对语法(MSSQL)不太确定。 Thank you 谢谢

I would need: 我会需要:

SELECT * 
  FROM X 
 WHERE (IF value passed is numeric = "ID" ELSE "NAME") = Value //ID or Name are columns

Based on your example: 根据您的示例:

SELECT * 
  FROM TABLE X 
 WHERE (CASE 
          WHEN ISNUMBERIC(@parameter_value) = 1 THEN x.id 
          ELSE x.name
        END) = @parameter_value

...would work, I'd like to stress that the approach is not sargable --it won't perform as well as it should. ...会工作,我想强调一下这种方法不是可靠的 -效果不佳。

If dealing with a single parameter, using an IF ELSE would perform better: 如果处理单个参数,则使用IF ELSE会更好:

IF ISNUMERIC(@parameter_value) = 1
BEGIN

 SELECT x.*
   FROM TABLE x
  WHERE x.id = @parameter_value

END
ELSE
BEGIN

 SELECT x.*
   FROM TABLE x
  WHERE x.name = @parameter_value

END

The other alternative (which should definitely be considered if dealing with more than one filter criteria) is to use dynamic SQL. 另一种选择(如果处理多个过滤器标准,则绝对应考虑使用)是动态SQL。 I recommend reading The curse and blessings of dynamic SQL before looking at this SQL Server 2005+ example: 我建议在查看此SQL Server 2005+示例之前,先阅读动态SQL的诅咒和祝福

DECLARE @paramater_value NVARCHAR(255)

DECLARE @SQL NVARCHAR(4000)
    SET @SQL = 'SELECT x.*
                  FROM TABLE x '

    SET @SQL = @SQL + CASE 
                        WHEN ISNUMBERIC(@paramater_value) = 1 THEN 
                          ' WHERE x.id = @paramater_value '
                        ELSE 
                          ' WHERE x.name = @paramater_value '
                      END   

BEGIN

  EXEC sp_executesql @SQL,
                     N'@paramater_value NVARCHAR(255)',
                     @paramater_value

END

I'm assuming there is code somewhere that constructs this query and sends it to the database? 我假设某处有代码构造此查询并将其发送到数据库? Why not have that code determine whether the value is numeric and generate the appropriate query? 为什么不用该代码确定值是否为数字并生成适当的查询? It keeps the query cleaner and has the advantage of being compatible with virtually every RDBMS on the planet. 它使查询保持整洁,并具有与地球上几乎所有RDBMS兼容的优点。

This is one way, but your question is a wee bit vague 这是一种方法,但是您的问题有点含糊

WHERE
    CASE ISNUMERIC(Something) WHEN 1 THEN x ELSE Y END

two ways: 两种方式:

using or 使用或

select ..
from tablename
where (something = clause1 and isnumeric(value) = 1)
or (something = clause2 and isnumeric(value) <> 1)

or maybe using union 或使用工会

select ..
from tablename
where something = clause1 and isnumeric(value) = 1
union all
select ..
from tablename
where something = clause1 and isnumeric(value) <> 1

Simple - just retrieve the numeric values: 简单-只需检索数值:

SELECT [columns] FROM X WHERE ISNUMERIC(value) = 1

More complex: 更复杂:

SELECT [columns] FROM X WHERE CASE ISNUMERIC(value) WHEN 1 THEN [something] ELSE [something_else] END

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

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