PL/SQL 格式化指南 (PL/SQL Formatting Guide)

 

http://bianxq.iteye.com/blog/537478

最近一直在修改以前同事写的Oracle存储过程,由于编码不规范,修改起来相当费神,于是乎,翻译了这篇 PL/SQL Formatting Guide (Oracle PL/ SQL by Example,Appendix A),费了九牛二虎之力,终于翻译完了,也终于知道自己的英语有多烂了,呜呜。

声明:原创翻译,不足之处请多多指正,转载请注明出处!

一、大小写

就像在SQL中一样,PL / SQL中是不区分大小写的。其一般准则如下:

关键字(BEGIN, EXCEPTION, END, IF THEN ELSE,LOOP, END LOOP)、数据类型(VARCHAR2, NUMBER)、内部函数(LEAST, SUBSTR)和用户定义的子程序(procedures, functions,packages),使用大写。
变量名以及SQL中的列名和表名,使用小写。
二、空白

空白(空行和空格)在PL/SQL中如同在SQL中一样重要,因为它是提高代码可读性的一个重要因素。换句话说,可以通过在代码中使用缩进来体现程序的逻辑结构。以下是一些建议:

在等号或比较操作符的左右各留一个空格;
结构词(DECLARE, BEGIN, EXCEPTION, END,IF 和 END IF, LOOP 和 END LOOP)居左排列。另外,结构中的嵌套结构要缩进三个空格(使用空格键,而不是Tab键);
主要代码段之间用空行隔开;
把同一结构的不同逻辑部分分开写在独立的行,即使这个结构很短。例如,IF和THEN被放在同一行,而ELSE 和END IF则放在独立的行。
三、命名约定

使用以下前缀对于避免与关键字和表名列名相冲突是很有帮助的:

v_变量名
con_常量名
i_输入参数名,o_输出参数名,io_输入输出参数名
c_游标名 或者 游标名_cur
rc_ Ref Cursor名
r_Record名 或者 Record名_rec
FOR r_stud IN c_stud LOOP…
FOR stud_rec IN stud_cur LOOP
type_名称,名称_type (用户定义的类型)
t_表名,表名_tab (PL/SQL 表)
rec_Record名,Record名_rec (Record变量)
e_异常名 (用户定义的异常)
包的名称应该描述包内的存储过程和函数主要所完成的功能

存储过程的名称应该描述该存储过程所执行的动作

函数的名称应该描述所返回的变量

例如:

 

PACKAGE student_admin
-- admin 后缀可能是用于表示管理功能.
PROCEDURE remove_student (i_student_id IN student.studid%TYPE);
 
FUNCTION student_enroll_count (i_student_id student.studid%TYPE)
RETURN INTEGER;四、注释

PL/SQL中的注释如同SQL中的注释一样重要。他们应该解释程序的主要部分和所有关键的逻辑步骤。

使用单行注释(–)而不是多行注释(/*)。即使PL/SQL对这些注释做同样处理,这样在代码完成后进行调试也会容易些,因为你不能在多行注释中嵌入多行注释。换句话说,单行注释代码中可以部分取消注释,而在多行注释代码中则不行。

五、其他的建议

对于PL/SQL中嵌入的SQL声明,使用相同的格式化指南来决定这些声明应该如何在代码块中出现

提供一个头部注释,用于说明代码块的用途并列出创建日期和作者名字。并且每个修订版都要有一行注释,包含作者名、日期和修订版描述。

例如:下面的这个示例体现了上述建议。请注意该示例还使用了等宽字体(Courier New),因为每个字体占据同等宽度可以使格式化更加简便。等比例空格字体会隐藏空格使得行间对齐比较困难。多数文本和程序编辑器默认使用等宽字体。

REM ********************************************************
REM * 文件名:coursediscount01.SQL
REM * 版本:1
REM * 用途:对于至少有一部分超过十个学生登记的课程给予折扣
REM * 参数:无
REM *
REM * 作者:s.tashi  时间:2000.1.1
REM * 修改者:y.sonam 时间:2000.2.1
REM * 描述:修正游标,添加缩进和注释。
REM ********************************************************
DECLARE
   -- C_DISCOUNT_COURSE 找出那些至少有一部分超过十个学生登记的课程
   CURSOR c_discount_course IS
      SELECT DISTINCT course_no
      FROM section sect
      WHERE 10 <= (SELECT COUNT(*)
                    FROM enrollment enr
                   WHERE enr.section_id = sect.section_id
                   );
   -- 费用超过 $2000.00的课程的折扣率
   con_discount_2000 CONSTANT NUMBER := .90;
   -- 费用在$1001.00和$2000.00之间的课程的折扣率
   con_discount_other CONSTANT NUMBER := .95;
 
   v_current_course_cost course.cost%TYPE;
   v_discount_all NUMBER;
   e_update_is_problematic EXCEPTION;
BEGIN
   -- 对于那些要打折的课程, 确定当前费用和新的费用
   FOR r_discount_course IN c_discount_course LOOP
       SELECT cost
       INTO v_current_course_cost
       FROM course
       WHERE course_no = r_discount_course.course_no;
 
       IF v_current_course_cost > 2000 THEN
          v_discount_all := con_discount_2000;
       ELSE
          IF v_current_course_cost > 1000 THEN
             v_discount_all := con_discount_other;
          ELSE
             v_discount_all := 1;
          END IF;
       END IF;
 
       BEGIN
          UPDATE course
          SET cost = cost * v_discount_all
          WHERE course_no = r_discount_course.course_no;
       EXCEPTION
          WHEN OTHERS THEN
             RAISE e_update_is_problematic;
       END; -- 更新记录的子代码块结束
 
   END LOOP;-- 主循环结束
 
   COMMIT;
 
EXCEPTION
   WHEN e_update_is_problematic THEN
   -- 事务回滚
   ROLLBACK;
   DBMS_OUTPUT.PUT_LINE
      ('There was a problem updating a course cost.');
   WHEN OTHERS THEN
      NULL;
END;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
作者:Steven Feuerstein, Bill Pribyl 出版日期:October 1, 2009 出版社:O'Reilly 页数:1226 ISBN:ISBN-10: 0596514468 ISBN-13: 978-0596514464 文件格式:PDF 文件大小:15.06 MB Review If you’re doing database application development in the Oracle environment, you’re going to have to know PL/SQL, the company’s extended query and update language. If you want your programs to exploit the special capabilities of Oracle software, you’ll need to know the language well. That’s where the third edition of Oracle PL/SQL Programming comes into play. It’s an absolutely comprehensive reference (as well as a rather extensive tutorial) on PL/SQL, ideally suited to answering your questions about how to perform some programming tasks and reminding you of the characteristics of functions, triggers, and other elements of the database programmer’s toolkit. The new edition covers calls to Java methods from within PL/SQL programs, autonomous transactions, object type inheritance, and the new Timestamp and XMLType data types. There’s also more information about server internals–the way PL/SQL programs are run–than before, better enabling readers to optimize their code for fast and safe execution. Steven Feuerstein takes care to explain, with prose and example code, the characteristics of PL/SQL elements. In explaining number conversions, for example, he explores Oracle’s different ways of formatting numbers, then details the behavior of the to_number function under different conditions (with and without a specified format model, and with National Language Support information attached). It’s a helpful approach that will have readers using the index to locate places in which Feuerstein mentions language elements of interest. –David Wall Topics covered: How to use Oracle PL/SQL in all its manifestations through Oracle9i. Fundamentals of program structure (loops, cases, exceptions, etc.) and execution get attention, as do data types, transaction management, triggers, and the object-oriented aspects of the language. There’s also coverage of calls to external Java and C programs. –This text refers to the Paperback edition. Product Description This book is the definitive reference on PL/SQL, considered throughout the database community to be the best Oracle programming book available. Like its predecessors, this fifth edition of Oracle PL/SQL Programming covers language fundamentals, advanced coding techniques, and best practices for using Oracle’s powerful procedural language. Thoroughly updated for Oracle Database 11g Release 2, this edition reveals new PL/SQL features and provides extensive code samples, ranging from simple examples to complex and complete applications, in the book and on the companion website. This indispensable reference for both novices and experienced Oracle programmers will help you: Get PL/SQL programs up and running quickly, with clear instructions for executing, tracing, testing, debugging, and managing PL/SQL code Optimize PL/SQL performance with the aid of a brand-new chapter in the fifth edition Explore datatypes, conditional and sequential control statements, loops, exception handling, security features, globalization and localization issues, and the PL/SQL architecture Understand and use new Oracle Database 11g features, including the edition-based redefinition capability, the function result cache, the new CONTINUE statement, fine-grained dependency tracking, sequences in PL/SQL expressions, supertype invocation from subtypes, and enhancements to native compilation, triggers, and dynamic SQL Use new Oracle Database 11g tools and techniques such as PL/Scope, the PL/SQL hierarchical profiler, and the SecureFiles technology for large objects Build modular PL/SQL applications using procedures, functions, triggers, and packages

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值