Use the%type keyword to declare a data type that is the same as the specified column name, which is usually followed by the specified column name.
2 Benefits of using%type:
1. Users do not have to look at the data types of individual columns in the table to ensure that the defined variables can store the retrieved data.
2. If the data type of a column in a table changes, as long as the field name does not change, the user does not have to consider changing the data type of the variable.
Here is a simple example:
Declarev_ename Emp.ename%Type--declaring a variable with the same type as the ename columnV_job Emp.job%Type--declaring variables with the same type as the job columnbegin SelectEname,job intoV_ename,v_job fromEMPwhereEmpno=7369;--retrieving data and saving it in a variableDbms_output.put_line (v_ename||''s position is'||V_job);--Output ResultsEnd;
The output results are as follows:
Smith's job is clerk.
In the process of use should pay attention to:
Since the variables in the INTO clause can only store a single value, it is required that the clause returned by select can only be a row of data, which is qualified by the WHERE clause, and if multiple rows of data are returned, the code runs with an error.
Oracle uses%type-type variables to output results