Oracle使用遊標查詢所有資料表備忘__Oracle

來源:互聯網
上載者:User

功能作用:應用對應的SQL語句,能方便快速的查詢Oracle資料庫指定使用者的所有使用者表說明,快速知道每個資料表是做什麼的,方便寫文檔和方案。

運行環境:搭建好Oracle資料庫,並使用PQ/SQL Developer軟體和指定的資料庫帳號密碼串連上您要查詢的資料庫。

詳細內容如下:

1、使用到的SQL查詢指令碼如下: ---------------------------------------------------------------------------------------------------------------
--Oracle使用遊標查詢所有資料表備忘

declare 
mytablename NVARCHAR2(200):=''; --定義要查詢的資料表名稱變數 
mytablecomment NVARCHAR2(200):=''; --定義要查詢的資料表注釋變數 
commentsql VARCHAR2(2000):=''; --定義要輸出的注釋結果變數  
cursor mycursor1 is select * from user_tab_comments  order by table_name;--定義遊標  
      
myrecord1 mycursor1%rowtype;  --定義遊標記錄類型   


begin   
open mycursor1;  --開啟遊標   
if mycursor1%isopen  then  --判斷開啟成功   
loop --迴圈擷取記錄集     
  fetch mycursor1 into myrecord1;
  
  if mycursor1%found then  --遊標的found屬性判斷是否有記錄  
  begin
  
    mytablename:=myrecord1.table_name;
    mytablecomment:=myrecord1.comments;
    
    --commentsql:='表名為    '||mytablename||'    注釋為    '||mytablecomment;
    commentsql:=mytablename||'    '||mytablecomment;
    dbms_output.put_line(commentsql);     
    
  end;
   
  else
  exit; --擷取遊標中的記錄      
  
  end if;


end loop;   
else     
dbms_output.put_line('遊標沒有開啟');   
end if; 


close mycursor1;

end;

---------------------------------------------------------------------------------------------------------------


2、PL/SQL Developer中的操作一:複製上面的查詢指令碼到【SQL】選項卡,並執行查詢,截圖如下:



3、PL/SQL Developer中的操作二:切換到【輸出】選項卡,查看結果,截圖如下:


---------------------------------------------------------------------------------------------------------------

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.