PLSQL操作


点击(此处)折叠或打开

  1. Declare
  2.     counter Number;
  3. Begin
  4.     For counter In Reverse 0..10 Loop
  5.         dbms_output.put_line(counter);
  6.         End Loop;
  7. End ;

  8. Declare
  9. First Varchar2(20);
  10. Last Varchar2(20);
  11. email Varchar2(25);
  12. Begin
  13.     Select first_name,last_name,email Into First,Last,email
  14.     From scott.employees Where employee_id=100;
  15.     dbms_output.put_line('Employee Information for ID: ' || First || ' ' || Last || ' - ' || email);
  16. Exception
  17.     When no_data_found Then
  18.         dbms_output.put_line('No employee matches the given ID');
  19.     When too_many_rows Then
  20.         dbms_output.put_line('More than one employee matches the given ID');
  21. End;

  22. Declare
  23.     Cursor emp_cursor Is
  24.     Select first_name,last_name,email From employees Where employee_id = &emp_id;
  25.     
  26.     First Varchar2(20);
  27.     Last Varchar2(20);
  28.     email Varchar2(20);
  29. Begin
  30.     Open emp_cursor;
  31.     Fetch emp_corsor Into First,Last,email;
  32.     If emp_cursor%Notfound Then
  33.         Raise no_data_found;
  34.     Else
  35.         --Perform second fetch to see if more than one row is returned
  36.         Fetch emp_cursor Into First,Last,email;
  37.         If emp_cursor%Found Then
  38.             Raise too_many_rows;
  39.         Else
  40.             dbms_output.put_line(
  41.             'Employee Information for ID: ' || First || ' ' || Last || ' - ' || email);
  42.         End If ;
  43.     End If ;
  44.     
  45.     Close emp_cursor;
  46. Exception
  47.     When no_data_found Then
  48.         dbms_output.put_line('No employee matches the given ID');
  49.     When too_many_rows Then
  50.         dbms_output.put_line('More than one employee matches the given ID');
  51. End;

  52. --限定列名与变量名
  53. Create Or Replace Procedure retrieve_emp_info(last_name In Varchar2) As
  54.     First Varchar2(20);
  55.     Last Varchar2(20);
  56.     email Varchar2(20);
  57. Begin
  58.     Select first_name,last_name,email Into First,Last,email From employees Where last_name = retrieve_emp_info.last_name;
  59.     
  60.     dbms_output.put_line('Employee Inforation for ID: ' || First || ' ' || last_name || ' - ' || email);
  61. Exception
  62.     When no_data_found Then
  63.         dbms_output.put_line('No employee matches the last name ' || last_name);
  64. End ;


  65. Declare
  66.     First employees.first_name%Type;
  67.     Last employees.last_name%Type;
  68.     email employees.email%Type;
  69. Begin
  70.     Select first_name,last_name,email Into First,Last,email From employees Where employee_id = &emp_id;
  71.     
  72.         dbms_output.put_line('Employee Information for ID: ' || First || ' ' || Last || ' - ' || email);
  73. Exception
  74.     When no_data_found Then
  75.         dbms_output.put_line('No matching employee was found,please try again.');
  76.     When Others Then
  77.         dbms_output.put_line('An unknown error has occured,please try again.');
  78. End ;

  79. Declare
  80.     Cursor emp_cur Is
  81.     Select * From employees Where employee_id = &emp_id;
  82.         --Declaring a local variable using the Rowtype attribute
  83.         --of the employees table
  84.     emp_rec employees%Rowtype;
  85. Begin
  86.     Open emp_cur;
  87.     Fetch emp_cur Into emp_rec;
  88.     If emp_cur%Found Then
  89.         dbms_out.put_line('Employee Information for ID: ' || emp_rec.first_name || ' ' || emp_rec.last_name || ' - ' || emp_rec.email);
  90.     Else
  91.         dbms_output.put_line('No matching employee for the given ID') ;
  92.     End If ;
  93.     Close emp_cur ;
  94.  Exception
  95.      When no_data_found Then
  96.          dbms_output.put_line('No employee matches the given emp ID') ;
  97.          
  98.  End ;
  99.  
  100.  
  101.  Declare
  102.      Type emp_info Is Record(First employees.first_name%Type,
  103.                                  employees.last_name%Type,
  104.                                   employees.email%Type);
  105.     emp_info_rec emp_info;
  106.  Begin
  107.      Select first_name,last_name,email Into emp_info_rec From employees Where last_name = 'Vargas' ;
  108.      
  109.      dbms_output.put_line('The queried employee''s email address is ' || emp_info_rec.email) ;
  110.  Exception
  111.      When no_data_found Then
  112.          dbms_output.put_line(\'No employee matches the last name provided\') ;
  113.          
  114.  End ;
  115.  
  116.  Declare
  117.      Cursor emp_cur Is
  118.     Select first_name,last_name,email From employees Where employee_id = 100 ;
  119.     emp_rec emp_cur%Rowtype ;
  120.  Begin
  121.      Open emp_cur;
  122.      Fetch emp_cur Into emp_rec;
  123.      If emp_cur %Found Then
  124.      Close emp_cur;
  125.      dbms_output.put_line(emp_rec.first_name || ' ' || emp_rec.last_name ||
  126.                                                  '''s email Is ' || emp_rec.email);
  127.      Else
  128.          dbms_output.put_line('No employee matches The provided Id Number');
  129.      End If ;
  130.   exception
  131.       when no_data_found then
  132.           dbms_output.put_line( ' No employee matches The Last Name provided');
  133.   
  134.  End ;


  135.  ---循环遍历查询结果
  136.  Begin
  137.      For emp In
  138.          (
  139.             Select first_nam,last_name,email From employees Where commission_pct Is Not Null
  140.          )
  141.      Loop
  142.          dbms_output.put_line(emp.first_name || ' ' || emp.last_name || ' - ' || emp.email);
  143.      End Loop ;
  144.  End ;
  145.  
  146.  
  147.  Declare
  148.      Cursor emp_cur Is
  149.         Select first_name,last_name,email From employees Where commission_pct Is Not Null ;
  150.         emp_rec emp_cur%Rowtype ;
  151.  Begin
  152.       For emp_rec In emp_cur Loop
  153.           dbms_output.put_line(
  154.               emp_rec.first_name || ' ' || emp_rec.last_name || ' - ' || emp_rec.email );
  155.        End Loop ;
  156.  End ;

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29874114/viewspace-1683289/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29874114/viewspace-1683289/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值