Oracle 用過程獲得表的欄位

來源:互聯網
上載者:User

//在預存程序裡面,我們常常將從一個表裡面查詢到的欄位資料複製給一寫變數,  
//然後調用包裡面的函數dbms_output.put_line()來將其列印出來;  
//下面是一個執行個體:  
procedure show_employee(empno_in in emp.empno%type)    
as    
  v_sign number;    
  v_empno emp.empno%type;    
  v_ename emp.ename%type;    
  v_deptno emp.deptno%type;    
begin  
     select 1 into v_sign    
     from dual    
     where exists(select count(*) from emp where empno=empno_in);    
     if v_sign=1 then    
        select empno,ename,deptno into v_empno,v_ename,v_deptno    
        from emp where empno=empno_in;    
        dbms_output.put_line('information of'||empno_in||' are:');    
        dbms_output.put_line('empno:'||v_empno||',ename:'||v_ename||',deptno:'||v_deptno);    
     end if;    
     exception    
     when others then    
          dbms_output.put_line('no data found');    
end show_employee;  
SQL> exec show_employee('7900');  
information of7900 are:  
empno:7900,ename:JAMES,deptno:30  
//  
//但是我們怎麼樣將一個表的結構用預存程序倒出來呢?  
//就是寫一個過程,參數是傳進去表的名字,然後列印出表的所有欄位;  
create or replace procedure pro_display_col(table_name_in varchar2)  
as 
  type arry_type is table of varchar2(30) index by pls_integer;  
  v_cols arry_type;  
begin  
     select tc.column_name bulk collect  
            into v_cols  
     from user_tab_columns tc  
     where tc.table_name=upper(table_name_in);  
       
     for i in 1..v_cols.count  
     loop  
         dbms_output.put_line(v_cols(i));  
     end loop;  
       
     exception  
     when others then  
          dbms_output.put_line(sqlerrm);  
end pro_display_col;  
/  
//解析:  
//此處定義了一個巢狀表格資料類型,通俗意義上叫做數組,沒有最大限制,  
//並定義了一個接收傳回值的字串類型,30個位元組,接受15個字元  
//我們也可以將其定義為接受30個字元的巢狀表格:  
//type array_type is a table of varchar2(30 char) index by pls_integer;  
//index by pls_integer是為了獲得column_name資料時,不需要用v_cols.extend  
//來為擷取的資料顯示開闢空間,如果沒有index by pls_integer,  
//那麼我們每次擷取資料時都要為其顯示的開闢空間,v_cols.extend;  
//關於bulk collect  
//採用bulk collect可以將查詢結果一次性地載入到collections中。  
//而不是通過cursor一條一條地處理。  
SQL> exec pro_display_col('emp');  
EMPNO  
ENAME  
JOB  
MGR  
HIREDATE  
SAL  
COMM  
DEPTNO  
//  
SQL> exec pro_display_col('dept');  
DEPTNO  
DNAME  
LOC  
//  
//其實最簡單的擷取表欄位的方法:  
SQL> col data_type format a13;  
SQL> select column_name,data_type,data_length,data_precision,data_scale  
  2  from user_tab_columns  
  3  where table_name=upper('emp');  
COLUMN_NAME                    DATA_TYPE     DATA_LENGTH DATA_PRECISION DATA_SCALE  
------------------------------ ------------- ----------- -------------- ----------  
EMPNO                          NUMBER                 22              4          0  
ENAME                          VARCHAR2               10                  
JOB                            VARCHAR2                9                  
MGR                            NUMBER                 22              4          0  
HIREDATE                       DATE                    7                  
SAL                            NUMBER                 22              7          2  
COMM                           NUMBER                 22              7          2  
DEPTNO                         NUMBER                 22              2          0  
// 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.