我有一个像
这样的字符串LEAST("col1", "col2") GREATEST("col1", "col2")
从这个字符串我想得到一个像
这样的结果字符串col1 IN LEAST(v_col1, v_col2) AND col2 IN GREATEST(v_col1, v_col2)
其中v_附加到列名称,它们表示预定义变量。 列数可以是动态的。 我尝试了太多选项,但我无法获得理想的结果。
请建议。
这是我尝试的几个选项,但似乎没有朝着正确的方向前进。
declare txt varchar2(1000);
anstxt varchar2(1000);
begin
txt := 'LEAST("col1","col2")';
select REGEXP_SUBSTR(TXT,'"([^"]*)"',1,1,'',1) into anstxt from dual;
DBMS_OUTPUT.PUT_LINE(anstxt);
end;
declare
v_Var5 varchar2(50);
v_Var6 varchar2(1000);
v_Var7 varchar2(10);
v_Var8 varchar2(10);
CURSOR c3 IS
SELECT column_expression FROM all_ind_expressions WHERE table_owner = 'schemaname' AND table_name = 'mytable';
names_t c3%ROWTYPE;
TYPE names_ntt IS TABLE OF names_t%TYPE; -- must use type
l_names names_ntt;
BEGIN
v_Var7 := ' IN (';
v_Var8 := ');';
--FOR i in c3
OPEN c3;
FETCH c3 BULK COLLECT INTO l_names;
CLOSE c3;
FOR indx IN 1..l_names.COUNT LOOP
IF(l_names.COUNT > 1) THEN
v_Var6 := v_Var6 || REPLACE(l_names(indx).column_expression, '"', '');
DBMS_OUTPUT.PUT_LINE(l_names(indx).column_expression);
ELSE
v_Var6 := REPLACE(l_names(indx).column_expression, '"', '');
END IF;
END LOOP;
v_Var6 := v_Var7 || REPLACE(v_Var6, ' ', ' AND ') || v_Var8;
--DBMS_OUTPUT.PUT_LINE(v_Var5);
DBMS_OUTPUT.PUT_LINE(v_Var6);
END;
问候。
答案 0 :(得分:1)
经过这么多试验,我终于得到了我想要的东西。 我正在分享此论坛的参考资料。
DECLARE
v_Var5 varchar2(1000);
v_Var6 varchar2(1000);
v_Var7 varchar2(10);
v_Var8 varchar2(10);
v_Var9 varchar2(1000);
v_Counter NUMBER;
CURSOR c3 IS
SELECT column_expression FROM all_ind_expressions WHERE table_owner = 'myschema' AND table_name = 'mytable';
names_t c3%ROWTYPE;
TYPE names_ntt IS TABLE OF names_t%TYPE; -- must use type
l_names names_ntt;
BEGIN
v_Counter := 1;
v_Var7 := ' IN (';
v_Var8 := ');';
OPEN c3;
FETCH c3 BULK COLLECT INTO l_names;
CLOSE c3;
FOR indx IN 1..l_names.COUNT
LOOP
--v_Counter := l_names.COUNT;
IF(l_names.COUNT > 1) THEN
v_Var6 := REPLACE(l_names(indx).column_expression, '"', '');
FOR i IN 1..l_names.COUNT LOOP
v_Var6 := REPLACE(v_Var6, REGEXP_SUBSTR(l_names(i).column_expression,'"([^"]*)"',1,i,'',1),'v_' || REGEXP_SUBSTR(l_names(i).column_expression,'"([^"]*)"',1,i,'',1));
END LOOP;
v_Var9 := REGEXP_SUBSTR(l_names(indx).column_expression,'"([^"]*)"',1,indx,'',1) || ' IN (';
CASE
WHEN indx < l_names.COUNT THEN
v_Var5 := v_Var5 || v_Var9 || v_Var6 || ') AND ';
WHEN indx = l_names.COUNT THEN
v_Var5 := v_Var5 || v_Var9 || v_Var6 || ');';
END CASE;
ELSE
v_Var6 := REPLACE(l_names(indx).column_expression, '"', '');
v_Var6 := REPLACE(v_Var6, REGEXP_SUBSTR(l_names(indx).column_expression,'"([^"]*)"',1,indx,'',1),'v_' || REGEXP_SUBSTR(l_names(indx).column_expression,'"([^"]*)"',1,indx,'',1));
v_Var9 := REGEXP_SUBSTR(l_names(indx).column_expression,'"([^"]*)"',1,indx,'',1) || ' IN ';
v_Var5 := v_Var5 || v_Var9 || v_Var6;
END IF;
END LOOP;
--DBMS_OUTPUT.PUT_LINE('v_Var9' || CHR(13) || v_Var9);
--DBMS_OUTPUT.PUT_LINE('v_Var6' || CHR(13) ||v_Var6);
DBMS_OUTPUT.PUT_LINE('v_Var5' || CHR(13) ||v_Var5);
END;