简体   繁体   English

SQL:我可以将“ <”运算符的字符串值转换为where子句中的运算符吗?

[英]SQL: Can I convert a string value of a “<” operator into an operator in a where clause?

I am storing an operator in a string column for two rows of a table (">", "<=") 我将运算符存储在表的两行的字符串列中(“>”,“ <=”)

I am joining the table with another table and want to make the where clause as dynamic as possible. 我正在将该表与另一个表连接,并希望使where子句尽可能动态。

I was wondering if it's possible to convert the string value operator into an actual operator for this line of SQL code: 我想知道是否可以将这行SQL代码的字符串值运算符转换为实际的运算符:

ABS(DATEDIFF(dd,Table2.DUE_DT,GETDATE())) > 120

VS VS

ABS(DATEDIFF(dd,Table2.DUE_DT,GETDATE())) <= 120

The operator will change depending on matching columns in the row. 操作员将根据行中匹配的列进行更改。 Is it possible to change the operator based of the string value containing the correct operator? 是否可以根据包含正确运算符的字符串值来更改运算符? If so, how can this be done? 如果是这样,该怎么办?

Below are the two rows from Table1 以下是表1中的两行

NEFL_TYPE   GRGR_ID     NEFL_KEY    NEFL_VALUE  NEFL_COLUMN
"PDRU"      "2600"      "PD"         "RV"        ">"
"PDRU"      "2600"      "RV"         "PD"        "<="

This is the snippet of code I use: 这是我使用的代码片段:

INNER JOIN
    Table1
ON
    Table2.STATUS = Table1.NEFL_KEY
AND
    Table1.NEFL_TYPE = 'PDRU'
WHERE
    Table1.GRGR_ID = '2600'
AND
    ABS(DATEDIFF(dd, Table2.DUE_DT,GETDATE())) > 120

So Table2.STATUS should determine which operator to use in the NEFL_COLUMN 因此,Table2.STATUS应该确定在NEFL_COLUMN中使用哪个运算符

I don't think there's an easy way to do what you want for a general case, not even with dynamic queries or client-side generated queries, as the comparison is per row and performance would be an issue with dynamic queries. 我认为没有一种简单的方法可以完成一般情况下的操作,即使对于动态查询或客户端生成的查询也是如此,因为比较是按行进行的,而性能对于动态查询将是一个问题。

I see 2 ways to solve the particular example case, though: 我看到了两种解决特定示例情况的方法:

a) Make 2 separate queries and do a UNION on them a)进行2个单独的查询并对其进行UNION

SELECT...
INNER JOIN
    Table1
ON
    Table2.STATUS = Table1.NEFL_KEY
AND
    Table1.NEFL_TYPE = 'PDRU'
WHERE
    Table1.GRGR_ID = '2600'
AND
    ABS(DATEDIFF(dd, Table2.DUE_DT,GETDATE())) > 120 AND Table1.NEFL_COLUMN = ">"
UNION
SELECT...
INNER JOIN
    Table1
ON
    Table2.STATUS = Table1.NEFL_KEY
AND
    Table1.NEFL_TYPE = 'PDRU'
WHERE
    Table1.GRGR_ID = '2600'
AND
    ABS(DATEDIFF(dd, Table2.DUE_DT,GETDATE())) <= 120 AND Table1.NEFL_COLUMN = "<="

b) Use 2 range columns instead of an "operator" column b)使用2个范围列而不是“运算符”列

NEFL_TYPE   GRGR_ID     NEFL_KEY    NEFL_VALUE  NEFL_COLUMN   START    END
"PDRU"      "2600"      "PD"         "RV"        ">"           121    9999
"PDRU"      "2600"      "RV"         "PD"        "<="           0      120
--------------------------------------------------------------------------
INNER JOIN
    Table1
ON
    Table2.STATUS = Table1.NEFL_KEY
AND
    Table1.NEFL_TYPE = 'PDRU'
WHERE
    Table1.GRGR_ID = '2600'
AND
    ABS(DATEDIFF(dd, Table2.DUE_DT,GETDATE())) BETWEEN Table1.START AND Table1.END

But none of them are as clean or elegant as I think you would like 但是它们都不像我想的那么干净或优雅

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

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