Oracle principle: PL/SQL foundation

1. PL/SQL functions and features

 PL/SQL is a programming language that combines procedural language (Procedural Language) and structured query (SQL) language; PL/SQL is an extension of SQL and supports large objects and collection types. You can also add program logic to the SQL language. Support SQL, which can be used in PL/SQL: data manipulation commands, transaction control commands, cursor control, SQL functions and SQL operators. PL/SQL has better performance. When the user sends the PL/SQL block to the server, the Oracle server compiles, runs, and returns the result to the user. It is portable and can be executed on any operating system. PL/SQL can improve security by restricting access to users through stored procedures.

  The Oracle server has PL/SQL engine and SQL engine. The PL/SQL engine executes for, if and other procedural statements, while the SQL engine executes SQL statements such as insert and delete.

PL/SQL is divided into three parts, the declaration part, the executable part, and the exception handling part, of which the executable part is a must.

DECLARE
--声明变量在这里写
BEGIN
--过程程序在这里写
EXCEPTION
--异常处理在这里写
END;

 

2. Oracle 11g's PL/SQL improves the sequence, that is, the use of no dual sequence

----创建一个序列 seq ----
create sequence seq start with 1 increment by 1;
----SQL语句的序列取值必须用select -----
select seq.nextval from dual ;
----过程语句不需要select 也可以进行序列取值
declare 
 se positive ; --正整数
begin
  se :=seq.nextval ;
  -- select seq.nextval into se from dual ;
  dbms_output.put_line(se);   
 end;
/
drop sequence seq;

Three, control statement and continue statement

Conditional judgment statement: if ..then; if..then..else; if..then.elsif..; case statement;

Loop statement: Loop [ unconditional loop content]  end loop; while [loop condition] loop [ loop content ] end loop;   

                  for [ loop body variable ] in [ value range of loop body variable ] loop [ loop content ] end loop;

Note: Loop [ unconditional loop content]  end loop is usually matched with exit, which is equivalent to JAVA break. If there is no control statement, it is equivalent to an infinite loop.

     continue; go directly to the next cycle;

 

DDL statements cannot be used in PL procedure statements, namely operations such as create, drop, and truncate. To use DDL statements must be executed with dynamic SQL:

execute immediate sql_command [into execution result set ] [using bind variable set  ]

---可重跑的SQL脚本:创建并初始化表示例------
declare
 sql_command varchar2(500);
 isExsit number ;
begin
  select count(1) into isExsit from user_tables where table_name ='SALARY_TBL'; 
  if isExsit <> 0  then 
    sql_command :='truncate table salary_tbl';
    execute immediate sql_command;
    sql_command :='drop table salary_tbl';
    execute immediate sql_command;
  end if;
  sql_command := '  
create  table salary_tbl(
   employer_nm varchar(20),
   department varchar(20) not null,
   salary number not null,
   leader_nm varchar(20)
)';
 execute immediate sql_command;
 for i in  1..13000
     loop
     insert into salary_tbl values('雇佣者'||i,'部门'||Mod(i,50),100+sqrt(i),'雇佣者'||Mod(i,20)); 
     if Mod(i,1000)=0 then 
       commit;
     end if;
   end loop;
 commit;
end; 
/

---goto 语句示例

declare
 sql_command varchar2(500);
 isExsit number ;
 i number := 1;
begin
  select count(1) into isExsit from user_tables where table_name ='SALARY_TBL'; 
  if isExsit <> 0  then 
    sql_command :='truncate table salary_tbl';
    execute immediate sql_command;
    sql_command :='drop table salary_tbl';
    execute immediate sql_command;
  end if;  
  sql_command := '  
create  table salary_tbl(
   employer_nm varchar(20),
   department varchar(20) not null,
   salary number not null,
   leader_nm varchar(20)
)';
 execute immediate sql_command;
 <<code_aa>>    -
     insert into salary_tbl values('雇佣者'||i,'部门'||Mod(i,50),100+sqrt(i),'雇佣者'||Mod(i,20)); 
      if Mod(i,1000)=0 then 
       commit;
     end if;
     i :=i+1;
     if i <13000 then 
       goto code_bb;
     else 
       goto code_aa;
     end if ;   
   <<code_bb>> 
 commit;
end; 
/

 

Fourth, error handling in Oracle11g

Exceptions are divided into predefined exceptions and user-defined exceptions. The predefined exception is the exception that comes with the system, and the user-defined exception (raise) is the throw that is equivalent to java declared by itself.

Predefined exception exception when [exception name] then ... when [exception name 2] then ...  

raise_application_error ( exception number , exception information ) is to interrupt the program abnormally and output the exception information

declare 
 department varchar(20) := '';
 nullinserterror1 exception;
begin
   if department is null then
     raise nullinserterror1;
   end if;
   insert into salary_tbl values('雇佣者9999',department,100,'雇佣者4'); 
exception 
   when nullinserterror1   then
     dbms_output.put_line('自定义空值插入异常');
     raise_application_error (-20001,'空值异常');
end;
/

Show in the output

Guess you like

Origin blog.csdn.net/superSmart_Dong/article/details/106744970