What does “%Type” mean in Oracle sql?

前端 未结 3 903
有刺的猬
有刺的猬 2021-01-30 13:22

I\'m getting my first experience with Oracle and TOAD (I know SSMS). I came across this \"%Type\" next to an input parameter in an update procedure and I have no idea what it is

3条回答
  •  暖寄归人
    2021-01-30 13:51

    Oracle (and PostgreSQL) have:

    • %TYPE
    • %ROWTYPE

    %TYPE

    %TYPE is used to declare variables with relation to the data type of a column in an existing table:

    DECLARE v_id ORDERS.ORDER_ID%TYPE
    

    The benefit here is that if the data type changes, the variable data type stays in sync.

    Reference: http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14261/fundamentals.htm#i6080

    %ROWTYPE

    This is used in cursors to declare a single variable to contain a single record from the resultset of a cursor or table without needing to specify individual variables (and their data types). Ex:

    DECLARE
      CURSOR c1 IS
         SELECT last_name, salary, hire_date, job_id 
           FROM employees 
          WHERE employee_id = 120;
    
      -- declare record variable that represents a row fetched from the employees table
      employee_rec c1%ROWTYPE; 
    
    BEGIN
     -- open the explicit cursor and use it to fetch data into employee_rec
     OPEN c1;
     FETCH c1 INTO employee_rec;
     DBMS_OUTPUT.PUT_LINE('Employee name: ' || employee_rec.last_name);
    END;
    /
    

提交回复
热议问题