10. Define and use variables - scalar types

Definition:
When writing a pl/sql program, you can define variables and constants; the pl/sql program includes:
1) scalar type (scalar)
2) composite type (composite)
3) reference type (reference)
4) lob( large object)

scalar (scalar)---common type
When writing pl/sql blocks, if you want to use variables, you need to define variables in the definition section. The syntax
for defining variables and constants in pl/sql is as follows: identifier [constant] datatype [not null] [:=| default exptr] identifier: name constant: specifies a constant, its initial value needs to be specified, and its value cannot be changed datatype: data type not null: the specified variable value cannot be null := specify the initial value for the variable or constant default: used to specify the initial value expr: pl/sql expression specifying the initial value, which can be text value, other 1) Define a variable-length string v_ename varchar2(10); 2) Define a decimal range -9999.99 - 9999.99 v_sal number(6,2); 3) Define a decimal and give an initial value 5.4 := is the assignment number of pl/sql
















v_sal2 number(6,2):=5.4
4) Define a date type data
v_hiredate date;
5) Define a Boolean variable, which cannot be empty, the initial value is false
v_valid boolean not null default false;


scalar (scalar)--- Using scalars
Once variables are defined, they can be used. What needs to be explained here is that the assignment of pl/sql blocks to variables is different from other programming languages. You need to add a colon (:=) before the equal sign


to enter the employee number, and display the employee's name, salary, and personal income tax (the tax rate is 0.03) as Example
declare --declare
constant tax rate and assign initial value
c_tax_rate number(3,2):=0.03; --define
variable --user
name
v_ename varchar2(5);
--salary
v_sal number(7,2);
--individual Income tax
v_tax_sal number(7,2);
begin
  --execute
  select ename,sal into v_ename,v_sal from emp where empno=&no; --calculate
  income tax
  v_tax_sal:=v_sal*c_tax_rate;
  --output
  dbms_output.put_line('Name:'||v_ename||' Salary:'||v_sal||' Personal income tax:'||v_tax_sal||' Actual salary:'||(v_sal-v_tax_sal));
  end;

scalar(
scalar) --- There is a problem with the above pl/sql block using the %type type
: that is, if the employee's name exceeds 5 characters, there will be an error. In order to reduce the maintenance workload of the pl/sql program, you can use % The Type property defines a variable so that it will determine the type and length of the variable you define according to the type of database column.
Usage: identifier name table name. column name %type;

example: --user
name
v_ename emp.ename%type;
//meaning that the type of the defined variable v_ename is consistent with the type of the ename column of the emp table


Example: --The
following is an example of entering the employee number, displaying the employee's name, salary, and personal income tax (tax rate is 0.03)
declare --Define
a constant tax rate and assign the initial value
c_tax_rate number(3,2):=0.03; --Define
variables--
User name
v_ename emp.ename% type;
- salary
V_SAL Number (7, 2);
- Personal income tax
v_tax_sal number (7, 2);
begin
  - execution
  select ename,sal into v_ename,v_sal from emp where empno=&no; --Calculate
  income tax
  v_tax_sal:=v_sal*c_tax_rate; --Output
  dbms_output.put_line
  ('Name:'||v_ename||' Salary:'||v_sal| | 'Personal income tax:' || v_tax_sal || 'actual salary:' || (v_sal-v_tax_sal)
  ;
 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327054067&siteId=291194637