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.