“%Type”是什么意思? Oracle sql 中的意思?

发布于 2024-09-24 12:41:14 字数 163 浏览 3 评论 0原文

我第一次体验 Oracle 和 TOAD(我知道 SSMS)。我在更新过程中的输入参数旁边遇到了这个“%Type”,但我不知道它是什么或意味着什么。我在 Google 上找到了与“%Rowtype”相关的链接。是相同的东西还是完全不同的东西?

如果这含糊不清,我深表歉意。一如既往,感谢您的帮助。

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 or what it means. I found links on Google related to "%Rowtype". Is the same thing or something entirely different?

If this is vague, I apologize. As always, thanks for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甩你一脸翔 2024-10-01 12:41:14

Oracle(和 PostgreSQL)具有:

  • %TYPE
  • %ROWTYPE

%TYPE

%TYPE 用于声明与现有表中列的数据类型相关的变量:

DECLARE v_id ORDERS.ORDER_ID%TYPE

这样做的好处是,如果数据类型发生变化,变量数据类型保持同步。

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

%ROWTYPE

这在游标中用于声明单个变量以包含游标或表结果集中的单个记录,而无需指定单个变量(及其数据类型) 。前任:

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;
/

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;
/
就像说晚安 2024-10-01 12:41:14

除了 OMG Ponies 指出的目的之外,%TYPE 还用于继承先前声明的变量所使用的相同数据类型。

语法是:

 DECLARE
            L_num NUMBER(5,2) NOT NULL default 3.21;
            L_num_Test L_num%TYPE := 1.123;

因此不需要声明第二个变量的数据类型,即L_num_Test

如果有人需要有关此主题的进一步说明,请发表评论。

参考:https://docs.oracle.com/ cd/B19306_01/appdev.102/b14261/fundamentals.htm#BEIIGBBF

Apart from the purpose pointed by OMG Ponies, %TYPE is also used for inheriting the same data type used by a previously declared variable.

The syntax is :

 DECLARE
            L_num NUMBER(5,2) NOT NULL default 3.21;
            L_num_Test L_num%TYPE := 1.123;

So there is no need to declare the data type for the second variable i.e L_num_Test.

Comment if anyone needs further clarification regarding this topic.

Reference: https://docs.oracle.com/cd/B19306_01/appdev.102/b14261/fundamentals.htm#BEIIGBBF

等待我真够勒 2024-10-01 12:41:14

%Type 及其用途:

  1. %Type 数据类型用于将变量定义为指定表的列名数据类型。
  2. 让我们以 customer 表为例,其中 Customer_name 数据类型为 varchar2(30)。如果指定 %type,则指定的列或变量将采用 varchar2(30) 数据类型。
  3. 定义这个低吗? vCustomerName Customer.Customer_Name%TYPE;在上面的定义语句中,Customer_Name 的数据类型将分配给名为 vCustomerName 的变量。
    现实生活中的示例:如果您想创建一个简单的 PL SQL 块来检查直接表中数据类型的分配。 PL SQL 块:

%Type with its use :

  1. %Type datatype is use to define the variable as column name datatype for specified table.
  2. Lets take a small example of customer table where Customer_name datatype is varchar2(30). If you give the %type then the specified column or variable will take the datatype as varchar2(30).
  3. Low to define this? vCustomerName Customer.Customer_Name%TYPE; In the above define statement the datatype of Customer_Name will be assign to the variable named vCustomerName.
    Real life example : If you want to create a simple PL SQL block to check the assignment for the datatype from direct table. PL SQL block :
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文