Fundamentals of Oracle

declare
    msg varchar2(20);
    msg1 varchar2(20);
begin
   msg:= 1/0;
   msg:='Who are you';
   DBMS_OUTPUT.PUT_line(msg);
    DBMS_OUTPUT.PUT_line(msg1);
    EXCEPTION
     when others then   
     DBMS_OUTPUT. PUT_line('There is an error');
end;


conn sys/sys as sysdba;
CREATE user tangtang identified by tang1350;

declare
   msg varchar(20);
begin
  msg:='Hello bad guy';
DBMS_OUTPUT.put_line(msg);
end;
-- Create employee table and department table
create table dept(
deptno int primary key,
dname varchar2(20),
loc varchar2(100)
);

create table emp(
empno int primary key,
ename varchar2(20),
job varchar2(20),
mgr int,
hiredate date,
sal number(7,2),
comm number(7,2),
deptno int,
foreign key (deptno) references dept(deptno)
);

insert into dept (deptno,dname,loc) values (10,'accounting','new york');
insert into dept (deptno,dname,loc) values(20,'RESEARCH','DALLAS');
insert into dept (deptno,dname,loc) values (30,'sale','wuhan');
insert into dept (deptno,dname,loc) values(40,'IT','beijing');
insert into dept (deptno,dname,loc) values(50,'teaching','wuhan');
insert into dept (deptno,dname,loc) values(60,'personnel','wuhan');
insert into dept (deptno,dname,loc) values(70,'development','beijing');



insert  into emp  values (7369,'张三','boos',NULL,sysdate,10000.00,10000.00,10);
insert  into emp  values (7370,'smi','clerk',7369,sysdate,'800.00','800',70);
insert  into emp  values (7934,'miller','clerk',7369,to_date('2011-01-01','yyyy-mm-dd'),'1300.00','1000',10);
insert  into emp  values (7935,'silence','salesman',7369,to_date('2017-03-15','YYYY-MM-DD'),'4000.00','4100',70);
insert  into emp  values (7936,'tomn','salesman',7934,to_date('2017-02-15','yyyy-mm-dd'),'4500.00','4444',10);
insert  into emp  values (7937,'cat','teacher',7934,to_date('2017-03-23','yyyy-mm-dd'),'12345.00','12345',10);
insert  into emp values (7938,'zhangsan','salesman',7369,to_date('2017-04-19','yyyy-mm-dd'),'3000.00','3000',70);
insert  into emp  values (7939,'lisi','salesman',7937,to_date('2016-11-17','yyyy-mm-dd'),'11111.00','11111',70);
insert  into emp  values (7940,'wangwu','teacher',7937,to_date('2016-10-13','yyyy-mm-dd'),'7000.00','8888',40);
insert  into emp   values (7941,'zhaoliu','docter',7937,to_date('2017-01-24','yyyy-mm-dd'),'3000.00','3000',40);
insert  into emp values (7942,'xiaoming','MANAGER',7937,to_date('2016-08-11','yyyy-mm-dd'),'4600.00','5555',70);



DECLARE
  sname varchar(20) :='廖宁';
begin
  sname :=sname ||'湖南';
  dbms_output.put_line(sname);
end;


declare

  sname varchar(20);
begin
select ename into sname from EMP where empno=7369;
dbms_output.put_line(sname);
end;

-- get the type of a column of data
declare
  sname emp.ename%type;
begin
   select ename into sname from emp where empno=7369;
   dbms_output.put_line(sname);
end;

-- get the type of a piece of data
declare
emps emp%rowtype;
begin
select * into emps from emp where empno =7369;
dbms_output.put_line(emps.job);
end;


- - Specify the type of some fields to
declare
    - Create a type and add the required fields according to your needs
     type empa is record(
       ename emp.ename%type,
       job  emp.job%type  
    );
    -- 定义变量
     emps empa;
begin
select ename,job into emps from emp where empno=7369;
  dbms_output.put_line(emps.ename);
end;



declare
  ssal emp.sal%type;
 
begin
select sal into ssal from emp where empno=7369;

if ssal>10000 then
   update emp set comm =300 where empno =7369; 
   elsif ssal>8000 then
   update emp set comm =8000 where empno=7369;
   else
   update emp set comm=800 where empno =7369;
end if;
    commit;
end;

declare

v_grade char(2) := ('&ooo');
begin
dbms_output.put_line(v_grade);
case v_grade
    when 'a' then
    dbms_output.put_line('优');
end case;
end;



declare
i_sum number(4):=0;
i_i number(3):=1;
begin
       loop
           if i_i>=101 then
           exit;
           end if;
       i_sum := i_sum+i_i;
        i_i := i_i+1;
      end loop;
dbms_output.put_line(i_sum);
end;

declare
i_sum number(4):=0;
i_i number(3):=1;
begin
while i_i<101 loop

       i_sum := i_sum+i_i;
        i_i := i_i+1;
      end loop;
dbms_output.put_line(i_sum);

end;


declare
i_sum number(4):=0;
i_i number(3):=1;
begin
for i_i in 1..100 loop
  i_sum:= i_sum+i_i;
end loop;
dbms_output.put_line(i_sum);
end;

declare
i_sum number(4):=0;
i_i number(3):=0;
begin
for i_i in reverse 1..100 loop
i_sum:=i_sum+i_i;
end loop;
dbms_output.put_line(i_sum);
end;


declare
sename emp.ename%type;
sempno emp.empno%type;
begin
sempno:=7369;
-- 动态Sql语句
execute immediate 'select ename from emp where empno=:1' into sename using sempno;
dbms_output.put_line(sename);
end;

begin
execute immediate 'create table liang(empno number,ename varchar(2))';
end;

declare
- - define the cursor
cursor liang is select ename,job from emp;
sname emp.ename%type;
sjob emp.job%type;
begin
  -- determine whether the cursor is open and open her
   if not liang%isopen then
   open liang;
   end if;
   -- grab the value of a cursor
   fetch liang into sname,sjob;
   dbms_output.put_line(sname);
   -- close the cursor
   close liang; 
end;


declare
    cursor liang is select ename,job from emp;
    begin
     for emps in liang loop
      dbms_output.put_line(emps.ename||''||emps.job);
     end loop;
end;

-- 动态强游标
declare
  type empcursor is ref cursor return emp%rowtype;
  emps empcursor;
  emp1 emps%rowtype;
begin
open emps for select * from emp;
loop
   fetch emps into emp1;
  exit when emps%notfound;
   dbms_output.put_line(emp1.ename);
  end loop;

end;

Guess you like

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