本文介绍了遍历表列的列表,并对它们应用查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有mytable表,其中包含列:id,值,描述....

We have our table mytable, that contains columns: id, value, description....

现在,我想将此SQL查询应用于每列:

Now I want to apply this sql query to each column:

select distinct [column] from mytable; 

有没有办法做到这一点?

Is there a way to do this?

nb.我想提供要遍历的列列表,而不是遍历表中的每一列.

nb. I want to provide the list of columns to loop through, rather than looping through every column in the table.

推荐答案

declare
  l_tab_name varchar2(32) := 'MY_TABLE';
begin
  for c1 in (select t.column_name from user_tab_columns t where t.table_name = l_tab_name)
    loop
      execute immediate 'select distinct ' || c1.column_name || ' from ' || l_tab_name;
    end loop;
end;

这篇关于遍历表列的列表,并对它们应用查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 21:13