Usually, in the database we will use a field to store a plurality of different states,
For example, a query: SELECT * FROM STUDENTS WHERE STATUS IN ( 'IN', 'OUT', 'LEAVE')
On the UI we may use a CheckBoxList to represent the three states, selected as IN conditions.
FUNCTION created in a database, it is the role string delimiter split into a press table.
CREATE FUNCTION [dbo].[F_STRING_SPLIT]
(@text VARCHAR(8000),
@delimiter VARCHAR(20) = ' ')
RETURNS @Strings TABLE
(
ITEM_VALUE VARCHAR(8000)
)
AS
BEGIN
DECLARE @index INT
SET @index = -1
WHILE (LEN(@text) > 0)
BEGIN
SET @index = CHARINDEX(@delimiter,@text)
IF (@index = 0)
AND (LEN(@text) > 0)
BEGIN
INSERT INTO @Strings
VALUES (@text)
BREAK
END
IF (@index > 1)
BEGIN
INSERT INTO @Strings
VALUES (LEFT(@text,@index - 1))
SET @text = RIGHT(@text,(LEN(@text) - @index))
END
ELSE
SET @text = RIGHT(@text,(LEN(@text) - @index))
END
RETURN
END
Once you've created, you can use this to query FUNCTION uncertain conditions.
For example, a stored procedure SEARCH_ALL_STUDENTS
EXEC SEARCH_ALL_STUDENTS 'IN,OUT'
CREATE PROC SEARCH_ALL_STUDENTS
@p_status NVARCHAR(50)
AS
SELECT *
FROM STUDENTS
WHERE STATUS IN (
SELECT ITEM_VALUE FROM dbo.F_STRING_SPLIT(@p_status, ',')
)
SQL is assembled with respect, this is still regarded as relatively safe.
For each state as a condition, the maintenance of such a little higher, lower performance.
Reproduced in: https: //www.cnblogs.com/lezyyang/archive/2008/11/13/1332781.html