SQL/JDBC:对变量表名进行选择查询

发布于 2024-09-18 09:52:47 字数 527 浏览 6 评论 0原文

我正在使用 Oracle DB,我想编写一个 SQL 查询,然后可以使用 JDBC 调用该查询。我对 SQL 不太熟悉,所以如果有人可以帮助我,那就太好了!问题就在这里。我有一个表 MY_TABLE ,其中包含其他表的列表,我想仅保留非空表和名称以特定字符串开头的表。 我编写的查询如下:

 select TABLE_NAME 
 from MY_TABLE 
 where TABLE_NAME like '%myString%' 
 and (select count(*) from TABLE_NAME where rownum=1)<>0 
 order by TABLE_NAME;`

问题来自第二个 SELECT,但我不知道如何使用 TABLE_NAME 值。

有人有主意吗?

谢谢。


[从评论添加]

实际上,我需要测试 ALL_CATALOG 表中包含的 V$ 视图。但是,如果我能找到另一个表,其中也包含所有这些视图并且也包含 NUM_ROWS 列,那就太完美了!

I'm using Oracle DB and I would like to write a SQL query that I could then call with JDBC. I'm not very familiar with SQL so if someone can help me, that could be great ! Here is the problem. I have a table MY_TABLE wich contains a list of another tables, and I would like to keep only the nonempty tables and those that their names start by a particular string.
The query I wrote is the following :

 select TABLE_NAME 
 from MY_TABLE 
 where TABLE_NAME like '%myString%' 
 and (select count(*) from TABLE_NAME where rownum=1)<>0 
 order by TABLE_NAME;`

The problem comes from the second SELECT, but I don't know how can I do to use the TABLE_NAME value.

Does someone have an idea ?

Thanks.


[Added from comments]

Actually, I need to test the V$ views contained in the ALL_CATALOG table. But if I can find another table where all these views are contained too and with a NUM_ROWS column too, it would be perfect !

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

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

发布评论

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

评论(2

貪欢 2024-09-25 09:52:47

标准版本的 SQL 不允许您用变量值或占位符替换查询的“结构元素”,例如表名或列名。

有几种方法可以解决这个问题。

  1. 为 MY_TABLE 中列出的每个表名生成单独的 SQL 语句,并依次执行每个语句。蛮力,但有效。
  2. 直接查询系统目录。
  3. 研究是否有 JDBC 元数据操作允许您了解表中的行数,而无需与您正在使用的特定 DBMS 的系统目录相关联。

Standard versions of SQL do not allow you to replace 'structural elements' of the query, such as table name or column name, with variable values or place-holders.

There are a few ways to approach this.

  1. Generate a separate SQL statement for each table name listed in MY_TABLE, and execute each in turn. Brute force, but effective.
  2. Interrogate the system catalog directly.
  3. Investigate whether there are JDBC metadata operations that allow you to find out about the number of rows in a table without being tied to the system catalog of the specific DBMS you are using.
深巷少女 2024-09-25 09:52:47

可以使用oracle视图USER_TABLES吗?那么查询就会容易得多

select TABLE_NAME 
from USER_TABLES 
where TABLE_NAME like '%myString%' 
and Num_ROWS > 0
order by TABLE_NAME;`

Can you use oracle view USER_TABLES? then query will be much easier

select TABLE_NAME 
from USER_TABLES 
where TABLE_NAME like '%myString%' 
and Num_ROWS > 0
order by TABLE_NAME;`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文