Oracle中使用遊標獲取指定數據表的所有字段名對應的字符串

操作步驟:打開PLSQL Developer後,直接執行下面的語句就可以出來


--Oracle中使用遊標獲取指定數據表的所有字段名對應的字符串

declare
mytablename VARCHAR(255):='STAFFDOC'; --定義要查詢的數據表名變量,STAFFDOC爲我測試用的數據表名,請修改成您的數據庫中的對應數據表名字
mystring NVARCHAR2(4000):=''; --定義要輸出的字符串變量   

cursor mycursor is --定義遊標          
select * from all_tab_columns where TABLE_NAME=mytablename ORDER BY column_id;  
myrecord mycursor%rowtype;  --定義遊標記錄類型   
Counter int :=0;   
begin   
open mycursor;  --打開遊標   
if mycursor%isopen  then  --判斷打開成功   
loop --循環獲取記錄集     
fetch mycursor into myrecord; --獲取遊標中的記錄         

if mycursor%found then  --遊標的found屬性判斷是否有記錄  
begin
if length(mystring)>0 then
mystring:=mystring||','||myrecord.column_name;
else
mystring:=myrecord.column_name;
end if;
end;

else            
exit;
end if;
   
end loop;   
else     
dbms_output.put_line('遊標沒有打開');   
end if;  
dbms_output.put_line(mystring);    
close mycursor;
end;

發佈了300 篇原創文章 · 獲贊 99 · 訪問量 154萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章