需求:现在需要判断TK_SURVEY 的字段scope(开放的部门)是否允许某个部门的人访问,需要判断
scope字段是否包含该部门,涉及到字符串的分割,可以采用自定义函数的方式来解决。
--CLOB 要分割的字符串
--p_sep分隔符,默认值,
CREATE OR REPLACE FUNCTION split (p_list CLOB, p_sep VARCHAR2 := ',')
RETURN tabletype
PIPELINED
IS
l_idx PLS_INTEGER;
v_list VARCHAR2 (32676) := p_list;
BEGIN
LOOP
l_idx := INSTR (v_list, p_sep);
IF l_idx > 0
THEN
PIPE ROW (SUBSTR (v_list, 1, l_idx - 1));
v_list := SUBSTR (v_list, l_idx + LENGTH (p_sep));
ELSE
PIPE ROW (v_list);
EXIT;
END IF;
END LOOP;
END;
--分割字符后取出指定序号的字串
CREATE OR REPLACE FUNCTION splitstr (str IN CLOB,
i IN NUMBER := 0,
sep IN VARCHAR2 := ','
)
RETURN VARCHAR2
IS
--t_i NUMBER;
t_count NUMBER;
t_str VARCHAR2 (4000);
BEGIN
IF i = 0
THEN
t_str := str;
ELSIF INSTR (str, sep) = 0
THEN
t_str := sep;
ELSE
SELECT COUNT ( * )
INTO t_count
FROM table (split (str, sep));
IF i <= t_count
THEN
SELECT str
INTO t_str
FROM (SELECT ROWNUM AS item, COLUMN_VALUE AS str
FROM table (split (str, sep)))
WHERE item = i;
END IF;
END IF;
RETURN t_str;
END;
在sql中的运用:
select *
from TK_SURVEY s
where exists
(select 1
from (select COLUMN_VALUE scope from table(split('A', ','))) t
where t.scope = s.scope) and status = 'OPEN'
scope字段是否包含该部门,涉及到字符串的分割,可以采用自定义函数的方式来解决。
--CLOB 要分割的字符串
--p_sep分隔符,默认值,
CREATE OR REPLACE FUNCTION split (p_list CLOB, p_sep VARCHAR2 := ',')
RETURN tabletype
PIPELINED
IS
l_idx PLS_INTEGER;
v_list VARCHAR2 (32676) := p_list;
BEGIN
LOOP
l_idx := INSTR (v_list, p_sep);
IF l_idx > 0
THEN
PIPE ROW (SUBSTR (v_list, 1, l_idx - 1));
v_list := SUBSTR (v_list, l_idx + LENGTH (p_sep));
ELSE
PIPE ROW (v_list);
EXIT;
END IF;
END LOOP;
END;
--分割字符后取出指定序号的字串
CREATE OR REPLACE FUNCTION splitstr (str IN CLOB,
i IN NUMBER := 0,
sep IN VARCHAR2 := ','
)
RETURN VARCHAR2
IS
--t_i NUMBER;
t_count NUMBER;
t_str VARCHAR2 (4000);
BEGIN
IF i = 0
THEN
t_str := str;
ELSIF INSTR (str, sep) = 0
THEN
t_str := sep;
ELSE
SELECT COUNT ( * )
INTO t_count
FROM table (split (str, sep));
IF i <= t_count
THEN
SELECT str
INTO t_str
FROM (SELECT ROWNUM AS item, COLUMN_VALUE AS str
FROM table (split (str, sep)))
WHERE item = i;
END IF;
END IF;
RETURN t_str;
END;
在sql中的运用:
select *
from TK_SURVEY s
where exists
(select 1
from (select COLUMN_VALUE scope from table(split('A', ','))) t
where t.scope = s.scope) and status = 'OPEN'