oracle 抓取所有列,除了一些[重复]

vof42yt1  于 2023-11-17  发布在  Oracle
关注(0)|答案(2)|浏览(119)

此问题在此处已有答案

Can you SELECT everything, but 1 or 2 fields, without writer's cramp?(12个答案)
22天前关闭。
我有一个表,有很多列(大约50个)。我还有5个列,名字以expr_...开头。我想抓取除了expr_...列之外的所有列。我可以快速完成而不定义所有需要的列吗?
比如:

SELECT
 * FROM TABLE WHERE column_name NOT LIKE '%EXPR%'

字符串
而不是:

SELECT
a, b, c, d, e, f, g, h, .... FROM table```

gopyfrb3

gopyfrb31#

PL/SQL提供了各种选项,最明显的是返回ref游标或自定义类型的函数。
然而,如果你想留在纯SQL中,你可以写一些代码来为你写代码。
包含多个expr_列的示例表:

SQL> create table test as
  2  select empno id, ename name, mgr, hiredate,
  3    job expr_1, sal expr_2, deptno expr_3
  4  from emp where rownum <= 5;

Table created.

SQL> select * From test;

        ID NAME              MGR HIREDATE  EXPR_1        EXPR_2     EXPR_3
---------- ---------- ---------- --------- --------- ---------- ----------
      7369 SMITH            7902 17-DEC-80 CLERK            800         20
      7499 ALLEN            7698 20-FEB-81 SALESMAN        1600         30
      7521 WARD             7698 22-FEB-81 SALESMAN        1250         30
      7566 JONES            7839 02-APR-81 MANAGER         2975         20
      7654 MARTIN           7698 28-SEP-81 SALESMAN        1250         30

字符串
如果您查询user_tab_columns,将关键字与 desired 列的聚合列表连接起来(因为where子句消除了expr),结果就是您需要的:

SQL> select 'select ' ||
  2    listagg(column_name, ', ') within group (order by column_id) ||
  3    ' from test' result
  4  from user_Tab_columns
  5  where table_name = 'TEST'
  6    and column_name not like 'EXPR%';

RESULT
--------------------------------------------------------------------------------
select ID, NAME, MGR, HIREDATE from test


最后,复制/粘贴上面的语句并执行它:

SQL> select ID, NAME, MGR, HIREDATE from test;

        ID NAME              MGR HIREDATE
---------- ---------- ---------- ---------
      7369 SMITH            7902 17-DEC-80
      7499 ALLEN            7698 20-FEB-81
      7521 WARD             7698 22-FEB-81
      7566 JONES            7839 02-APR-81
      7654 MARTIN           7698 28-SEP-81

  • 将 * previous语句升级为create view,以便将其存储在数据库中,并从现在开始查询它(视图):
SQL> create or replace view v_test as select ID, NAME, MGR, HIREDATE from test;

View created.

SQL> select * From v_test where name = 'SMITH';

        ID NAME              MGR HIREDATE
---------- ---------- ---------- ---------
      7369 SMITH            7902 17-DEC-80

SQL>


下面是返回ref cursor* 选项的 * 函数(如果你认为你想要这样做;我不会,因为它不像以前发布的视图选项那样灵活):

SQL> create or replace function f_test
  2    return sys_refcursor
  3  is
  4    l_str varchar2(1000);
  5    rc    sys_refcursor;
  6  begin
  7    select 'select ' ||
  8      listagg(column_name, ', ') within group (order by column_id) ||
  9      ' from test'
 10    into l_str
 11    from user_Tab_columns
 12    where table_name = 'TEST'
 13      and column_name not like 'EXPR%';
 14
 15    open rc for l_str;
 16    return rc;
 17  end;
 18  /

Function created.


测试:

SQL> select f_test from dual;

F_TEST
--------------------
CURSOR STATEMENT : 1

CURSOR STATEMENT : 1

        ID NAME              MGR HIREDATE
---------- ---------- ---------- ---------
      7369 SMITH            7902 17-DEC-80
      7499 ALLEN            7698 20-FEB-81
      7521 WARD             7698 22-FEB-81
      7566 JONES            7839 02-APR-81
      7654 MARTIN           7698 28-SEP-81

SQL>

vu8f3i0k

vu8f3i0k2#

您可以为此创建一个过程,并立即执行或输出到控制台进行编辑,然后像这样执行,并将EXAMPLE替换为您的表名:

DECLARE
  v_sql VARCHAR2(1000);
BEGIN
  v_sql := 'SELECT ';
  FOR col IN (
    SELECT column_name
    FROM all_tab_columns
    WHERE table_name = 'EXAMPLE'
      AND column_name NOT LIKE 'expr\_%' ESCAPE '\'
  ) LOOP
    v_sql := v_sql || col.column_name || ', ';
  END LOOP;
  -- Remove trailing comma and space
  v_sql := SUBSTR(v_sql, 1, LENGTH(v_sql) - 2);
  v_sql := v_sql || ' FROM EXAMPLE';
  -- Print or execute the constructed SQL query
  DBMS_OUTPUT.PUT_LINE(v_sql); -- Use DBMS_OUTPUT for printing print
  -- EXECUTE IMMEDIATE v_sql; -- Uncomment to execute the query
END;

字符串
在执行任何动态创建的查询之前进行彻底的测试是非常重要的。
或者,只需这样做即可在键入时保存所有列名:

SELECT column_name
FROM all_tab_columns
WHERE table_name = 'EXAMPLE'
  AND column_name NOT LIKE 'expr\_%' ESCAPE '\';

相关问题