PL/SQL knowledge (personal notes) (1)

PL/SQL knowledge (personal notes) (1)

1. Basic structure

declare
--声明部分,比如变量定义,常量定义及赋值;
begin
--需要执行的代码,比如输出数据,数据的增删改查, DML, DDL都可以
end;

2. Variables and constants

variable:

The content is variable, and can be assigned at any time during the running of the program, that is, the content of the variable can be changed, so it is called a variable; variables generally start with v_: v_empname varchar2(50);

constant:

Once the value is defined and assigned, the value cannot be changed; the constant usually starts with c_, and the constant keyword needs to be added when defining it: c_empno constant number(4);

3. Print information

dbms_output.put_line();

The parentheses can be column names, that is, the first line of this column of data is printed; the
parentheses can also be specific data, such as strings (requires single quotes), such as numbers, such as time, etc.

**

4. Assignment

**
Syntax:
variable name: = value;
constant name: = value;

example:

v_empname varchar2(50) :='ABC';
c_empno constant number(4) :=1234;

5. Comments:

1. Single line comment

Add-before the code, the code will turn red at this time, and the line of the table name code is commented out and will not be executed

2. Multi-line comments

Add / before the first line of code / add / after the last line of code , the middle is the commented out part, the program will not be executed when running;

declare
 v_v1 number(5) :=10;
 c_c1 constant varchar2(20) :='ABC';
begin
   dbms_output.put_line('v_v1: '||v_v1);
   dbms_output.put_line('c_c1: '||c_c1);
   dbms_output.put_line('今天是2月12日');
   dbms_output.put_line(trunc(sysdate));
   v_v1:=20;
   dbms_output.put_line('v_v1:'||v_v1);
   c_c1 :='TEST';
   dbms_output.put_line('c_c1:'||c_c1);
end;

6. Use the types in the table:

(1).%type: Use the type and length of a column in the table: v_empno emp.empno%type;
(2).%rowtype: Use the data type of a table, that is, the variable is the same as the table, how many columns are in the table , How many columns are there in the variable: v_emp emp%rowtype;
(3). When querying in the code block, you need to define a variable to take over the data that is queried, and then print the variable to print the query result. When outputting, every time Only one line can be output, so you need to add restrictions when querying;

create table test_0212
(
       name1 varchar2(50),
       age1 number(3)
);

insert into test_0212 values('A',1);
insert into test_0212 values('B',2);
insert into test_0212 values('C',3);
insert into test_0212 values('D',4);
insert into test_0212 values('E',5);
commit;

select * from test_0212;

declare
-the declaration part, define the variable
v_empno emp.empno%type;
v_emp emp%rowtype;

begin
--代码执行部分
/*insert into  test_0212 values('F',6);
commit;*/
/*delete from test_0212 where name1='A';
commit;*/
/*
update test_0212 set age1=10 where name1='B';
commit;*/
select empno into v_empno from emp where rownum<2;
dbms_output.put_line(v_empno);

select * into v_emp  from emp where rownum<2;
dbms_output.put_line('empno:'||v_emp.empno||' ename:'||v_emp.ename||' sal:'||v_emp.sal);

end;

7. Custom type:

grammar:

type 类型名 is record
(
	列名1 类型(长度),
	列名2 类型(长度),
	列名3 类型(长度)
);

example:

declare 
type t_type is record
(
     name1 varchar2(50),
     job1 varchar2(50)
);
v_d t_type;
begin
  select ename, job into v_d from emp where rownum<2;
  dbms_output.put_line('ename:'||v_d.name1||','||'job:'||v_d.job1);
end;

Note:
(1): When defining the type, the type name starts with t_;
(2): The syntax of the variable definition: variable name type name;-here means that the variable has the same type of columns and attributes
(3): when printing the variable , The list must be consistent with the column name in the type: variable name. column name

8.Keyboard input information:

Keyboard input:
&Prompt information: & indicates that you will enter from the keyboard, and the prompt information indicates the content of the input information;

Method 1:
Use a variable to accept keyboard input:

declare
v_name emp.ename%type;
v_empno number(20) := &编号;
begin
  select ename into v_name from emp where empno=v_empno;
  dbms_output.put_line(v_name);
end;

Method 2:
Directly use the column name to accept keyboard input:

declare
v_name emp.ename%type;
begin
  select ename
    into v_name
    from emp
   where empno = &empno;
  dbms_output.put_line(v_name);
end;

Guess you like

Origin blog.csdn.net/yang_z_1/article/details/111878722