代码之家  ›  专栏  ›  技术社区  ›  Matt M

在Oracle SQL中,“%type”是什么意思?

  •  33
  • Matt M  · 技术社区  · 15 年前

    我第一次接触甲骨文和蟾蜍(我知道SSMS)。在更新过程中,我在一个输入参数旁边遇到了这个“%type”,我不知道它是什么或它的含义。我在谷歌上找到了与“%rowtype”相关的链接。是相同的东西还是完全不同的东西?

    如果这是含糊不清的,我道歉。一如既往,谢谢你的帮助。

    3 回复  |  直到 15 年前
        1
  •  55
  •   OMG Ponies    15 年前

    PostgreSQL

    %TYPE

    DECLARE v_id ORDERS.ORDER_ID%TYPE
    

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

    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

    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;
    /
    
        2
  •  0
  •   Martin Evans    9 年前

    %Type

     Declare
                L_num Number(5,2) not null default 3.21;
                L_num_Test L_num%Type:=1.123;
    

    L_num_Test

        3
  •  -2
  •   zloctb    10 年前

    set serveroutput on
    DECLARE
      var1  table_a.id%TYPE;
      var2  table_a.value%TYPE;
      var3_row table_a%ROWTYPE;
    BEGIN
      SELECT id,value
        INTO var1, var2
        FROM table_a
      WHERE id= 8 AND ROWNUM<=1; 
    
      SELECT id+100,value INTO var3_row from table_A where rownum=1;
    
      INSERT INTO table_a VALUES var3_row;
    
      dbms_output.put_line('this is a test');
      dbms_output.put_line(var1);
      dbms_output.put_line(var2);
      NULL;  -- do something useful here
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        NULL;  -- do something appropriate here
      WHEN TOO_MANY_ROWS THEN
        NULL;  -- do something appropriate here
    END;
    /