Learning: Oracle database plsql simple programming

Tool: cmd sqlplus
Content: basic table operation, plsql programming (branch structure, loop, judgment, etc.)
Account: scott

Preliminary preparation


cmd use tips

Because the author is using sqlplus, and entered in the cmd environment. Text editing in the cmd environment is not easy, so I provide you with a little trick.

As shown in the figure:

write picture description here

cmd shortcut key: alt+space+k to mark the
selected area, enter copy
alt+space+p paste

!!!! Markers can select any range, which is super easy to use compared to the fixed mode of selecting multiple lines in office! ! !


C:\Users\Administrator>sqlplus

SQL*Plus: Release 11.2.0.1.0 Production on Thu Apr 26 08:13:18 2018

Copyright (c) 1982, 2010, Oracle. All rights reserved.

Please enter username: scott
Enter password:
ERROR:

ORA-12560: TNS: protocol adapter error

If you encounter a protocol adapter error, check if the service is enabled.
write picture description here
Please enter username: scott
Enter password:

连接到:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> select * from user_role_privs;

USERNAME GRANTED_ROLE ADM DEF OS_


SCOTT CONNECT NO YES NO
SCOTT RESOURCE NO YES NO

SQL> select sysdate,systimestamp from dual;

SYSDATE

SYSTIMESTAMP

26-Apr-18
26-Apr-18 08.20.24.358000 AM +08:00


text:


SQL> print sname;
SP2-0552: 未声明绑定变量 "SNAME"
SQL> set serveroutput on
SQL> declare
  2  sname varchar2(20) :='abcd';
  3  begin
  4  sname :=sname||'and tom';
  5  dbms_output.put_line(sname);
  6  end;
  7  /
abcdand tom

PL/SQL 过程已成功完成。

SQL> declare
2 ename varchar2(20default 'abc';
3 begin
4 select sname into ename from student where sno ='2015

SQL>set serveroutput on
SQL>declare
2 pi constant number :=3.14;
3 r number default 3;
4 area nubmer;
5 begin
6 area :=pi*r*r;
7 dbms_output.put_line(area);
8 end;
9 /
****

PL/SQL 过程已成功完成

SQL>var emp_name varchar2(30);
SQL>begin
2 select sname into : emp_name from student where sno='2018001';
3 end;
4 /

SQL> insert into student values('2018001','zhangsan','男','20');

已创建 1 行。

SQL> select * from student;

       SNO SNAME                SSEX                       SAGE
---------- -------------------- -------------------- ----------
   2018001 zhangsan             男                           20

SQL> var emp_name varchar2(30);
SQL> begin
  2  select sname into :emp_name from student where sno='2018001';
  3  end;
  4  /

PL/SQL 过程已成功完成。

SQL> print emp_name;

EMP_NAME
--------------------------------
zhangsan



----------
//修改scott账户权限
在plsql developer中要是以scott/tiger登录时提示ora-28000 the account is locked。

解决办法:

新装完Oracle10g后,用scott/tiger测试,会出现以下错误提示:
        oracle10g the account is locked
        oracle10g the password has expired
原因:默认Oracle10g的scott不能登陆。
解决:1)conn sys/sys as sysdba; //以DBA的身份登录2)alter user scott account unlock;// 然后解锁3)conn scott/tiger //弹出一个修改密码的对话框,修改一下密码就可以了

在运行里面输入cmd在DOS模式下输入sqlplus,以system用户名登录,密码是刚装oracle时自己填写的密码orcl,登录进去以后。

SQL> conn sys/sys as sysdba;       (分号是必须的但是我是以system登录的所在这不应该写conn sys/sys as sysdba应该写conn system/orcl as sysdba;)
         Connected.
SQL> alter user scott account unlock;
         User altered.
SQL> commit;
         Commit complete.
SQL> conn scott/tiger//请输入新密码,并确认后OK
Password changed
Connected.


----------

SQL> update  student set sname='张三' where sno='2018001';

已更新 1 行。

SQL> select * from student;

       SNO SNAME                SSEX                       SAGE
---------- -------------------- -------------------- ----------
   2018001 张三                 男                           20

SQL>

SQL> declare
  2  myemp student%rowtype;
  3  begin
  4  select * into myemp from student where sno='2018001';
  5  dbms_output.put_line(myemp.sname);
  6  end;
  7  /
zhangsan


----------
//查询james工资,如果大于900,则发放奖金若干

declare emp.sal % type;
//定义变量,与sal字段类型保持一致
select sal into newSal from emp
where ename='james';
if newSal>900 then
update emp
set comm=800
where ename='james';
end if;
commit;
end;


----------
//if else

SQL> declare
  2  newSal emp.sal % type;
  3  begin
  4  select sal into newSal from emp where ename='ALLEN';
  5  if newSal > 1000 then
  6  update emp set comm = 800 where ename ='ALLEN';
  7  else
  8  update emp set comm = 400 where ename ='ALLEN';
  9  end if;
 10  end;
 11  /

PL/SQL 过程已成功完成。

SQL> select * from emp;

ENAME                       SAL       COMM         ID
-------------------- ---------- ---------- ----------
SMITH                       800
ALLEN                      1600        800
WARD                       1250        500
JONES                      2975
MARTIN                     1250       1400
BLAKE                      2850
CLARK                      2450
SCOTT                      3000
KING                       5000
TURNER                     1500          0
ADAMS                      1100

ENAME                       SAL       COMM         ID
-------------------- ---------- ---------- ----------
JAMES                       950
FORD                       3000
MILLER                     1300

已选择14行。


----------
//选择结构
SQL> set serveroutput on
SQL>
SQL> declare
  2  v_grade char(1):=upper('&grade');
  3  begin
  4  case v_grade
  5  when 'A' then
  6  dbms_output.put_line('Excellent');
  7  when 'B' then
  8  dbms_output.put_line('Very Good');
  9  when 'C' then
 10  dbms_output.put_line('Good');
 11  else
 12  dbms_output.put_line('No such grade');
 13  end case;
 14  end;
 15  /
输入 grade 的值:  a
原值    2: v_grade char(1):=upper('&grade');
新值    2: v_grade char(1):=upper('a');
Excellent

PL/SQL 过程已成功完成。


----------
//选择结构
SQL> set serveroutput on
SQL>  declare
  2   v_grade char(1):=upper('&grade');
  3   p_grade varchar(20);
  4   begin
  5   p_grade :=
  6   case v_grade
  7   when 'A' then
  8   'Excellent'
  9   when 'B' then
 10   'Very Good'
 11   when 'C' then
 12   'Good'
 13   else
 14   'No such grade'
 15   end;
 16   dbms_output.put_line('Grade:'||v_grade||',the result is '||p_grade);
 17   end;
 18   /
输入 grade 的值:  a
原值    2:  v_grade char(1):=upper('&grade');
新值    2:  v_grade char(1):=upper('a');
Grade:A,the result is Excellent

PL/SQL 过程已成功完成。


----------
//loop循环 计算1+2+3+...+100的和

SQL> set serveroutput on
SQL>
SQL> declare
  2  counter number(3):=0;
  3  sumResult number:=0;
  4  begin
  5  loop
  6  counter := counter+1;
  7  sumResult :=sumResult+counter;
  8  if counter>=100 then exit;
  9  end if;
 10  end loop;
 11  dbms_output.put_line('result is:'||to_char(sumResult));
 12  end;
 13  /
result is:5050

PL/SQL 过程已成功完成。


----------
for循环

SQL>  set serveroutput on
SQL>  declare
  2   counter number(3):=0;
  3   sumResult number:=0;
  4   begin
  5   while counter<100 loop
  6   counter:=counter+1;
  7   sumResult:=sumResult+counter;
  8   end loop;
  9   dbms_output.put_line('result is:'||sumResult);
 10  end;
 11  /
result is:5050

PL/SQL 过程已成功完成。


----------
SQL>  set serveroutput on
SQL>  declare
  2   sumsal emp.sal % type;
  3    begin
  4    select sum(sal) into sumsal from emp;
  5    if sumsal>20000 then
  6    goto first_label;
  7  else
  8  goto second_label;
  9  end if;
 10  <<first_label>>
 11  dbms_output.put_line('ABOVE 20000:'||sumsal);
 12  <<second_label>>
 13  null;
 14  end;
 15  /
ABOVE 20000:29025

PL/SQL 过程已成功完成。


----------
SQL> select * from emp;

ENAME                       SAL       COMM         ID
-------------------- ---------- ---------- ----------
SMITH                       800
ALLEN                      1600        800
WARD                       1250        500
JONES                      2975
MARTIN                     1250       1400
BLAKE                      2850
CLARK                      2450
SCOTT                      3000
KING                       5000
TURNER                     1500          0
ADAMS                      1100

ENAME                       SAL       COMM         ID
-------------------- ---------- ---------- ----------
JAMES                       950
FORD                       3000
MILLER                     1300

已选择14行。

SQL> select sum(sal) from emp;

  SUM(SAL)
----------
     29025

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324908976&siteId=291194637