我的表中包含许多列,其中包含一些列值,作为特殊字符。
我查看了使用regex_like的示例,但我不想要列的值。我只需要包含值的列名作为特殊字符。
let requiredFilters = Set(filters)
let supplierWithEveryRequiredFilter = suppliers.filter { Set($0.category).isSupersetOf(filters) }
查询上述数据应该返回column1和column3。有人可以帮助查询将输出作为column1和column3吗?
感谢您的帮助。
答案 0 :(得分:1)
如果要识别包含逗号的列,可以执行以下操作:
select 'column1'
from t
where column1 like '%,%'
group by 1
union all
select 'column2'
from t
where column2 like '%,%'
group by 1
union all
. . .
这将返回包含逗号的列名列表。
您也可以在select
:
select ((case when sum(case when column1 like '%,%' then 1 else 0 end) > 0 then 'column1; ' end) ||
(case when sum(case when column2 like '%,%' then 1 else 0 end) > 0 then 'column2; ' end) ||
. . .
)
from t;
您可以使用all_tab_cols
以及SQL查询或电子表格构建其中一个查询。
答案 1 :(得分:1)
我不知道结果将如何使用,我会考虑动态构建查询(因为列数很大)。
你可以使用这样的函数:
CREATE OR REPLACE FUNCTION get_columns(
p_table_name IN VARCHAR2,
p_substring IN VARCHAR2)
RETURN VARCHAR2
IS
stmt VARCHAR2(32767) := 'SELECT ';
result VARCHAR2(32767);
CURSOR cols IS
SELECT column_name
FROM user_tab_columns
WHERE table_name = upper(p_table_name)
AND data_type = 'VARCHAR2';
BEGIN
FOR i IN cols LOOP
stmt := stmt || 'DECODE(SUM(CASE WHEN ' || i.column_name|| ' LIKE ''%' ||
p_substring || '%'' THEN 1 ELSE 0 END),0,NULL,''' || i.column_name ||
','') || ';
END LOOP;
stmt := SUBSTR(stmt, 1, LENGTH(stmt) - 3) || ' FROM your_table';
EXECUTE immediate(stmt) INTO result;
RETURN result;
END;
/
你可以像这样打电话:
SELECT get_columns('your_table', ',')
FROM dual;
只是一个想法。如果该方法适合您,您可能希望进行一些更改。
答案 2 :(得分:0)
使用@Carlos De Leon的方法使用函数动态生成查询,并使用xmlquery在SQL中执行此操作:
CREATE OR REPLACE FUNCTION fn_return_columns (p_owner IN VARCHAR2,
p_table_name IN VARCHAR2)
RETURN CLOB AS
v_dyn_query clob;
v_return clob;
BEGIN
WITH t AS (
SELECT owner,
table_name,
('SELECT listagg(column_names, '', '') WITHIN GROUP (ORDER BY column_names) column_names
FROM (SELECT column_names, count_vals
FROM (SELECT ') sql1,
decode(rn, 1, null, lpad(' ', 22)) || 'SUM(regexp_count(' || column_name || ', ''[^[:alnum:]]'')) ' || column_name || comma sql2,
' FROM ' || owner || '.' || table_name || ')
UNPIVOT (
count_vals for column_names in (' sql3,
decode(rn, 1, null, lpad(' ', 49)) || column_name || comma sql4,
lpad(' ', 49) || ')
)
)
WHERE count_vals > 0' sql5,
rn
FROM (SELECT c.owner,
c.table_name,
c.column_name,
nvl2(lead(c.column_id) over(PARTITION BY c.owner, c.table_name ORDER BY c.column_id ASC), ',' || chr(10), chr(10)) comma,
row_number() over (order by c.COLUMN_ID) rn
FROM dba_tab_columns c
WHERE c.owner = upper(p_owner)
AND table_name = upper(p_table_name)
)
)
SELECT dbms_xmlgen.convert((
xmlquery('//text()' passing
xmlconcat(
xmlelement("Resultado", xmlattributes(1 as "rn"), sql1),
xmlelement("Resultado", xmlattributes(2 as "rn"),
xmlquery('//text()' passing xmlagg(xmlelement("sql2", sql2 || ' ') ORDER BY rn) returning content)
),
xmlelement("Resultado", xmlattributes(3 as "rn"), sql3),
xmlelement("Resultado", xmlattributes(4 as "rn"),
xmlquery('//text()' passing xmlagg(xmlelement("sql4", sql4 || ' ') ORDER BY rn) returning content)
),
xmlelement("Resultado", xmlattributes(5 as "rn"), sql5)
)
returning content
)).getclobval(), 1) as dyn_query
INTO v_dyn_query
FROM t
GROUP BY sql1,
sql3,
sql5,
owner,
table_name;
EXECUTE IMMEDIATE v_dyn_query INTO v_return;
RETURN v_return;
END;
示例运行:
SQL> create table t1 (column1 varchar2(100),
2 column2 varchar2(100),
3 column3 varchar2(100),
4 column4 varchar2(100),
5 column5 varchar2(100));
Table created
SQL> insert into t1 values ('abc,', 'pqr', 'def,', 'xxx', 'yyy');
1 row inserted
SQL> insert into t1 values ('xyz', 'mno', 'ghi,', 'xxx', 'z,zz');
1 row inserted
SQL> commit;
Commit complete
SQL>
SQL> CREATE OR REPLACE FUNCTION fn_return_columns (p_owner IN VARCHAR2,
2 p_table_name IN VARCHAR2)
3 RETURN CLOB AS
4 v_dyn_query clob;
5 v_return clob;
6 BEGIN
7 WITH t AS (
8 SELECT owner,
9 table_name,
10 ('SELECT listagg(column_names, '', '') WITHIN GROUP (ORDER BY column_names) column_names
11 FROM (SELECT column_names, count_vals
12 FROM (SELECT ') sql1,
13 decode(rn, 1, null, lpad(' ', 22)) || 'SUM(regexp_count(' || column_name || ', ''[^[:alnum:]]'')) ' || column_name || comma sql2,
14 ' FROM ' || owner || '.' || table_name || ')
15 UNPIVOT (
16 count_vals for column_names in (' sql3,
17 decode(rn, 1, null, lpad(' ', 49)) || column_name || comma sql4,
18 lpad(' ', 49) || ')
19 )
20 )
21 WHERE count_vals > 0' sql5,
22 rn
23 FROM (SELECT c.owner,
24 c.table_name,
25 c.column_name,
26 nvl2(lead(c.column_id) over(PARTITION BY c.owner, c.table_name ORDER BY c.column_id ASC), ',' || chr(10), chr(10)) comma,
27 row_number() over (order by c.COLUMN_ID) rn
28 FROM dba_tab_columns c
29 WHERE c.owner = upper(p_owner)
30 AND table_name = upper(p_table_name)
31 )
32 )
33 SELECT dbms_xmlgen.convert((
34 xmlquery('//text()' passing
35 xmlconcat(
36 xmlelement("Resultado", xmlattributes(1 as "rn"), sql1),
37 xmlelement("Resultado", xmlattributes(2 as "rn"),
38 xmlquery('//text()' passing xmlagg(xmlelement("sql2", sql2 || ' ') ORDER BY rn) returning content)
39 ),
40 xmlelement("Resultado", xmlattributes(3 as "rn"), sql3),
41 xmlelement("Resultado", xmlattributes(4 as "rn"),
42 xmlquery('//text()' passing xmlagg(xmlelement("sql4", sql4 || ' ') ORDER BY rn) returning content)
43 ),
44 xmlelement("Resultado", xmlattributes(5 as "rn"), sql5)
45 )
46 returning content
47 )).getclobval(), 1) as dyn_query
48 INTO v_dyn_query
49 FROM t
50 GROUP BY sql1,
51 sql3,
52 sql5,
53 owner,
54 table_name;
55 EXECUTE IMMEDIATE v_dyn_query INTO v_return;
56
57 RETURN v_return;
58 END;
59 /
Function created
SQL> SET LONG 99999
SQL> SELECT fn_return_columns(user, 'T1') from dual;
FN_RETURN_COLUMNS(USER,'T1')
--------------------------------------------------------------------------------
COLUMN1, COLUMN3, COLUMN5