PL-SQL经典试题

10. 综合使用 if, while 语句, 打印 1 - 100 之间的所有素数

(素数: 有且仅用两个正约数的整数, 2, 3, 5, 7, 11, 13, ...).

declare

  v_flag number(1):=1;

  v_i number(3):=2;

  v_j number(2):=2;

begin

 

  while (v_i<=100) loop

        while v_j <= sqrt(v_i) loop

              if (mod(v_i,v_j)=0) then v_flag:= 0;end if;

              v_j :=v_j +1;

        end loop;

        if(v_flag=1) then dbms_output.put_line(v_i);end if;

 

        v_flag :=1;

        v_j := 2;

        v_i :=v_i +1;

   end loop;

 

end;

 

 

(法二)使用for循环实现1-100之间的素数的输出

declare

  --标记值, 若为 1 则是素数, 否则不是

  v_flag number(1) := 0;

begin

   for i in 2 .. 100 loop

 

       v_flag := 1;     

         

       for j in 2 .. sqrt(i) loop

           if i mod j = 0 then

              v_flag := 0;

           end if;        

       end loop;

       

       if v_flag = 1 then

           dbms_output.put_line(i);

       end if;

       

   end loop;

end;

 

11. 使用 goto

declare

  --标记值, 若为 1 则是素数, 否则不是

  v_flag number(1) := 0;

begin

   for i in 2 .. 100 loop

       v_flag := 1;     

         

       for j in 2 .. sqrt(i) loop

           if i mod j = 0 then

              v_flag := 0;

              goto label;

           end if;        

       end loop;

       

       <<label>>

       if v_flag = 1 then

           dbms_output.put_line(i);

       end if;

       

   end loop;

end;

 

11+.打印1——100的自然数,当打印到50时,跳出循环,输出“打印结束”

(方法一)

begin

  for i in 1..100 loop

      dbms_output.put_line(i);

      if(i = 50) then

      goto label;

      end if;

  end loop;

      

      <<label>>

      dbms_output.put_line('打印结束');

 

end;

(方法二)

begin

  for i in 1..100 loop

      dbms_output.put_line(i);

      if(i mod 50 = 0) then dbms_output.put_line('打印结束');

      exit;

      end if;

  end loop;

end;

******************************************************************************游标的使用

*******************************************************************************12.1 使用游标

要求: 打印出 80 部门的所有的员工的工资:salary: xxx

 

declare

  --1. 定义游标

  cursor salary_cursor is select salary from employees where department_id = 80;

  v_salary employees.salary%type;

begin

 --2. 打开游标

 open salary_cursor;

 

 --3. 提取游标

 fetch salary_cursor into v_salary;

 

 --4. 对游标进行循环操作: 判断游标中是否有下一条记录

while salary_cursor%found loop

      dbms_output.put_line('salary: ' || v_salary);

      fetch salary_cursor into v_salary;

end loop;  

 

 --5. 关闭游标

 close  salary_cursor;

end;

 

12.2 使用游标

要求: 打印出 80 部门的所有的员工的工资: Xxx 's salary is: xxx

 

declare

  cursor sal_cursor is select salary ,last_name from employees where department_id = 80;

  v_sal number(10);

  v_name varchar2(20);

begin

  open sal_cursor;

  

  fetch sal_cursor into v_sal,v_name;

  

  while sal_cursor%found loop

        dbms_output.put_line(v_name||'`s salary is '||v_sal);

        fetch sal_cursor into v_sal,v_name;

  end loop;

  

  close sal_cursor;

  

end;

 

13. 使用游标的练习:

打印出 manager_id 为 100 的员工的 last_name, email, salary 信息(使用游标, 记录类型)

 

declare  

           --声明游标    

           cursor emp_cursor is select last_name, email, salary from employees where manager_id = 100;

           

           --声明记录类型

           type emp_record is record(

                name employees.last_name%type,

                email employees.email%type,

                salary employees.salary%type

           );

           

           -- 声明记录类型的变量

           v_emp_record emp_record;

begin

           --打开游标

           open emp_cursor;

           

           --提取游标

           fetch emp_cursor into v_emp_record;

           

           --对游标进行循环操作

           while emp_cursor%found loop

                  dbms_output.put_line(v_emp_record.name || ', ' || v_emp_record.email || ', ' || v_emp_record.salary );                

                  fetch emp_cursor into v_emp_record;

           end loop;

           

           --关闭游标

           close emp_cursor;

end;

(法二:使用for循环)

declare

   

      cursor emp_cursor is

      select last_name,email,salary

      from employees

      where manager_id = 100;

 

begin

 

 

      for v_emp_record in emp_cursor loop

          dbms_output.put_line(v_emp_record.last_name||','||v_emp_record.email||','||v_emp_record.salary);

      end loop;

end;

 

14. 利用游标, 调整公司中员工的工资:

    

    工资范围       调整基数

    0 - 5000       5%

    5000 - 10000   3%

    10000 - 15000  2%

    15000 -        1%

 

declare

    --定义游标

    cursor emp_sal_cursor is select salary, employee_id from employees;

    

    --定义基数变量

    temp number(4, 2);

    

    --定义存放游标值的变量

    v_sal employees.salary%type;

    v_id employees.employee_id%type;

begin

    --打开游标

    open emp_sal_cursor;

    

    --提取游标

    fetch emp_sal_cursor into v_sal, v_id;

    

    --处理游标的循环操作

    while emp_sal_cursor%found loop

          --判断员工的工资, 执行 update 操作

          --dbms_output.put_line(v_id || ': ' || v_sal);

            

          if v_sal <= 5000 then

             temp := 0.05;

          elsif v_sal<= 10000 then

             temp := 0.03;   

          elsif v_sal <= 15000 then

             temp := 0.02;

          else

             temp := 0.01;

          end if;

          

          --dbms_output.put_line(v_id || ': ' || v_sal || ', ' || temp);

          update employees set salary = salary * (1 + temp) where employee_id = v_id;

                  

          fetch emp_sal_cursor into v_sal, v_id;

    end loop;

    --关闭游标

    close emp_sal_cursor;

end;

 

使用SQL中的 decode 函数

 

update employees set salary = salary * (1 + (decode(trunc(salary/5000), 0, 0.05,

                                                          1, 0.03,

                                                          2, 0.02,

                              

                            0.01)))

 

 

本教程由尚硅谷教育大数据研究院出品,如需转载请注明来源。
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQL是高级的非过程化编程语言,是沟通数据库服务器和客户端的重要工具,允许用户在高层数据结构上工作。它不要求用户指定对数据的存放方法,也不需要用户了解具体的数据存放方式,所以,具有完全不同底层结构的不同数据库系统,可以使用相同的SQL语言作为数据输入与管理的SQL接口。 它以记录集合作为操作对象,所有SQL语句接受集合作为输入,返回集合作为输出,这种集合特性允许一条SQL语句的输出作为另一条SQL语句的输入,所以SQL语句可以嵌套,这使它具有极大的灵活性和强大的功能,在多数情况下,在其他语言中需要一大段程序实现的功能只需要一个SQL语句就可以达到目的,这也意味着用SQL语言可以写出非常复杂的语句。    结构化查询语言(Structured Query Language)最早是IBM的圣约瑟研究实验室为其关系数据库管理系统SYSTEM R开发的一种查询语言,它的前身是SQUARE语言。SQL语言结构简洁,功能强大,简单易学,所以自从IBM公司1981年推出以来,SQL语言得到了广泛的应用。如今无论是像Oracle、Sybase、DB2、Informix、SQL Server这些大型的数据库管理系统,还是像Visual Foxpro、PowerBuilder这些PC上常用的数据库开发系统,都支持SQL语言作为查询语言。    美国国家标准局(ANSI)与国际标准化组织(ISO)已经制定了SQL标准。ANSI是一个美国工业和商业集团组织,负责开发美国的商务和通讯标准。ANSI同时也是ISO和International Electrotechnical Commission(IEC)的成员之一。ANSI 发布与国际标准组织相应的美国标准。1992年,ISO和IEC发布了SQL国际标准,称为SQL-92。ANSI随之发布的相应标准是ANSI SQL-92。ANSI SQL-92有时被称为ANSI SQL。尽管不同的关系数据库使用的SQL版本有一些差异,但大多数都遵循 ANSI SQL 标准。SQL Server使用ANSI SQL-92的扩展集,称为T-SQL,其遵循ANSI制定的 SQL-92标准。    SQL语言包含4个部分:    数据定义语言(DDL),例如:CREATE、DROP、ALTER等语句。    数据操作语言(DML),例如:INSERT(插入)、UPDATE(修改)、DELETE(删除)语句。    数据查询语言(DQL),例如:SELECT语句。    数据控制语言(DCL),例如:GRANT、REVOKE、COMMIT、ROLLBACK等语句。    SQL语言包括三种主要程序设计语言类别的语句:数据定义语言(DDL),数据操作语言(DML)及数据控制语言(DCL)。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值