T10=] 使用来自字段中字符串的整个 WHERE 条件 table

T-SQL use an entire WHRE condition coming from a string in a field table

我想执行 运行,其中使用 T-SQL 过滤两个 table。 我只能用T-SQL。 我对同一个字段有几个条件,具体取决于传感器类型。

而不是使用仅适用于两种情况的类似方法:

SELECT
i.sensorSN,
i.measurename,
i.measureval,
r.alarmlow,
r.alarmhigh
FROM i
JOIN r
ON (i.sensorSN = r.sensorSN)
WHERE measurevalue > r.alarmhigh OR measurevalue < r.alarmlow

我想从引用 table 中的 table 字段(例如,表达式)获取整个 where 条件,该引用包含所有运算符和占位符 (%) 作为字符串对于比较 table 字段,保存如下:

"%>100 OR %<10"

然后,表达式可以这样计算:

SELECT
i.sensorSN,
i.measurename,
i.measureval,
r.expression
FROM i
JOIN r
ON (i.sensorSN = r.sensorSN)
WHERE «a way to EVALUATE r.expression CONDITION»

几乎可以吗?你能指导我吗?

参考 table 是一个简单的 JSON 文件,如下所示:

[{
    "deviceserialnumber": "MyDotnetDevice",
    "sensorserialnumber": "MyDotnetDeviceHum",
    "_comment": "for instance... % is a placeholder, 2 cond OR ...",
    "expression": "%>90 OR %<30"
},
{
    "deviceserialnumber": "MyDotnetDevice",
    "sensorserialnumber": "MyDotnetDevicePressure",
    "_comment": "for instance... % is a placeholder, 2 cond AND ...",
    "expression": "%<60 AND %>50"
}, ...
]

因此您可以使用 SP_EXECUTESQL (https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-executesql-transact-sql?view=sql-server-ver15) 创建动态查询,您可以创建您喜欢的查询。在你的情况下,你需要一次评估多个传感器,所以我想这不是一个查询的事情,而是所有传感器内的迭代,所以它会是这样的:

DECLARE @IndexNum AS INT
DECLARE @MaxCount AS INT
DECLARE @QueryText AS nvarchar(max)
DECLARE @WhereText AS nvarchar(max)
DECLARE @SQLToExec AS nvarchar(max)
DECLARE @SensorSerial AS nvarchar(max)

/*Declaring the Results Table*/
CREATE TABLE #ResultsTable (
    sensorSN nvarchar(max)
   ,measurename nvarchar(max)
   ,measureval DOUBLE PRECISION
   ,expression nvarchar(max)
)

/*Creating a list of sensors to evaluate*/
SELECT *
    ,ROW_NUMBER()  OVER(ORDER BY sensorSN ASC) 'RowIndex'
    INTO #SensorList FROM (
        SELECT DISTINCT sensorSN FROM i
        ) A
ORDER BY RowIndex ASC

/*Declaring the standard query*/
SET @QueryText = N'SELECT i.sensorSN,
                         i.measurename,
                         i.measureval,
                         r.expression
                         FROM i
                         JOIN r
                         ON (i.sensorSN = r.sensorSN)
                         WHERE i.sensorSN = '
/*Setting Max Value for the Loop*/
SET @MaxCount = (SELECT MAX(RowIndex) AS MaxVal FROM #SensorList)
/*Initializing loop index*/
SET @IndexNum = 0
WHILE @IndexNum < @MaxCount
BEGIN
    /*Setting the sensor we want to work with*/
    SET @SensorSerial = (SELECT sensorSN FROM #SensorList WHERE RowIndex = @IndexNum)
    /*Printing the sensor to know where are you working at the momment*/
    PRINT N'Working on sensor: ' + @SensorSerial
    /*Creating the dynamic Where clause*/
    SET @WhereText = (SELECT REPLACE(expression,'%','r.expression') AS 'newExpression' FROM refTable WHERE sensorserialnumber = @SensorSerial)      /*Creating the insert into the results table for the sensor evaluation*/
    SET @SQLToExec = N'INSERT INTO #ResultsTable
                           SELECT * FROM (' + @QueryText + '''' + @SensorSerial + ''' AND (' + @WhereText + '))'
    /*Executing the dynamic query*/
    EXEC sp_executesql @SQLToExec
    /*Incrementing the index*/
    SET @IndexNum = @IndexNum + 1
END

/*Showing all the results*/
SELECT * FROM #ResultsTable

/*Deleting temp tables*/
DROP TABLE #SensorList
DROP TABLE #ResultsTable

我试着在代码上添加了尽可能多的注释,所以你会知道每一步都在做什么。

请使用您真正需要从中获取表达式的查询更新查询

更新 添加了括号以避免 Where 子句冲突