作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经搜索并只发现了这个问题:
Loop through columns SQL
它在某些方面很相似,但不涉及 PL/SQL 和 Oracle 数据库,因此我在问新问题。
我有一张 table 。 2000 行和 600 列。有一些列在每行中仅包含 NULL。我想要做的是编写一个 PL/SQL 过程来从表中删除这些列。
所以我遇到了一个问题,我想在 all_tab_columns View 的帮助下遍历 PL/SQL 中的列。你可以在下面看到我的代码(我的表名是 PreparedDocumentFeaturesValues):
PROCEDURE dropNullColumns AS
l_query VARCHAR2(10000);
all_row_count NUMBER;
null_row_count NUMBER;
BEGIN
SELECT count(*)
INTO all_row_count
FROM PreparedDocumentFeaturesValues;
FOR columnItem IN (SELECT column_name
FROM all_tab_columns
WHERE TABLE_NAME = UPPER('PreparedDocumentFeaturesValues'))
LOOP
SELECT count(*)
INTO null_row_count
FROM PreparedDocumentFeaturesValues
WHERE columnItem.column_name IS NULL;
IF all_row_count=null_row_count THEN
l_query := 'ALTER TABLE PreparedDocumentFeaturesValues DROP COLUMN ' || columnItem.column_name;
EXECUTE IMMEDIATE l_query;
END IF;
END LOOP;
END;
SELECT count(*)
INTO null_row_count
FROM PreparedDocumentFeaturesValues
WHERE columnItem.column_name IS NULL;
最佳答案
我相信你想要
execute immediate 'SELECT count(*) FROM PreparedDocumentFeaturesValues WHERE '|| columnItem.column_name||' IS NULL' into null_row_count;
DVLP SQL>create table foo as select * from dba_objects where rownum < 10;
Table created.
DVLP SQL>update foo set status = null;
9 rows updated.
DVLP SQL>
DVLP SQL>declare
2 tab_name constant varchar2(32) := 'foo';
3 not_null number;
4 begin
5 for x in (select column_name from all_tab_columns where table_name = upper(tab_name)) loop
6 dbms_output.put('Checking '||tab_name||'.'||x.column_name);
7 begin
8 execute immediate 'select 1 from (select 1 from '||tab_name||
9 ' where '||x.column_name||' is not null) where rownum = 1' into not_null;
10 dbms_output.put_line('.');
11 exception when NO_DATA_FOUND then
12 dbms_output.put_line('...all null.');
13 end;
14 end loop;
15 end;
16 /
Checking foo.OWNER.
Checking foo.OBJECT_NAME.
Checking foo.SUBOBJECT_NAME...all null.
Checking foo.OBJECT_ID.
Checking foo.DATA_OBJECT_ID.
Checking foo.OBJECT_TYPE.
Checking foo.CREATED.
Checking foo.LAST_DDL_TIME.
Checking foo.TIMESTAMP.
Checking foo.STATUS...all null.
Checking foo.TEMPORARY.
Checking foo.GENERATED.
Checking foo.SECONDARY.
Checking foo.NAMESPACE.
Checking foo.EDITION_NAME...all null.
关于oracle - 如何使用 PL/SQL 遍历列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7825167/
我是一名优秀的程序员,十分优秀!