Oracle SQL 查找所有表中的最高 ID

发布于 2024-11-27 22:52:42 字数 152 浏览 4 评论 0原文

我的每一张表都有一个“id”字段。考虑到表列表可能会发生变化,我需要能够找到所有表中的最高 ID。

有什么方法可以获取oracle数据库中的表列表,聚合它们的行(仅id),然后获取max()

PS 这是为了更新已经不正常的序列。

Every one of my tables has an "id" field. I need to be able to find the highest ID across all of them, given that the list of tables might change.

Is there any way to get the list of tables in an oracle database, aggregate their rows (only ids), and then get the max()?

P.S. This is for updating a sequence which has gone out of whack.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

榕城若虚 2024-12-04 22:52:42

下面是一些驱动数据字典的简单动态 SQL:

SQL> set serveroutput on
SQL> declare
  2      l_id pls_integer;
  3      max_id pls_integer;
  4      max_tab_name varchar2(30);
  5  begin
  6      max_id := 0;
  7      for r in ( select table_name
  8                 from user_tab_columns
  9                 where column_name = 'ID' )
 10      loop
 11          execute immediate 'select max(id) from '||r.table_name
 12              into l_id;
 13          if l_id > max_id
 14          then
 15              max_id := l_id;
 16              max_tab_name := r.table_name;
 17          end if;
 18      end loop;
 19      dbms_output.put_line('Highest score = '||max_id||' table='||max_tab_name);
 20  end;
 21  /
Highest score = 2010070705 table=SESSIONS

PL/SQL procedure successfully completed.

SQL>

如果序列服务表跨多个模式,则需要驱动 ALL_TAB_COLUMNS 并在查询中包含 OWNER。

Here is some simple dynamic SQL driving off the data dictionary:

SQL> set serveroutput on
SQL> declare
  2      l_id pls_integer;
  3      max_id pls_integer;
  4      max_tab_name varchar2(30);
  5  begin
  6      max_id := 0;
  7      for r in ( select table_name
  8                 from user_tab_columns
  9                 where column_name = 'ID' )
 10      loop
 11          execute immediate 'select max(id) from '||r.table_name
 12              into l_id;
 13          if l_id > max_id
 14          then
 15              max_id := l_id;
 16              max_tab_name := r.table_name;
 17          end if;
 18      end loop;
 19      dbms_output.put_line('Highest score = '||max_id||' table='||max_tab_name);
 20  end;
 21  /
Highest score = 2010070705 table=SESSIONS

PL/SQL procedure successfully completed.

SQL>

If the sequence services tables across several schemas, you will need to drive off ALL_TAB_COLUMNS and include OWNER in the query.

余罪 2024-12-04 22:52:42

这个怎么样?

 SELECT MAX(ID) 
 FROM
 (
    SELECT MAX(ID) AS ID FROM CUSTOMER
    UNION ALL 
    SELECT MAX(ID) AS ID FROM EMPLOYEE
    UNION ALL 
    SELECT MAX(ID) AS ID FROM MANAGER
 ); 

对需要搜索的所有表重复 UNION ALL。

How about this?

 SELECT MAX(ID) 
 FROM
 (
    SELECT MAX(ID) AS ID FROM CUSTOMER
    UNION ALL 
    SELECT MAX(ID) AS ID FROM EMPLOYEE
    UNION ALL 
    SELECT MAX(ID) AS ID FROM MANAGER
 ); 

Repeat the UNION ALL for all tables that you need to search from.

爱你不解释 2024-12-04 22:52:42

如何查询驱动 id 的 CURRVAL 序列?

如果您还需要找出 id 所在的表,那么构建一个新表来集中跟踪 id 并添加触发器以在插入时填充。

how about querying the sequence that drives the id's for CURRVAL...

if you need to find out also what table that id is in, then construct a new table to track the id centrally and add triggers to populate on insert.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文