INSQL的即時資料有效性判定規則;需要一個比較靈活的定義,我採用了SQL的標準運算式用字串方式定義到規則內,通過預存程序解析輸出斷定結果。具體的代碼請看下面的內容;
Code
-- =============================================
-- Author: SUIFEI
-- Create date: 2008-8-5
-- Description: 判定規則
-- =============================================
ALTER PROCEDURE [dbo].[MP_JC_DATAISVALID]
@EXPS varchar(4096),--運算式
@RESULT int = null OUT--傳出參數:1表示有效,0表示無效
AS
BEGIN
/*
調用示範:
運算式:[YT601_PLAN_YP] BETWEEN 6500 AND 7000, [WQ4_EXPORT_TEMP]>=30
解釋:YT601_PLAN_YP 的tag點的資料必須小於或等於 6500 和7000之間,並且WQ4_EXPORT_TEMP點的資料大於或等於20
DECLARE @RESULT INT
EXEC MP_JC_DATAISVALID '[YT601_PLAN_YP] BETWEEN 6500 AND 7000, [WQ4_EXPORT_TEMP]>=30',@RESULT OUT
SELECT @RESULT
*/
/*
定義的資料結構: tag名,參數代碼,運算式(採用標準sql運算式)
TAGNAME , PARAMETERCODE , EXPRESSION
A1 1001 NULL
A2 1001 [A1]>100,[A2] BETWEEN 98 AND 102
A3 1001 NULL
*/
SET NOCOUNT ON
--開啟本句直接調試: SET @EXPS = '[YT601_PLAN_YP] BETWEEN 6500 AND 7000, [WQ4_EXPORT_TEMP]>=30'
DECLARE @EXP VARCHAR(2048)
DECLARE @R INT
DECLARE @EXPSQL VARCHAR(4096)
SET @R = 0
SET @EXPS = LTRIM(RTRIM(@EXPS))
SET @EXPSQL = ''
IF ( RIGHT(RTRIM(@EXPS),1)<>',') SET @EXPS = @EXPS + ','
select '['+TAGNAME+']' AS TAGNAME,VALUE INTO #tmp FROM LIVE --LIVE表是即時資料表
UPDATE #TMP SET @EXPS =REPLACE(@EXPS, TAGNAME, VALUE)
WHERE CHARINDEX(TAGNAME, @EXPS)<>0
DROP TABLE #TMP
CREATE TABLE #EXPRESSION ( RESULT INT )
WHILE( CHARINDEX(',',@EXPS)<>0 )
BEGIN
SET @EXP = SUBSTRING(@EXPS,1,CHARINDEX(',',@EXPS) -1)
SET @EXPS = SUBSTRING(@EXPS,CHARINDEX(',',@EXPS) +1,LEN(@EXPS))
SET @EXPSQL = 'INSERT INTO #EXPRESSION '+
'SELECT (CASE WHEN (' +
@EXP +
') THEN 1 ELSE 0 END) '
EXEC(@EXPSQL)
PRINT @EXPSQL
END
SELECT @RESULT = MIN(RESULT) FROM #EXPRESSION
DROP TABLE #EXPRESSION
PRINT @RESULT
RETURN @RESULT;
END