通过在oracle中传递值从表中获取列名
我希望oracle中的查询通过传递值从表中获取列名。 这意味着在大多数情况下,我们都是这样编写查询的-select*from table,其中column='value'。但在我的情况下,我不知道列名 谁能给我推荐一下吗。通过在oracle中传递值从表中获取列名,oracle,oracle11g,Oracle,Oracle11g,我希望oracle中的查询通过传递值从表中获取列名。 这意味着在大多数情况下,我们都是这样编写查询的-select*from table,其中column='value'。但在我的情况下,我不知道列名 谁能给我推荐一下吗。 提前感谢…您可以尝试构建一个动态查询来检查数据库的所有表 select table_name from user_Tables where table_name = 'bogus'; 设置: create table tab1 ( v1 varchar2(100), n1
提前感谢…您可以尝试构建一个动态查询来检查数据库的所有表
select table_name from user_Tables where table_name = 'bogus';
设置:
create table tab1 ( v1 varchar2(100), n1 number, v1b varchar2(100));
create table tab2 ( v2 varchar2(100), n2 number, v2b varchar2(100));
create table tab3 ( v3 varchar2(100), n3 number, v3b varchar2(100));
insert into tab1 values ('Maria', 1, 'aa');
insert into tab1 values ('xx', 2, 'bb');
insert into tab2 values ('yy', 3, 'Maria');
insert into tab2 values ('zz', 3, 'cc');
insert into tab3 values ('WW', 4, 'DD');
select 'select table_name,
matches from (' || listagg(statement, ' UNION ALL ') within group (order by table_name) || ')
where matches > 0'
from (
select 'select ''' || table_name ||
''' as TABLE_NAME, count(1) as MATCHES from ' || table_name || ' WHERE ' ||
listagg(column_name || ' = ''Maria''', ' OR ') within group (order by column_name) as statement,
table_name
from user_tab_columns col
where data_type = 'VARCHAR2'
group by table_name
)
构建动态查询:
create table tab1 ( v1 varchar2(100), n1 number, v1b varchar2(100));
create table tab2 ( v2 varchar2(100), n2 number, v2b varchar2(100));
create table tab3 ( v3 varchar2(100), n3 number, v3b varchar2(100));
insert into tab1 values ('Maria', 1, 'aa');
insert into tab1 values ('xx', 2, 'bb');
insert into tab2 values ('yy', 3, 'Maria');
insert into tab2 values ('zz', 3, 'cc');
insert into tab3 values ('WW', 4, 'DD');
select 'select table_name,
matches from (' || listagg(statement, ' UNION ALL ') within group (order by table_name) || ')
where matches > 0'
from (
select 'select ''' || table_name ||
''' as TABLE_NAME, count(1) as MATCHES from ' || table_name || ' WHERE ' ||
listagg(column_name || ' = ''Maria''', ' OR ') within group (order by column_name) as statement,
table_name
from user_tab_columns col
where data_type = 'VARCHAR2'
group by table_name
)
这将返回一个查询,您可以运行该查询来检查所有表;在我的示例中,这将生成查询(未格式化):
运行此查询将提供:
TABL MATCHES
---- ----------
TAB1 1
TAB2 1
请注意,我使用了USER\u TAB\u COLUMNS
,因此只在登录模式的表中搜索;如果要在不同的模式中搜索,可以使用所有选项卡列
或DBA选项卡列
,具体取决于您的需要和用户的权限;再看看
也可以考虑<代码> uSeriTabyStudio将获得表和视图的CULL;如果您想将搜索限制在表中,您可以通过
表名,表名和所有者将用户表列(所有表,DBA表)连接到用户表(所有表,DBA表),如果您决定使用所有或DBA表:
SQL> create view vTab1 as select * from tab1;
View created.
SQL> select count(1)
2 from user_tab_columns
3 where table_name = 'VTAB1';
COUNT(1)
----------
3
SQL> select count(1)
2 from user_tab_columns
3 inner join user_tables using(table_name)
4 where table_name = 'VTAB1';
COUNT(1)
----------
0
SQL>
从用户表中选择*从用户表中选择表名,其中表名='school\u master';我有一个类似“Maria”的值,我想从oracle数据库中搜索列名和表名。是否要查找包含该值的任何列(在任何表中?)的名称?如果是这样,你的问题就不清楚了;“Maria”是包含在一个或多个未知列中的值吗?你怎么不知道要查找的列的名称?这是家庭作业问题还是什么?我有一个像“Maria”这样的值,我想从oracle数据库中搜索列名和表名OP要求一个给定值的列名。您的答案只会给出他们已经拥有的表名。