PL/SQL programing 第六版学习笔记-1

    SQL> set serveroutput on/off
    SQL> set echo on/off  --if you want to see the original source from the file, use the SQL*Plus command SET ECHO ON.
        
    SQL> @abc.pkg  --調用腳本
    @@符號  --解決調用不在同一路徑
--顯示設置
    show all
--定義變量:
  1. --you can use the DEFINE command:
    SQL> DEFINE x = "the answer is 42"

--To view the value of x, specify:

    SQL> DEFINE x
     DEFINE X = "the answer is 42" (CHAR)
    SQL> SELECT '&x' FROM DUAL;    --引用時加單引號
     old   1: SELECT '&x' FROM DUAL
    new   1: SELECT 'the answer is 42' FROM DUAL
    'THEANSWERIS42'
    ----------------
    the answer is 42

2.--bind variables

    SQL> variable x varchar2(10)   --定義
    SQL> begin
      2  :x := 'hullo';
      3  end;
      4  /
    PL/SQL procedure successfully completed.
    SQL> print :x
    X
    --------------------------------
    hullo

       SQL> SELECT :x, '&x' FROM DUAL;
    old   1: SELECT :x, '&x' FROM DUAL
    new   1: SELECT :x, 'the answer is 42' FROM DUAL
    :X                               'THEANSWERIS42'
    -------------------------------- ----------------
    hullo                            the answer is 42
   --Saving output to a file
    
     spool report.txt
     spool off
--定義編輯器:
     SQL> DEFINE _EDITOR=/bin/vi
     SQL> edit
     SQL> EDIT abc.pkg
--SQL/PLUS主要命令:
    L 顯示緩衝區命令
    n 設置第n行為當前行
    del 刪除當前行
    C/old/new 
    n text:把text作為第n行的內容。
    I 在當前行之後插入一行,要想在第一行之前插入一個新行,使用0命令(即0 text)
$ORACLE_HOME/sqlplus/admin/glogin.sql腳本,設置全局sqlplus環境。
當前目錄下的login.sql配置當前環境。
--Error Handling in SQL*Plus
    SQL> WHENEVER SQLERROR EXIT SQL.SQLCODE
    SQL> WHENEVER SQLERROR EXIT SQL.SQLCODE ROLLBACK
--Creating a Stored Program

--to use the SQL*Plus SHOW ERRORS command, abbreviated as SHO ERR:

    SQL> SHO ERR
       Errors for FUNCTION WORDCOUNT:
       LINE/COL ERROR
    -------- ----------------------------------------------
    14/13 PLS-00201: identifier 'WORDS' must be declared
    14/13 PL/SQL: Statement ignored
    21/4 PL/SQL: Statement ignored
    21/11 PLS-00201: identifier 'WORDS' must be declared

    SQL> SHOW ERRORS category [schema.]object
    SQL> SHOW ERRORS FUNCTION wordcount
--Executing a Stored Program
    BEGIN
      --invoke wordcount() function
    DBMS_OUTPUT.PUT_LINE('There are ' || wordcount(CHR(9)) || ' words in a tab');     
    END;
    /
    
      SELECT isbn, wordcount(description) FROM books;
     
      VARIABLE words NUMBER
    
     CALL wordcount('some text') INTO :words;
    PRINT :words
--Showing Stored Programs

--to see a complete list of your programs (and tables, indexes, etc.), query the
USER_OBJECTS view,

    SELECT * FROM USER_OBJECTS;
    SQL> DESCRIBE wordcount
    FUNCTION wordcount RETURNS BINARY_INTEGER
--Managing Grants and Synonyms for Stored Programs
    GRANT EXECUTE ON wordcount TO scott;
    REVOKE EXECUTE ON wordcount FROM scott;
--To view a list of privileges you have granted to other users and roles, you can query the USER_TAB_PRIVS_MADE:
    SQL> SELECT table_name, grantee, privilege
     FROM USER_TAB_PRIVS_MADE
     WHERE table_name = 'WORDCOUNT';
--Hiding the Source Code of a Stored Program --p44

--Language Fundamentals

--struture
图片.png
Anonymous Blocks
Named Blocks
PROCEDURE [schema.]name [ ( parameter [, parameter ... ] ) ]
[AUTHID {DEFINER | CURRENT_USER}]

A function header has similar syntax, but includes the RETURN keyword:

FUNCTION [schema.]name [ ( parameter [, parameter ... ] ) ]
RETURN return_datatype
[AUTHID {DEFINER | CURRENT_USER}]
[DETERMINISTIC]
[PARALLEL ENABLE ...]
[PIPELINED [USING...] | AGGREGATE USING...]
Nested Blocks

图片.png

You can nest anonymous blocks within anonymous blocks to more than one level

--Initially the code is using the emp_name_ix index, but when I set NLS_COMP=LINGUISTIC and NLS_SORT=BINARY_CI to enable case insensitivity I stop using the 
index and start doing full table scans instead—oops! One solution is to create a functionbased,case-insensitive index, like this:
    
    CREATE INDEX last_name_ci ON EMPLOYEES (NLSSORT(last_name, 'NLS_SORT=BINARY_CI'))

    
BEGIN
    DBMS_OUTPUT.PUT_LINE('Session Timezone='||SESSIONTIMEZONE);
    DBMS_OUTPUT.PUT_LINE('Session Timestamp='||CURRENT_TIMESTAMP);
    DBMS_OUTPUT.PUT_LINE('DB Server Timestamp='||SYSTIMESTAMP);
    DBMS_OUTPUT.PUT_LINE('DB Timezone='||DBTIMEZONE);
    EXECUTE IMMEDIATE 'ALTER SESSION SET TIME_ZONE=DBTIMEZONE';
    DBMS_OUTPUT.PUT_LINE('DB Timestamp='||CURRENT_TIMESTAMP);
    -- Revert session time zone to local setting
    EXECUTE IMMEDIATE 'ALTER SESSION SET TIME_ZONE=LOCAL';
    END;
Scope --Page 58

when you run a procedure such as this (order_id is the primary key of the
orders table):

PROCEDURE remove_order (order_id IN NUMBER)
IS
BEGIN
DELETE orders WHERE order_id = order_id; -- Oops!
END;

This code will delete everything in the orders table regardless of the order_id that you
pass in. The reason: SQL’s name resolution matches first on column names rather than
on PL/SQL identifiers. The WHERE clause “order_id = order_id” is always true, so poof
goes your data. One way to fix it would be:

PROCEDURE remove_order (order_id IN NUMBER)
IS
BEGIN
DELETE orders WHERE order_id = remove_order.order_id;
END;

The PL/SQL Character Set

you would write two single quotes next to each other if you wanted the string to contain a single quote in that position. The following table offers some examples.

'There''s no business like show business.'     There's no business like show business.
'"Hound of the Baskervilles"'                  "Hound of the Baskervilles"
''''                                           '
'''hello'''                                    'hello'
''''''                                         ''

In an attempt to simplify this type of construct,Oracle Database 10g introduced user-defined delimiters. Start the literal with “q” to mark your delimiter, and surround your delimited expression with single quotes. The following table shows this feature in action.

q' ( There's no business like show business.) '    There's no business like show busi
ness.
q' { "Hound of the Baskervilles" } '               "Hound of the Baskervilles"
q' [ ' ] '                                         '
q' !'hello' ! '                                    'hello'
q' |'' | '                                         ''

Exception

DECLARE
no_such_sequence EXCEPTION;
PRAGMA EXCEPTION_INIT (no_such_sequence, −2289);
BEGIN
...
EXCEPTION
WHEN no_such_sequence
THEN
q$error_manager.raise_error ('Sequence not defined');
END;
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,393评论 5 467
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,790评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,391评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,703评论 1 270
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,613评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,003评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,507评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,158评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,300评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,256评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,274评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,984评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,569评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,662评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,899评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,268评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,840评论 2 339