gpt4 book ai didi

sql - 循环查询表的每一列 - Oracle 数据库

转载 作者:行者123 更新时间:2023-12-02 05:36:12 27 4
gpt4 key购买 nike

我正在使用 oracle 数据库,我基本上需要做的是计算某个表中每列的 NULL 字段数。

类似的东西:

DECLARE
BlankCount number(20);
i number(2) := 1;

BEGIN
loop that would take each column individualy and exit after the last one
SELECT COUNT(*) INTO BlankCount FROM name_of_my_table
DBMS_OUTPUT.PUT_LINE('Column '||i||' has '||BlankCount||' empty cells');
i := i + 1;
END LOOP;
END;

我只是找不到任何可以完成循环部分的东西。如果我可以显示列名称(但这不是很重要)而不只是对它们进行编号(使用 i),那也很好。

谢谢!

最佳答案

像这样:

declare 

mytable varchar(32) := 'MY_TABLE';

cursor s1 (mytable varchar2) is
select column_name
from user_tab_columns
where table_name = mytable
and nullable = 'Y';

mycolumn varchar2(32);
query_str varchar2(100);
mycount number;

begin

open s1 (mytable);

loop
fetch s1 into mycolumn;
exit when s1%NOTFOUND;

query_str := 'select count(*) from ' || mytable || ' where ' || mycolumn || ' is null';

execute immediate query_str into mycount;

dbms_output.put_line('Column ' || mycolumn || ' has ' || mycount || ' null values');

end loop;
end;

关于sql - 循环查询表的每一列 - Oracle 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11642079/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com