我想在Table行中搜索32列,在每一列中搜索特定值和计数外观,是否可以查询整行而不在oracle中键入每个列名?
答案 0 :(得分:1)
是的,这是可能的,但解决方案并不快,并且需要有关xquery的基本知识。
准备测试表
create table test_objects as select * from user_objects where rownum < 100;
select * from
xmltable('for $row in ora:view("test_objects")/ROW return count($row/*[contains(text(),$TO_SEARCH)])' passing 'N' as "TO_SEARCH" columns cnt number path '.');
查询计数包含&#34; N&#34;的列。价值。
如果您更改了一个查询,我们可以看到哪些列包含搜索值
select * from
xmltable('for $row in ora:view("test_objects")/ROW return <ROW><CNT>{count($row/*[contains(text(),$TO_SEARCH)])}</CNT><COLUMNS>{string-join($row/*[contains(text(),$TO_SEARCH)]/name(),", " )}</COLUMNS></ROW>' passing 'N' as "TO_SEARCH"
columns
cnt number path 'CNT'
, list_of_column varchar2(4000) path 'COLUMNS'
);
答案 1 :(得分:0)
DBA_TAB_COLUMNS
,如果没有USER_TAB_COLUMNS
,下面可以在SQL Plus或您选择的SQL客户端上运行:)
VARIABLE cur REFCURSOR;
DECLARE
v_chr VARCHAR2(4000);
v_chr_tablename VARCHAR2(30) := 'EMP';
v_chr_searchterm VARCHAR2(100) := 'KING';
BEGIN
SELECT 'SELECT * FROM ' || v_chr_tablename || ' where '
|| RTRIM ( XMLAGG (XMLELEMENT (E, 'TO_CHAR('
||column_name
|| ')', ' || ').EXTRACT ( '//text()')
ORDER BY column_name).getStringVal (), ' || ')
||' like ''%' || v_chr_searchterm || '%'''
INTO v_chr
FROM DBA_TAB_COLUMNS
WHERE table_name = v_chr_tablename
AND owner = 'APPS';
OPEN :cur FOR v_chr;
END;
/
print cur;