我想循环遍历oracle中的表,在每列中找到空值,然后更新这些值, 现在我试图只输出每列中的空值数,但我收到一条错误消息:
SET FEEDBACK OFF;
SET SERVEROUTPUT ON;
DECLARE
N_NULLS NUMBER;
BEGIN
FOR I IN (SELECT COLUMN_NAME
FROM ALL_TAB_COLUMNS
WHERE TABLE_NAME = 'my_table')
LOOP
SELECT SUM (CASE WHEN I IS NULL THEN 1 ELSE 0 END)
INTO N_NULLS
FROM my_table
WHERE COLUMN_ITEM IS NULL;
DBMS_OUTPUT.PUT_LINE ('NUMBER OF NULLS IN'|| I ||' IS ' || N_NULLS);
END LOOP;
END;
/
我收到以下错误:
Error at line 10
ORA-06550: line 9, column 23:
PLS-00382: expression is of wrong type
ORA-06550: line 9, column 8:
PL/SQL: Statement ignored
ORA-06550: line 10, column 29:
PLS-00382: expression is of wrong type
答案 0 :(得分:2)
@a_horse_with_no_name是正确的,您可以尝试以下代码在特定表的列中查找空行。
SET SERVEROUTPUT ON
DECLARE
l_sql VARCHAR2(4000);
n_nulls NUMBER;
BEGIN
FOR i IN (
SELECT
column_name
FROM
all_tab_columns
WHERE
table_name = 'my_table'
) LOOP
l_sql := ' SELECT SUM (CASE WHEN '
|| i.column_name
|| ' IS NULL THEN 1 ELSE 0 END)
FROM my_table
WHERE '
|| i.column_name
|| ' IS NULL';
EXECUTE IMMEDIATE l_sql INTO
n_nulls;
dbms_output.put_line('NUMBER OF NULLS IN'
|| i.column_name
|| ' IS '
|| n_nulls);
END LOOP;
END;
/
答案 1 :(得分:0)
(CASE WHEN I IS NULL THEN 1 ELSE 0 END)
应该是
(CASE WHEN I.COLUMN_NAME IS NULL THEN 1 ELSE 0 END)
同样的问题:
DBMS_OUTPUT.PUT_LINE ('NUMBER OF NULLS IN'|| I ||' IS ' || N_NULLS);