查询表和字段有没有注释

--定义一个数组类型msg_array,元素类型是varchar2,元素的长度不能超过30
create or replace type msg_array is table of varchar2(30);
--定义一个存储过程,查询表和字段是否有注释,参数tableNames的类型为上面定义的类型
create or replace procedure testGetComments(tableNames in msg_array) is
  --定义变量tab_comments,记录表是否有注释,有则大于0
  tab_comments int := 0;
  --定义变量col_comments,记录没有注释的字段总数
  col_comments int := 0;
  --定义变量str,输出信息
  str          varchar2(100) := '';
  --定义变量flag,是否需要打印信息,大于0则需要打印
  flag         int := 0;

  begin
  
    --输出共有几个表(特定环境才能输出)
    DBMS_OUTPUT.put_line('共有:' || tableNames.COUNT || '个表');
    if tableNames.COUNT != 0  --如果数组长度大于0
    then
      for i in 1..tableNames.COUNT  --循环数组
      loop

        --查询没有注释的字段总数
        select count(*) into col_comments
        from user_col_comments
        where Table_Name = tableNames(i)
          and comments is null;
        --查询表是否有注释
        select count(*) into tab_comments
        from user_tab_comments
        where Table_Name = tableNames(i)
          and comments is null;

        if tab_comments > 0
        then
          flag := 1;
          str := '没有注释,';
        end if;

        if col_comments > 0
        then
          flag := 1;
          str := str || col_comments || '个字段没有注释';
        end if;

        if flag > 0
        then
          DBMS_OUTPUT.put_line('表:' || tableNames(i) || ': ' || str);
        end if;

        str := '';
        flag := 0;
      end loop;
    end if;
  end;
  
  
--声明一个变量tableNames,用来放表名
declare tableNames msg_array :=msg_array('tableName1','tableName2');
begin
  --调用存储过程testGetComment
  testGetComments(tableNames);
end;

--删除存储过程
drop procedure testGetComments;
--删除自定义类型
drop type msg_array;

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值