INSQL的实时数据有效性判定规则;需要一个比较灵活的定义,我采用了SQL的标准表达式用字符串方式定义到规则内,通过存储过程解析输出断定结果。具体的代码请看下面的内容;
-- =============================================
-- 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
本文转自suifei博客园博客,原文链接:http://www.cnblogs.com/Chinasf/archive/2008/09/08/1286768.html,如需转载请自行联系原作者