如何使用 PL/SQL 循环遍历列
我查了一下,只发现这个问题: 循环遍历列 SQL 它在某些方面很相似,但不涉及 PL/SQL 和 Oracle 数据库,因此我提出新问题。
我有一张桌子。 2000 行和 600 列。有些列每行中仅包含 NULL。我想要做的是编写一个 PL/SQL 过程来从表中删除这些列。 所以我遇到了一个问题,我想在 all_tab_columns 视图的帮助下循环遍历 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;
具有字符类型作为column_name并且null_row_count始终等于0。
我很确定,有人知道我如何应对这个问题(通过改进上面的代码,或者也许还有其他方法可以做到这一点?> 预先感谢您的帮助。
I have searched through and found only this problem:
Loop through columns SQL
it's similar in some ways, but doesn't concern PL/SQL and Oracle Database, therefore I'm Asking new Question.
I have a table with ca. 2000 rows and 600 columns. There are some columns comprised only NULLs in each row. What I want to do is to write a PL/SQL Procedure to remove those columns from the table.
So I have met a problem, I wanted to loop through columns in PL/SQL with help of all_tab_columns view. You can see my code below (my table name is 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;
The problem is that statement:
SELECT count(*)
INTO null_row_count
FROM PreparedDocumentFeaturesValues
WHERE columnItem.column_name IS NULL;
has character type as a column_name and null_row_count always equals 0.
I'm pretty sure, here is somebody who know how can I cope with this problem (by improving the code above, or maybe is there any other way to do such a thing?>
Thank you in advance for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信您想要
这是一个更完整的答案,它比您上面的答案更高效。
I believe you want
Here's a more complete answer that will be more performant than what you have above.
由于您在编译时不知道列名称,因此您的查询也需要使用动态 SQL。 这还有一个
您可能还可以通过仅计算非 NULL 行来稍微简化逻辑,
好处是,如果您碰巧在任何列上有任何索引,则循环中的查询可能会使用这些索引。并且查询可以在找到单个非 NULL 值时立即停止,而不是扫描整个表。
Since you don't know the column name at compile time, your query would need to use dynamic SQL as well. Something like
You could probably also simplify the logic a bit by just counting the non-NULL rows
This also has the benefit that if you happen to have any indexes on any columns, the queries in the loop could potentially use those. And the query can stop as soon as it finds a single not-NULL value rather than scanning the entire table.