I. What is PL/SQL (Procedure language/sql)
Concept: Plsql is Oracle's procedural extension of SQL language
Refers to the addition of process-processing statements (such as branching, loops, and so on) in the SQL command language, enabling the SQL language to have process processing power.
The structure of PL/sql:
declare 说明部分 (变量说明,光标申明,例外说明 〕begin 语句序列 (DML语句〕… exception 例外处理语句 End;
A simple PL/SQL program
If you need to use set serveroutput on the command line to open the display first
--声明变量begin-- 程序执行的主体dbms_output.put_line(‘Hello World‘);--输出语句end;
Two. basic syntax for PL/SQL programs
1. Constants and variables
2. Variable type
3. Use of reference and record-type variables
The use of reference-type variables
-- 查询并打印7839的姓名和薪水declare -- 定义姓名和薪水的变量 --即引用emp表中ename列的类型 psal emp.sal%type; --即引用emp表中sal列的类型begin -- 查询语句 赋值的两种方式 1.:= 2. into关键字 select ename,sal into pname,psal from emp where empno=‘7839‘; dbms_output.put_line(pname ||‘的薪水是‘|| psal);end;
Record type variable
-- 查询并打印7839的姓名和薪水declare -- 定义记录型变量,代表一行--即记录了emp表中一行的类型,使用的时候直接.调用begin -- 查询语句 select * into dec_emp from emp where empno=‘7839‘; --使用的时候 dec_emp.sal 变量名.列名 dbms_output.put_line(dec_emp.ename ||‘的薪水是‘|| dec_emp.sal);end;
3.if statements
Three different formats:
格式一: IF 条件 THEN 语句1; 语句2; ENDIF;
IF 条件 THEN 语句序列1; ESLE 2; END IF;
格式三(主要使用这种): IF 条件 THEN 语句; ELSIF 条件 THEN 语句; ELSE 语句; END IF;
--Determine the number of digits entered on the user's keyboard--input of the receiving keyboard--NUM is the address value that holds the input number on the address valueAccept Num Prompt' Please enter a number ';d Eclare--Define variables to save numbersPnum Number: = #beginifPnum =0 ThenDbms_output.put_line (' The number you have entered is 0 '); elsif Pnum =1 ThenDbms_output.put_line (' The number you have entered is 1 '); elsif Pnum =2 ThenDbms_output.put_line (' The number you have entered is 2 ');ElseDbms_output.put_line (' other numbers ');End if;End;
4. Looping statements
格式一:WHILE 条件LOOP语句序列;END LOOP;-----------------------------------------------例如:WHILE total <= 25000 LOOP.. .total : = total + salary;END LOOP;
格式二(主要使用这种,EXIT的地方容易理解):LoopEXIT [when 条件];语句序列Endloop
格式三:FOR I IN 13 LOOP语句序列 ;END LOOP
-- 打印1~10的数字declare 1;begin loop --循环 --退出条件 exitwhen10; dbms_output.put_line(pnum); -- 加1 1; endloop;end;
The above is the basic syntax of PL/SQL Programming (a) of the whole content, more information, please pay attention to: CPP Learning Network _cpp University
This article fixed link: CPP Learning Network _cpp University-pl/sql Programming Basic Grammar explanation (I.)
A detailed explanation of basic grammar of PL/SQL Programming (I.)