declare v_temp number(1) := 0; v_count binary_integer := 0; v_sal number(7, 2) := 4000.00; v_date date := sysdate; v_pi constant number(3, 2) := 3.14; -- 常量 v_valid boolean := false; v_name varchar2(20) not null := 'MyName'; -- 不能取空值 begin dbms_output.put_line('v_temp value:' || v_temp); end;
declare v_empno number(4); -- 自定义类型 v_empno2 emp.empno%type; -- emp表的empno字段的类型 v_empno3 v_empno2%type; -- v_empno2的类型 begin dbms_output.put_line('test %type'); end;
declare type type_table_empno -- 声明 类型 is table of emp.empno%type -- 指定是 table类型 以及每个元素的类型 index by binary_integer; -- 指定下标的类型 -- 声明一个 type_table_empno 类型的变量 v_empnos type_table_empno; begin v_empnos(0) := 1111; v_empnos(1) := 2222; v_empnos(2) := 3333; dbms_output.put_line(v_empnos(1)); end;
declare type type_record_dept is record ( deptno dept.deptno%type, dname dept.dname%type, loc dept.loc%type ); v_temp type_record_dept; begin v_temp.deptno := 50; v_temp.dname := 'zhangsan'; dbms_output.put_line('v_temp.deptno ' || v_temp.deptno); dbms_output.put_line('v_temp.dname ' || v_temp.dname); end;
declare v_temp dept%rowtype; begin v_temp.deptno := 55; v_temp.dname := 'zhangsan'; v_temp.loc := 'WuHan'; dbms_output.put_line('v_temp.deptno ' || v_temp.deptno); dbms_output.put_line('v_temp.dname ' || v_temp.dname); dbms_output.put_line('v_temp.loc ' || v_temp.loc); end;
declare v_ename emp.ename%type; v_sal emp.sal%type; begin select ename, sal into v_ename, v_sal from emp where empno = 7566; dbms_output.put_line('v_ename ' || v_ename); dbms_output.put_line('v_sal ' || v_sal); end;
declare v_emp emp%rowtype; begin select * into v_emp from emp where empno = 7566; dbms_output.put_line('v_emp.empno ' || v_emp.empno); dbms_output.put_line('v_emp.ename ' || v_emp.ename); dbms_output.put_line('v_emp.sal ' || v_emp.sal); end;
create table dept2 as select * from dept; declare v_deptno dept.deptno%type := 50; v_dname dept.dname%type := 'game'; v_loc dept.loc%type := 'WuHan'; begin insert into dept2 values(v_deptno, v_dname, v_loc); commit; end;
注: 关键字sql, 其属性rowcount, 刚刚执行的语句影响了几条记录 create table emp2 as select * from emp; declare v_deptno emp2.deptno%type := 10; v_count number; begin update emp2 set sal = sal / 2 where deptno = v_deptno; -- select count(*) into v_count from emp; --影响一条记录 dbms_output.put_line(sql%rowcount || ' 条记录被影响'); commit; end;
declare i binary_integer := 1; begin loop dbms_output.put_line(i); i := i + 1; exit when ( i >= 11 ); end loop; end;
declare i binary_integer := 1; begin while i < 11 loop dbms_output.put_line(i); i := i + 1; end loop; end;
begin for i in 1..10 loop dbms_output.put_line(i); end loop; end;
begin for i in reverse 1..10 loop dbms_output.put_line(i); end loop; end;
declare x number := 1; begin if x < 10 then dbms_output.put_line(x || ' is less than 10'); end if; end;
declare x number(2) := 11; begin if x < 10 then dbms_output.put_line(x || ' is less than 10'); else dbms_output.put_line(x || ' is bigger than 10'); end if; end;
declare x number(2) := 10; begin if x < 10 then dbms_output.put_line(x || ' is less than 10'); elsif x < 20 then dbms_output.put_line(x || ' is less then 20'); else dbms_output.put_line(x || ' is bigger than 20'); end if; end;
declare v_temp number(4); begin select empno into v_temp from emp where deptno = 10; exception when too_many_rows then dbms_output.put_line('太多记录了'); when others then dbms_output.put_line('error'); end;
declare v_temp number(4); begin select empno into v_temp from emp where empno = 2222; exception when no_data_found then dbms_output.put_line('没有数据'); end;
-- 记录错误的日志表 create table errorlog ( id number primary key, errorcode number, errormsg varchar2(1024), errordate date ); -- 创建序列 create sequence seq_errorlog_id start with 1 increment by 1; -- 出错后, 插入错误日志表, SQLCODE, SQLERRM declare v_deptno dept.deptno%type := 10; v_errorcode errorlog.errorcode%type; v_errormsg errorlog.errormsg%type; begin delete from dept where deptno = v_deptno; commit; exception when others then rollback; v_errorcode := SQLCODE; v_errormsg := SQLERRM; insert into errorlog values(seq_errorlog_id.nextval, v_errorcode, v_errormsg, sysdate); commit; end; -- clean -- drop sequence seq_errorlog_id; -- drop table errorlog;
case sex when '1' then '男' when '2' then '女' else '其他' end
case when sex = '1' then '男' when sex = '2' then '女' else '其他' end
SELECT grade, COUNT (CASE WHEN sex = 1 THEN 1 /*sex 1为男生,2位女生*/ ELSE NULL END) 男生数, COUNT (CASE WHEN sex = 2 THEN 1 ELSE NULL END) 女生数 FROM students GROUP BY grade;
SELECT T2.*, T1.* FROM T1, T2 WHERE (CASE WHEN T2.COMPARE_TYPE = 'A' AND T1.SOME_TYPE LIKE 'NOTHING%' THEN 1 WHEN T2.COMPARE_TYPE != 'A' AND T1.SOME_TYPE NOT LIKE 'NOTHING%' THEN 1 ELSE 0 END) = 1
SELECT CASE WHEN salary <= 500 THEN '1' WHEN salary > 500 AND salary <= 600 THEN '2' WHEN salary > 600 AND salary <= 800 THEN '3' WHEN salary > 800 AND salary <= 1000 THEN '4' ELSE NULL END salary_class, -- 别名命名 COUNT(*) FROM Table_A GROUP BY CASE WHEN salary <= 500 THEN '1' WHEN salary > 500 AND salary <= 600 THEN '2' WHEN salary > 600 AND salary <= 800 THEN '3' WHEN salary > 800 AND salary <= 1000 THEN '4' ELSE NULL END;