如何在 varchar 列中搜索 varchar 变量? PL SQL
How do you search varchar variables in varchar column ? PL SQL
我想知道数据库中是否存在 Oracle 11g 中的某些给定职责。
假设我们有 10 个职责,我想搜索它们是否存在于包含 60 个职责的数据库中。
如何在 varchar 列中搜索 varchar 变量?
对于单个变量应该很容易..
declare
var varchar(100);
begin
var:='&var';
if var is not null
then
select responsibility_name FROM apps.fnd_responsibility_tl where responsibility_name _ ?
dbms_output.put_line(var||' EXISTS');
else
dbms_output.put_line(var||' ERROR');
end if;
end;
/
有两种方法可以做到这一点,简单的方法,以及您在问题中使用 PL/SQL 请求的方法。
首先,使用 SQL 的简单方法。这使用取自 a blog post by Arunkumar Ramamoorthy 的技巧,使用 REGEXP_SUBSTR 和 CONNECT BY 将逗号分隔列表转换为行。
WITH search_criteria AS (
SELECT TRIM(REGEXP_SUBSTR('&search_criteria','[^,]+', 1, LEVEL)) AS search_responsibility
FROM dual
CONNECT BY
TRIM(REGEXP_SUBSTR('&search_criteria', '[^,]+', 1, LEVEL)) IS NOT NULL
)
SELECT s.search_responsibility,
CASE WHEN r.responsibility_name IS NULL THEN 'ERROR' ELSE 'EXISTS' END AS status
FROM search_criteria s
LEFT OUTER JOIN
apps.fnd_responsibility_tl r ON (r.responsibility_name = s.search_responsibility);
其次,使用 PL/SQL 和 DBMS_OUTPUT 的更复杂的方法。这使用与 SQL 版本相同的原理,但使用游标检查每个责任是否存在,输出 EXISTS 或 ERROR 消息。
CREATE OR REPLACE PROCEDURE check_responsibilities (p_search_criteria IN VARCHAR2)
IS
CURSOR c_responsibilities (p_responsibilities IN VARCHAR2)
IS
SELECT TRIM(REGEXP_SUBSTR(p_responsibilities,'[^,]+', 1, LEVEL)) AS search_responsibility
FROM dual
CONNECT BY
REGEXP_SUBSTR(p_responsibilities, '[^,]+', 1, LEVEL) IS NOT NULL;
lv_temp apps.fnd_responsibility_tl.responsibility_name%TYPE;
BEGIN
FOR c IN c_responsibilities(p_search_criteria)
LOOP
BEGIN
SELECT r.responsibility_name
INTO lv_temp
FROM apps.fnd_responsibility_tl r
WHERE r.responsibility_name = c.search_responsibility;
dbms_output.put_line(c.search_responsibility || ' EXISTS');
EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line(c.search_responsibility || ' ERROR');
END;
END LOOP;
END;
我想知道数据库中是否存在 Oracle 11g 中的某些给定职责。
假设我们有 10 个职责,我想搜索它们是否存在于包含 60 个职责的数据库中。
如何在 varchar 列中搜索 varchar 变量?
对于单个变量应该很容易..
declare
var varchar(100);
begin
var:='&var';
if var is not null
then
select responsibility_name FROM apps.fnd_responsibility_tl where responsibility_name _ ?
dbms_output.put_line(var||' EXISTS');
else
dbms_output.put_line(var||' ERROR');
end if;
end;
/
有两种方法可以做到这一点,简单的方法,以及您在问题中使用 PL/SQL 请求的方法。
首先,使用 SQL 的简单方法。这使用取自 a blog post by Arunkumar Ramamoorthy 的技巧,使用 REGEXP_SUBSTR 和 CONNECT BY 将逗号分隔列表转换为行。
WITH search_criteria AS (
SELECT TRIM(REGEXP_SUBSTR('&search_criteria','[^,]+', 1, LEVEL)) AS search_responsibility
FROM dual
CONNECT BY
TRIM(REGEXP_SUBSTR('&search_criteria', '[^,]+', 1, LEVEL)) IS NOT NULL
)
SELECT s.search_responsibility,
CASE WHEN r.responsibility_name IS NULL THEN 'ERROR' ELSE 'EXISTS' END AS status
FROM search_criteria s
LEFT OUTER JOIN
apps.fnd_responsibility_tl r ON (r.responsibility_name = s.search_responsibility);
其次,使用 PL/SQL 和 DBMS_OUTPUT 的更复杂的方法。这使用与 SQL 版本相同的原理,但使用游标检查每个责任是否存在,输出 EXISTS 或 ERROR 消息。
CREATE OR REPLACE PROCEDURE check_responsibilities (p_search_criteria IN VARCHAR2)
IS
CURSOR c_responsibilities (p_responsibilities IN VARCHAR2)
IS
SELECT TRIM(REGEXP_SUBSTR(p_responsibilities,'[^,]+', 1, LEVEL)) AS search_responsibility
FROM dual
CONNECT BY
REGEXP_SUBSTR(p_responsibilities, '[^,]+', 1, LEVEL) IS NOT NULL;
lv_temp apps.fnd_responsibility_tl.responsibility_name%TYPE;
BEGIN
FOR c IN c_responsibilities(p_search_criteria)
LOOP
BEGIN
SELECT r.responsibility_name
INTO lv_temp
FROM apps.fnd_responsibility_tl r
WHERE r.responsibility_name = c.search_responsibility;
dbms_output.put_line(c.search_responsibility || ' EXISTS');
EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line(c.search_responsibility || ' ERROR');
END;
END LOOP;
END;