Stored Procedure for SQL Server

Source: Internet
Author: User

The Real-time Data Validity determination rule of INSQL requires a flexible definition. I use the standard SQL expression to define the rule in the string mode, and parse and output the result through the stored procedure to determine the result. For specific code, see the following content;

 

Code
-- ===================================================== ======
-- Author: SUIFEI
-- Create date: 2008-8-5
-- Description: Determination rule
-- ===================================================== ======
Alter procedure [dbo]. [MP_JC_DATAISVALID]
@ EXPS varchar (4096), -- Expression
@ RESULT int = null OUT -- output parameter: 1 indicates valid, 0 indicates invalid
AS
BEGIN
/*
Call Demo:
Expression: [YT601_PLAN_YP] BETWEEN 6500 AND 7000, [WQ4_EXPORT_TEMP]> = 30
Explanation: The tag data of YT601_PLAN_YP must be between 6500 and 7000, and the data of WQ4_EXPORT_TEMP must be greater than or equal to 20.

DECLARE @ RESULT INT
EXEC MP_JC_DATAISVALID '[YT601_PLAN_YP] BETWEEN 6500 AND 7000, [WQ4_EXPORT_TEMP]> = 30', @ RESULT OUT
SELECT @ RESULT
*/
/*
Defined data structure: tag Name, parameter code, expression (standard SQL expression)
TAGNAME, PARAMETERCODE, EXPRESSION
A_1 1001 NULL
A2 1001 [A1]> 100, [A2] BETWEEN 98 AND 102
A3 1001 NULL
*/
SET NOCOUNT ON
-- Enable direct debugging in this sentence: 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 table is a real-time data table
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.