Oracle——08PL/SQL简介,基本程序结构和语句

1.变量

2.常量

3.基本程序结构和语句




PL/SQL能够驻留在Oracle数据库服务器和Oracle开发工具两个环境中。在这两个环境中,PL/SQL引擎接受任何PL/SQL块和子程序作为输入,引擎执行过程语句将SQL语句发送给Oracle服务器的SQL语句执行器执行。

1、PL/SQL变量

(1)变量的声明:

变量通常是在PL/SQL块的声明部分定义的,每一个变量都有一个特定的类型。

变量定义的基本格式为:

<变量名> <数据类型>[(宽度):=<初始值>]

如定义一个名为countNum的number类型变量,初始值为1,可以如下写:

countNum number := 1;

		--变量的基本命名规则如下:


		--a.变量名必须以字母开头


		--b.变量长度不超过30个字符


		--c.变量名中不能有空格
 

(2)变量的属性

%用于表示属性提示符。

%type属性。%type属性提供了变量和数据库列的属性,有了这个属性是非常方便的,假设现在有个表A,里面有字段B,现在要定义一个变量C,需要它的数据类型和字段B一样,这个时候我们可以不知道B的数据类型,直接使用B%type就可以了,如:C B%type。

%rowtype属性。这个属性提供了表示一个表中一行记录的数据类型。有了它我们就可以方便的用一个变量表示游标中的一行,如cursorRecord cursorName%rowtype,其中,cursorRecord表示游标一行的变量名,cursorName表示对应游标的名称,利用%rowtype就可以取到一行的记录类型,这样以后在取数据的时候,就可以直接使用

fetch cursorName into cursorRecord中了。示例代码:

		declare cursor moduleCursor is


		select * from t_module;


		moduleRecord moduleCursor%rowtype;


		begin


		  open moduleCursor;


		  fetch moduleCursor into moduleRecord;


		  while moduleCursor%found 


		  loop


			dbms_output.put_line(moduleRecord.name || '**********************');


			fetch moduleCursor into moduleRecord;


		  end loop;


		  close moduleCursor;


		end;
 

(3)自定义数据类型

PL/SQL还支持用户自定义数据类型。下面是自定义一个PersonRecord数据类型的示例:

			declare


				type PersonRecord is record (


					name varchar(20),


					age number(3)


				);
 

下面是PersonRecord的实际应用示例:

			declare
				type PersonRecord is record (
					name varchar(20),
					id number(3)
				);
				person PersonRecord;
			begin
				select name,id into person from t_employee where username='admin';
				dbms_output.put_line('**********' || person.name);
			end;
 

2、PL/SQL常量

常量是指在程序运行过程中值不变的量。

常量声明的基本格式如下:

<常量名> constant <数据类型> := <值>

如定义一个整型常量num,值为5,可以如下写:

num constant integer := 5;

    3、PL/SQL基本程序结构和语句
    (1)条件结构
        a.if-then结构
        语法格式:

            if booleanExpression
                then runExpression
            end if;
        --当booleanExpression的结果为真时就执行runExpression部分,注意用end if结束if语句。

        示例代码:

            declare totalCount number;
            begin
              select count(*) into totalCount from t_module;
              if totalCount > 0 then
              dbms_output.put_line('共有' || totalCount || '条记录');
              end if;
            end;


        b.if-then-else结构
        语法格式:

            if booleanExpression then
                runExpression
            else
                runExpression2
            end if;
       --该结构表示当booleanExpression的结果为true时执行runExpression,否则执行runExpression2,注意要结束end if;
 


        示例代码:

            declare
            a number;
            b number;
            c number;
            begin
            a := 1;
            b := 2;
            c := 3;
            if a=1 then
              c := c + a;
            else
              c := c + b;
            end if;
            DBMS_OUTPUT.PUT_LINE(c);
            dbms_output.put_line(c);
            end;

        c.if-then-elsif-then-else结构
        语法格式:

            if booleanExpression then
                runExpression1
            elsif booleanExpression2 then
                runExpression2
            else
                runExpression3
            end if;
        --该结构表示当booleanExpression为true时执行runExpression1,否则当booleanExpression2为true时执行runExpression2,否则执行runExpression3
 


       
    (2)循环结构
        编写循环结构的时候一定要确保有退出条件满足。
        a.loop-exit-end循环
        语法格式:

            loop
                runExpression;    --执行循环体
                if booleanExpression then    --判断是否满足退出条件
                    exit;    --当满足退出条件时即退出循环
                end if; --结束if语句
            end loop; --结束loop语句
 


            示例代码,利用loop-exit-end循环求10的阶乘:

            declare
              n number := 10;
              results number := 1;
            begin
              loop
                results := results * n;
                n := n - 1;
                if n=0 then
                  exit;
                end if;
              end loop;
              dbms_output.put_line('10的阶乘是:' || results);
            end;

           
        b.loop-exit-when-end循环
        语法格式:

            loop
                runExpression;    /*执行循环体*/
                exit when booleanExpression    /*测试是否符合退出条件,符合即退出*/
            end loop;


            示例代码,利用此结构求10的阶乘:

                declare
                  a number := 1;
                  b number := 1;
                begin
                  loop
                    b := a * b;
                    a := a + 1;
                    exit when a > 10;
                  end loop;
                  dbms_output.put_line('the result is: ' || b);
                end;
 


        c.while-loop-end循环
        语法格式:

            while booleanExpression     --测试是否符合退出条件
            loop
                runExpression; --执行循环体
            end loop;
        --此种结构先判断是否符合条件再进行循环,而上面的两种是先执行一次循环再判断是否符合条件。
 


            示例代码:

                declare
                  a number := 1;
                  b number := 1;
                begin
                  while a<=10
                  loop
                    b := a * b;
                    a := a + 1;
                  end loop;
                  dbms_output.put_line('the result is: ' || b);
                end;
 


        d.for-in-loop-end循环
        语法格式:

            for countVar in count1..countn    --定义跟踪循环的变量
            loop
                runExpression    --执行循环体
            end loop;
        --countVar是循环变量,表示当前循环的当前值,in确定循环变量的初值count1和终值countn,如果循环变量的值小于终值则执行循环体,否则就退出
        --循环,每循环一次循环变量的值自动增加一个步长。
 


            示例代码:

                declare
                  b number := 1;
                begin
                  for c in 1..10
                  loop
                    b := b * c;
                  end loop;
                  dbms_output.put_line('the result is: ' || b);
                end;
        
 


    (3)选择语句
        case语句:可以使用简单结构,对数值列表做出选择。
        语法格式:

            case inputName
                when expression1 then result1
                when expression2 then result2
                when expressionn then resultn
                else result
            end;
        -- case语句会依次比较expressionn与inputName的值,若相等,则返回resultn,否则继续往下判断,若都不满足,则返回else中result的值。
 


        示例代码:

            declare
              num1 number;
            begin
              num1 := 6;
              case num1
              when 1 then
                dbms_output.put_line('the result is no.1');
              when 2 then
                dbms_output.put_line('the result is no.2');
              when 3 then
                dbms_output.put_line('the result is no.3');
              else
                dbms_output.put_line('the result is not in top 3');
              end case;
            end;
 

猜你喜欢

转载自elim.iteye.com/blog/1553915