`
zzzzzz5530041
  • 浏览: 34308 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

PL/SQL复习

阅读更多
 DESC STUDENTTABLE
SET SERVEROUTPUT ON;
declare 
v_str1 VARCHAR2(10);
v_str2 VARCHAR2(50);
begin
  v_str1:='hello';
  select STUDENT_NAME into v_str2 from STUDENTTABLE where STUDENT_NO=1;
  dbms_output.put_line(v_str1 ||', '|| v_str2);
  
  EXCEPTION
    WHEN NO_DATA_FOUND THEN
      dbms_output.put_line('NO DATA FOUND');
end;
-------------------

--------------------------------------------------------VIEW-----------------------------------------------
--------------------------------------------------------VIEW-----------------------------------------------
--------------------------------------------------------VIEW-----------------------------------------------
--------------------------------------------------------VIEW-----------------------------------------------
---------CREATE A VIEW
CREATE OR REPLACE VIEW TEST_VIEW
AS
SELECT * FROM STUDENTTABLE WHERE ROWNUM<10;
--QUERY DATA FROM TEST_VIEW
SELECT * FROM TEST_VIEW;
-----DROP VIEW
DROP VIEW TEST_VIEW;

--------------------------------------------------------SYNONYM-----------------------------------------------
--------------------------------------------------------SYNONYM-----------------------------------------------
--------------------------------------------------------SYNONYM-----------------------------------------------
--------------------------------------------------------SYNONYM-----------------------------------------------
CREATE SYNONYM SYN_PHRASES FOR CITIAP_USER; --HERE I LOGIN AS JFPAP USER TO CREATE A SYNONYM FOR CITIAP USER, TABLE WAS CREATED BY CITIAP USER.
GRANT ALL ON CITIAP.PHRASES TO JFPAPUSER;-- HERE I LOGIN AS CITIAP USER TO GRANT TABLE PHRASES TO JFPAP USER

--------------------------------------------------------SEQUENCE-----------------------------------------------
--------------------------------------------------------SEQUENCE-----------------------------------------------
--------------------------------------------------------SEQUENCE-----------------------------------------------
--------------------------------------------------------SEQUENCE-----------------------------------------------
--CREATE A SEQUENCE
CREATE SEQUENCE MY_SEQ INCREMENT BY 1 START WITH 1 MAXVALUE 10;
CREATE SEQUENCE MY_SEQ;
--DROP SEQUENCE
DROP SEQUENCE MY_SEQ;
--QUERY SEQUECE VALUE
SELECT MY_SEQ.CURRVAL FROM DUAL;--CURRENT VALUE
SELECT MY_SEQ.NEXTVAL FROM DUAL;--NEXT VALUE

--------------------------------------------------------PL/SQL BLOCK-----------------------------------------------
--------------------------------------------------------PL/SQL BLOCK-----------------------------------------------
--------------------------------------------------------PL/SQL BLOCK-----------------------------------------------
--------------------------------------------------------PL/SQL BLOCK-----------------------------------------------
DECLARE 
  V_STUDENT_NAME VARCHAR2(50);
BEGIN
  select STUDENT_NAME into V_STUDENT_NAME from STUDENTTABLE where STUDENT_NO=&NO;
  dbms_output.put_line(V_STUDENT_NAME);
END;


--------------------------------------------------------PROCEDURE-----------------------------------------------
--------------------------------------------------------PROCEDURE-----------------------------------------------
--------------------------------------------------------PROCEDURE-----------------------------------------------
--------------------------------------------------------PROCEDURE-----------------------------------------------
CREATE OR REPLACE PROCEDURE UPDATE_NAME(NEWSTUNAME VARCHAR2,STUNO VARCHAR2) IS
V_NAME VARCHAR2(10);
BEGIN
  UPDATE STUDENTTABLE SET STUDENT_NAME=NEWSTUNAME WHERE student_no=STUNO;
  COMMIT;
  SELECT STUDENT_NAME INTO V_NAME FROM studenttable WHERE student_no=STUNO;
  dbms_output.put_line('NAME WAS UPDATED, NEW NAME IS '|| V_NAME);
END;
CALL UPDATE_NAME('??','1');

--------------------------------------------------------FUNCTION-----------------------------------------------
--------------------------------------------------------FUNCTION-----------------------------------------------
--------------------------------------------------------FUNCTION-----------------------------------------------
--------------------------------------------------------FUNCTION-----------------------------------------------
CREATE OR REPLACE FUNCTION FINDSTUNAME(STU_NO   VARCHAR2)RETURN VARCHAR2 is 
  STU_NAME STUDENTTABLE.STUDENT_NAME%TYPE;
BEGIN
  SELECT STUDENT_NAME INTO STU_NAME FROM STUDENTTABLE WHERE student_no=STU_NO;
  RETURN STU_NAME;
END;

 

--------------------------------------------------------PACKAGE-----------------------------------------------
--------------------------------------------------------PACKAGE-----------------------------------------------
--------------------------------------------------------PACKAGE-----------------------------------------------
--------------------------------------------------------PACKAGE-----------------------------------------------
CREATE OR REPLACE PACKAGE STU_PKG AS
  PROCEDURE PROCEDURE UPDATE_NAME (NEWSTUNAME VARCHAR2 , STUNO VARCHAR2) ;
  FUNCTION FINDSTUNAME ( STU_NO   VARCHAR2 ) RETURN VARCHAR2 ;
 END ;
 
 --PACKAG BODY
CREATE PACKAGE BODY STU_PKG_BODY IS
  PROCEDURE UPDATE_NAME(NEWSTUNAME VARCHAR2,STUNO VARCHAR2) IS
V_NAME VARCHAR2(10);
BEGIN
  UPDATE STUDENTTABLE SET STUDENT_NAME=NEWSTUNAME WHERE student_no=STUNO;
  COMMIT;
  SELECT STUDENT_NAME INTO V_NAME FROM studenttable WHERE student_no=STUNO;
  dbms_output.put_line('NAME WAS UPDATED, NEW NAME IS '|| V_NAME);
END;
FUNCTION FINDSTUNAME(STU_NO   VARCHAR2)RETURN VARCHAR2 is 
  STU_NAME VARCHAR2(10) ;
BEGIN
  SELECT STUDENT_NAME INTO STU_NAME FROM STUDENTTABLE WHERE student_no=STU_NO;
  RETURN STU_NAME;
END;
END;

--------------------------------------------------------TRIGGER-----------------------------------------------
--------------------------------------------------------TRIGGER-----------------------------------------------
--------------------------------------------------------TRIGGER-----------------------------------------------
--------------------------------------------------------TRIGGER-----------------------------------------------
CREATE OR REPLACE TRIGGER UPDATE_STU_TRIGGER
BEFORE INSERT  
ON STUDENTTABLE
FOR EACH ROW
BEGIN
 
    SELECT MY_SEQ.NEXTVAL  INTO :NEW.STUDENT_NO FROM DUAL;
  
END;
 insert into STUDENTTABLE(student_name) values('hello');
 commit;
 select * from STUDENTTABLE;
--------------------------------------------------------IF-----------------------------------------------
--------------------------------------------------------IF-----------------------------------------------
--------------------------------------------------------IF-----------------------------------------------
--------------------------------------------------------IF-----------------------------------------------
SELECT * FROM STUDENTTABLE;
CREATE OR REPLACE FUNCTION FINDFLAG(STU_NO IN VARCHAR2) RETURN VARCHAR2 IS
  v_flag VARCHAR2(10);
  v_name STUDENTTABLE.STUDENT_NAME%TYPE;
  
BEGIN
  SELECT STUDENT_NAME INTO v_name FROM  STUDENTTABLE WHERE student_no=STU_NO;
  IF '??'=v_name THEN
    v_flag:=v_name||' 2';
    RETURN v_flag;
  ELSIF '??'=v_name THEN
    v_flag:=v_name||' 3';
    RETURN v_flag;
  ELSE 
    v_flag:='NOT FOUND!!!';
    RETURN v_flag;
  END IF;
END;

--------------------------------------------------------CASE WHEN-----------------------------------------------
--------------------------------------------------------CASE WHEN-----------------------------------------------
--------------------------------------------------------CASE WHEN-----------------------------------------------
--------------------------------------------------------CASE WHEN-----------------------------------------------

CREATE OR REPLACE FUNCTION FINDFLAG2(STU_NO IN VARCHAR2) RETURN VARCHAR2 IS
  v_flag VARCHAR2(10);
  v_name STUDENTTABLE.STUDENT_NAME%TYPE;
  
BEGIN
  SELECT STUDENT_NAME INTO v_name FROM  STUDENTTABLE WHERE student_no=STU_NO;
  CASE
  WHEN  '??'=v_name THEN
    v_flag:=v_name||' 2';
    RETURN v_flag;
  WHEN '??'=v_name THEN
    v_flag:=v_name||' 3';
    RETURN v_flag;
  ELSE  v_flag:='NOT FOUND!!!';
    RETURN v_flag;
  END CASE;
END;

--------------------------------------------------------LOOP-----------------------------------------------
--------------------------------------------------------LOOP-----------------------------------------------
--------------------------------------------------------LOOP-----------------------------------------------
--------------------------------------------------------LOOP-----------------------------------------------
SELECT * FROM STUDENTTABLE;
DECLARE 
  i INT:=1;
BEGIN
  LOOP
    INSERT INTO STUDENTTABLE(STUDENT_NAME) VALUES('STUDENT '|| i);
    EXIT WHEN i=5;
    i:=i+1;
    END LOOP;
    COMMIT;
END;
--------------------------------------------------------WHILE-----------------------------------------------
--------------------------------------------------------WHILE-----------------------------------------------
--------------------------------------------------------WHILE-----------------------------------------------
--------------------------------------------------------WHILE-----------------------------------------------

DECLARE

 i INT:=6;
BEGIN

  WHILE i<10 LOOP
    INSERT INTO STUDENTTABLE(STUDENT_NAME) VALUES('STUDENT '|| i);
    i:=i+1;
  END LOOP;
END;
--------------------------------------------------------FOR-----------------------------------------------
--------------------------------------------------------FOR-----------------------------------------------
--------------------------------------------------------FOR-----------------------------------------------
--------------------------------------------------------FOR-----------------------------------------------
BEGIN

  FOR i IN 10..15 LOOP
    INSERT INTO STUDENTTABLE(STUDENT_NAME) VALUES('STUDENT '|| i);
  END LOOP;
  END;
--------------------------------------------------------EXCEPTION-----------------------------------------------
--------------------------------------------------------EXCEPTION-----------------------------------------------
--------------------------------------------------------EXCEPTION-----------------------------------------------
--------------------------------------------------------EXCEPTION-----------------------------------------------
DECLARE 
  E_INVALID_INPUT EXCEPTION;
  V_STU_NAME studenttable.STUDENT_NAME%TYPE;
  V_STU_NO VARCHAR2(10):=&STU_NO;
BEGIN
    IF V_STU_NO='2' THEN
      RAISE E_INVALID_INPUT;
    END IF;
    SELECT STUDENT_NAME INTO V_STU_NAME FROM studenttable WHERE student_no=V_STU_NO;
    dbms_output.put_line('HELLO, '||V_STU_NAME);
    EXCEPTION 
      WHEN E_INVALID_INPUT THEN
      dbms_output.put_line('INPUT ERROR');
      
END;

--------------------------------------------------------IMPLICIT CURSOR-----------------------------------------------
--------------------------------------------------------IMPLICIT CURSOR-----------------------------------------------
--------------------------------------------------------IMPLICIT CURSOR-----------------------------------------------
--------------------------------------------------------IMPLICIT CURSOR-----------------------------------------------

---SQL%FOUND && SQL%NOTFOUND && SQL%ROWCOUND
DECLARE
  V_STU_NAME studenttable.STUDENT_NAME%TYPE;
  V_ROW_STU studenttable%ROWTYPE;
BEGIN 
  UPDATE studenttable SET STUDENT_NAME='CITI' WHERE student_no=&STU_NO
    RETURNING STUDENT_NAME INTO V_STU_NAME; --USING RETURNING
  IF SQL%FOUND THEN
    dbms_output.put_line('DATA UPDATED, UPDATED ROW COUNT '|| SQL%ROWCOUNT|| ' , NEW NAME IS '|| V_STU_NAME);
  END IF;
  IF SQL%NOTFOUND THEN
    dbms_output.put_line('DATA NOT FOUND...');
  END IF;
  SELECT * INTO V_ROW_STU FROM studenttable WHERE student_NAME=V_STU_NAME;
  dbms_output.put_line('V_ROW_STU ====> '|| V_ROW_STU.student_no||', '|| V_ROW_STU.STUDENT_NAME);
END;
SELECT * FROM STUDENTTABLE;

--USING IMPLICIT CURSOR ,ORACLE WILL EXECUTE OPEN, FETCH,CLOSE .

--BELOW 2 PROCEDURE A ONE EXAMPLE
--BELOW IS TO REMOVE ONE RECORD FROM TABLE
CREATE OR REPLACE PROCEDURE PRO_REMOVE_STU(V_STU_NO IN studenttable.student_no%TYPE)
IS
BEGIN
  DELETE FROM studenttable WHERE STUDENT_NO=V_STU_NO;
END;

--BELOW IS TRY TO QUERY RECORD COUNT, BUT GET FAIL

CREATE OR REPLACE PROCEDURE PRO_QUERY_COUNT1
IS
  V_COUNT INTEGER;
BEGIN
  SELECT COUNT(*) INTO V_COUNT FROM studenttable; --10 RECORDS IN TABLE
  PRO_REMOVE_STU('9999'); --CALL PRO_REMOVE_STU(), BUT 9999 WAS NOT IN DB.
  dbms_output.put_line(SQL%ROWCOUNT ||' RECORDS WAS FOUND IN TABLE'); --WILL PRINT : 0 RECORDS WAS FOUND IN TABLE .BECAUSE SQL%ROWCOUNT WILL ONLY RECORD THE LATEST DML EXECUTION. DUE TO 9999 IS NOT IN DB,SO THE DELETE RETURN 0.
END;

-- TO FIX PRO_QUERY_COUNT1 ISSUE 
CREATE OR REPLACE PROCEDURE PRO_QUERY_COUNT2
IS
  V_COUNT INTEGER;
  V_FOUND_NUM INTEGER;
  V_DELETE_COUNT INTEGER;
BEGIN
  SELECT COUNT(*) INTO V_COUNT FROM studenttable; --1 RECORDS IN TABLE
  V_FOUND_NUM:= SQL%ROWCOUNT;
  PRO_REMOVE_STU('9999'); --CALL PRO_REMOVE_STU(), BUT 9999 WAS NOT IN DB.
  V_DELETE_COUNT:=SQL%ROWCOUNT;
  dbms_output.put_line('TOTAL RECORD NUMBER OF TABLE: '||V_FOUND_NUM || ', REMOVE COUNT NUMBER: '||V_DELETE_COUNT);
END;
 
--------------------------------------------------------EXPLICIT CURSOR-----------------------------------------------
--------------------------------------------------------EXPLICIT CURSOR-----------------------------------------------
--------------------------------------------------------EXPLICIT CURSOR-----------------------------------------------
--------------------------------------------------------EXPLICIT CURSOR-----------------------------------------------

DECLARE 
  CURSOR CUR_STU IS SELECT STUDENT_NO,STUDENT_NAME FROM studenttable;
  V_STU_NO studenttable.STUDENT_NO%TYPE;
  V_STU_NAME studenttable.STUDENT_NAME%TYPE;
BEGIN
  OPEN  CUR_STU;
  LOOP
  FETCH CUR_STU INTO V_STU_NO,V_STU_NAME;
  dbms_output.put_line('STU_NO: '|| V_STU_NO||', STU_NAME: '|| V_STU_NAME);
  EXIT WHEN CUR_STU%NOTFOUND ;
  END LOOP;
  CLOSE CUR_STU;
END;


----use FOR LOOP TO FETCH CURSOR DATA

DECLARE 
  CURSOR CUR_STU IS SELECT STUDENT_NO,STUDENT_NAME FROM studenttable;
BEGIN
  FOR CUR_STU_TEMP IN CUR_STU
  LOOP
    dbms_output.put_line('STU_NO: '|| CUR_STU_TEMP.STUDENT_NO ||', STU_NAME: '|| CUR_STU_TEMP.STUDENT_NAME);
  END LOOP;
   
END;



--------------------------------------------------------TRANSACTION-----------------------------------------------
--------------------------------------------------------TRANSACTION-----------------------------------------------
--------------------------------------------------------TRANSACTION-----------------------------------------------
--------------------------------------------------------TRANSACTION-----------------------------------------------
--COMMIT
--ROLLBACK
--SAVEPOINT
--LOCK TABLE

 

0
1
分享到:
评论

相关推荐

    Oracle中SQL以及PL/SQL复习专用

    根据提供的文件信息,我们可以归纳出一系列关于...以上内容涵盖了Oracle数据库中SQL与PL/SQL的基础知识和常用功能,对于学习和复习都非常有用。理解并熟练掌握这些概念和技术,将有助于提高数据库管理和开发的能力。

    PL/SQL基础

    Oracle PL/SQL是一种在Oracle数据库环境中用于开发存储过程、函数、触发器和其他数据库对象的编程语言。它是SQL的扩展,提供了更高级别的控制结构和流程逻辑,使得数据库管理与应用程序开发更加灵活和强大。 PL/SQL...

    oracle复习笔记之PL/SQL程序所要了解的知识点

    在Oracle数据库环境中,PL/SQL是一种强大的编程语言,它扩展了SQL的功能,使得数据库操作更加灵活和高效。本文将深入探讨PL/SQL程序设计中的一些关键知识点,包括基本语法、记录类型、流程控制、游标使用、异常处理...

    oracle第一天练习

    Oracle数据库是世界上最广泛使用的数据库系统之一,其PL/SQL(Procedural Language/Structured Query Language)编程接口,也就是我们常说的PROC,是Oracle特有的、用于数据库操作的强大编程语言。"Oracle第一天练习...

    数据结构课后题解答(C语言描述)

    数据结构是计算机科学中的核心课程之一,它探讨了如何有效地组织和管理数据,以便于高效地执行各种操作。《数据结构用C语言描述》是一本广泛...对于准备面试或参加编程竞赛的人来说,这也是一个很好的复习和提升工具。

    plsql中文

    2. 1Z0-007 Note.doc:1Z0-007是Oracle Certified Associate, Oracle Database 12c Administrator Certified Professional的考试代码,这个文档可能是针对该考试的复习笔记或学习指南,包含PL/SQL部分的要点,如数据...

    我们公司的内部oracle开发课件(我的珍藏)

    Oracle开发不仅涉及到SQL语言的使用,还包括PL/SQL编程、数据库设计、性能优化等多个方面。1000.jpg可能是一张包含关键概念或流程图的图片,用于辅助理解复杂的Oracle架构或操作步骤。新建 Microsoft PowerPoint ...

    Oracle选择题复习(带答案).doc

    本资源摘要信息涵盖了 Oracle 数据库的多个方面,包括数据库启动和关闭、参数文件、控制文件、表空间、PL/SQL 编程、异常处理、数据库性能等。 参数文件 * 在参数文件中,以 # 符号开头的行表示注释行。 * 创建一...

    y2复习题及答案快快抢啊,而且分还低,答案真实!!!

    - **PL/SQL**(Procedural Language for SQL)是一种过程化SQL扩展语言,用于实现复杂的数据库操作。 #### PL/SQL块组成 1. **选项A**:PL/SQL支持游标操作和事务命令,这是正确的。 2. **选项B**:声明部分在PL/...

    ssd5-exam1选择题题库

    在准备考试或复习相关知识点时,理解并掌握每一项基本概念都是至关重要的,这有助于提升解决问题的能力和编写高质量代码的能力。 综上所述,“ssd5-exam1选择题题库”覆盖了C++编程语言中的多个核心知识点,包括...

    orcl复习资料.docx

    在上述的复习资料中,主要涉及了两个重要的PL/SQL编程概念:循环和异常处理。 1. 循环: - 简单Loop循环、While循环和For循环是PL/SQL中的基本控制结构,用于重复执行一段代码直到满足特定条件为止。 - 任务是...

    ORACLE复习资料

    本复习资料主要涵盖了Oracle中的数据操作语言(DML)和过程化SQL(PL/SQL)的相关实例,这对于理解和掌握Oracle数据库的操作至关重要。 1. 数据操作语言(DML): DML是Oracle中用于插入、更新、删除和查询数据的...

    oracle复习资料

    oracle PL/sql语言复习资料,很全的oracle函数,sql语句,以及游标,存储过程,触发器等复习资料.

    ACCP6.0 S1考前理论部分

    3. **数据结构与数据库**:深入理解数组、链表、树、图等数据结构,以及关系型数据库的基本概念,如SQL语言和数据库设计原则。 4. **网络技术**:介绍计算机网络的基本原理,TCP/IP协议栈,网络通信模型,以及网络...

    Oracle期末考试复习题.pdf

    本题涉及到的知识点涵盖了Oracle数据库的基础概念、SQL语法、数据类型、数据库对象以及PL/SQL编程。 1. **Oracle数据库应用**:Oracle数据库的应用主要分为开发和管理两大部分。开发主要涉及编写SQL和PL/SQL代码来...

    良心出品oracle数据库期末复习.doc

    这篇文档"良心出品oracle数据库期末复习.doc"涵盖了Oracle数据库的基础知识,包括数据文件、日志文件、控制文件等数据库组件,以及数据库操作、表空间管理、用户权限、SQL语句和PL/SQL编程等内容。 一、Oracle...

Global site tag (gtag.js) - Google Analytics