博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Oracle PL/SQL笔记
阅读量:2395 次
发布时间:2019-05-10

本文共 5656 字,大约阅读时间需要 18 分钟。

declare   v_first_name varchar2(35);   v_last_name varchar2(35);   c_student_id constant number := 2223; begin   select first_name, last_name   into v_first_name, v_last_name   from student   where student_id = c_student_id;   dbms_output.put_line('Student name: ' || v_first_name || ' ' || v_last_name); exception   when no_data_found then        dbms_output.put_line('There is no student with student id '||c_student_id); end;
BEGIN    INSERT INTO ZIPCODE (zip, city, state, created_by, created_date,         modified_by, modified_date)    VALUES ('10027', 'NEW YORK', 'NY', USER, SYSDATE, USER, SYSDATE);    COMMIT; EXCEPTION    WHEN OTHERS THEN       DECLARE          v_err_code NUMBER := SQLCODE;          v_err_msg VARCHAR2(100) := SUBSTR(SQLERRM, 1, 100);       BEGIN          DBMS_OUTPUT.PUT_LINE ('Error code: '||v_err_code);          DBMS_OUTPUT.PUT_LINE ('Error message: '||v_err_msg);       END; END;
declare   --rowtype:行记录类型   vr_student student%rowtype; begin   select *   into vr_student   from student   where student_id = 156;   --用“.”来访问行记录属性   dbms_output.put_line(vr_student.first_name || ' ' || vr_student.street_address || ' has an ID of 156'); exception   when no_data_found then        raise_application_error(-20001,'the student is not in the database'); end; declare        cursor c_zip is  -- 声明游标        select *        from zipcode;        vr_zip c_zip%rowtype; --游标行类型 begin        open c_zip;  --打开游标        loop             fetch c_zip into vr_zip; --对游标集合内的每个数据行操作             exit when c_zip%notfound;             dbms_output.put_line(vr_zip.city);        end loop; -- loop结束,自动关闭游标 end; DECLARE    v_sid      student.student_id%TYPE;    CURSOR c_student IS       SELECT student_id         FROM student        WHERE student_id < 110; BEGIN    OPEN c_student;    LOOP       FETCH c_student INTO v_sid;       EXIT WHEN c_student%NOTFOUND;         DBMS_OUTPUT.PUT_LINE('STUDENT ID : '||v_sid);    END LOOP;    CLOSE c_student;  EXCEPTION    WHEN OTHERS    THEN       IF c_student%ISOPEN       THEN          CLOSE c_student;       END IF; END; DECLARE    CURSOR c_student_enroll IS       SELECT s.student_id, first_name, last_name,              COUNT(*) enroll,              (CASE                    WHEN count(*) = 1 Then ' class.'                   WHEN count(*) is null then                                ' no classes.'                   ELSE ' classes.'               END) class                             FROM student s, enrollment e        WHERE s.student_id = e.student_id          AND s.student_id <110        GROUP BY s.student_id, first_name, last_name;    r_student_enroll    c_student_enroll%ROWTYPE; BEGIN    OPEN c_student_enroll;    LOOP       FETCH c_student_enroll INTO r_student_enroll;       EXIT WHEN c_student_enroll%NOTFOUND;       DBMS_OUTPUT.PUT_LINE('Student INFO: ID '||          r_student_enroll.student_id||' is '||          r_student_enroll.first_name|| ' ' ||          r_student_enroll.last_name||          ' is enrolled in '||r_student_enroll.enroll||          r_student_enroll.class);    END LOOP;    CLOSE c_student_enroll; EXCEPTION    WHEN OTHERS    THEN     IF c_student_enroll %ISOPEN       THEN     CLOSE c_student_enroll;     END IF; END; DECLARE    CURSOR c_group_discount IS       SELECT DISTINCT s.course_no         FROM section s, enrollment e        WHERE s.section_id = e.section_id         GROUP BY s.course_no, e.section_id, s.section_id        HAVING COUNT(*)>=8; BEGIN    FOR r_group_discount IN c_group_discount   LOOP       UPDATE course          SET cost = cost * .95        WHERE course_no = r_group_discount.course_no;    END LOOP;    COMMIT; END; DECLARE    CURSOR c_group_discount IS       SELECT DISTINCT s.course_no         FROM section s, enrollment e        WHERE s.section_id = e.section_id         GROUP BY s.course_no, e.section_id, s.section_id        HAVING COUNT(*)>=8;    v_course_no c_group_discount%rowtype; BEGIN    open c_group_discount;    LOOP       fetch c_group_discount into v_course_no;       UPDATE course          SET cost = cost * .95        WHERE course_no = v_course_no.course_no;    END LOOP;    COMMIT; END;
select count(*) from student; select * from student; select * from student where created_by = 'xls'; create or replace trigger student_bi before insert on student for each row declare     v_student_id student.student_id%type; begin     select student_id_seq.nextval     into v_student_id     from dual;     :new.student_id := v_student_id;     :new.created_by := user;     :new.created_date := sysdate;     :new.modified_by := user;     :new.modified_date := sysdate; end; INSERT INTO student (student_id, first_name, last_name, zip,     registration_date, created_by, created_date, modified_by,      modified_date) VALUES (STUDENT_ID_SEQ.NEXTVAL, 'John', 'Smith', '00914', SYSDATE,     USER, SYSDATE, USER, SYSDATE); INSERT INTO student (first_name, last_name, zip, registration_date) VALUES ('John', 'Smith', '00914', SYSDATE); create or replace trigger instructor_aud after update or delete on instructor declare       v_type varchar2(10);       pragma autonomous_transaction; begin       if UPDATING then          v_type := 'UPDATE';       elsif deleting then          v_type := 'DELETE';       end if;       update statistics       set transaction_user = user,           transaction_date = sysdate       where table_name = 'INSTRUCTOR'             and transaction_name = v_type;       if sql%notfound then          insert into statistics          values('INSTRUCTOR', v_type, user, sysdate);       end if;       commit; end; UPDATE instructor    SET phone = '7181234567'  WHERE instructor_id = 101; SELECT *   FROM statistics;

转载地址:http://gnwob.baihongyu.com/

你可能感兴趣的文章
UVA100 The 3n + 1 problem
查看>>
hdu1754 I Hate It
查看>>
hdu 1166 敌兵布阵(求区间的和,单节点更新)
查看>>
hiho一下 第四十四周 题目1 : 博弈游戏·Nim游戏
查看>>
poj2299 Ultra-QuickSort(线段树计数问题)
查看>>
hdu4565 So Easy!(矩阵快速幂)
查看>>
poj2528 Mayor's posters(线段树,离散化)
查看>>
线段树多lazy-tag(两个)
查看>>
hdu4578(三个更新操作,三个求值操作)
查看>>
并查集(初级)小结
查看>>
Treap
查看>>
编译原理 词法分析
查看>>
计算机系统结构 计算机指令集结构
查看>>
计算机系统结构 输入/输出系统
查看>>
信息安全技术及应用 常规加密技术
查看>>
02-线性结构1 两个有序链表序列的合并
查看>>
HDU 1080 DP LCS
查看>>
HDU 3308 线段树+区间合并
查看>>
ASP.NET 入手页面控件及事件触发
查看>>
HDU 4123 树状DP+RMQ
查看>>