SlideShare a Scribd company logo
PL/SQL Coding Conventions fang.yu@moodys.com Dec 28,  2010
Agenda Naming Conventions Coding Style, Format and Comments PL/SQL Best Practices
Naming Conventions ,[object Object]
Some Facts that Matter
An Appetizer
General Guidelines
Naming Conventions,[object Object]
Higher quality, more robust
Better products
Higher reputation,[object Object]
Case insensitive,[object Object]
1.4 General Guidelines  ,[object Object]
Do not use reserved words as variable names.SELECT  keyword FROM v$reserved_words   ,[object Object],CREATE TABLE demo_table CREATE TABLE demo ,[object Object],SQL> create table "HelloWorld" (id number); Table created. SQL> select * from helloworld; select * from helloworld               * ERROR at line 1: ORA-00942: table or view does not exist SQL> select * from HelloWorld; select * from HelloWorld               * ERROR at line 1: ORA-00942: table or view does not exist SQL> select * from "HelloWorld"; no rows selected
1.5 Naming Conventions for Variables
1.6 Naming Conventions for Cursors
1.7 Naming Conventions for Records and Collections
1.8 Naming Conventions for Database Objects
Coding Style, Format and Comments ,[object Object]
Some Examples
Tools that format PL/SQL
Code Commenting
View/Trigger Comment Block,[object Object]
Indention using tab or 3 spaces. Keep it consistent!
One command per line.
Specify columns explicitly in SELECT and INSERT statements
SQL keywords are right-aligned / left-aligned within a SQL command. Keep it consistent!PROCEDURE set_salary(p_empno_in IN emp.empno%TYPE) IS    CURSOR l_emp_cur(p_empnoemp.empno%TYPE)    IS               SELECT ename                             ,sal                  FROM emp               WHERE empno = p_empno         ORDER BY ename; l_empl_emp_cur%ROWTYPE; l_new_salemp.sal%TYPE; BEGIN    OPEN l_emp_cur(p_empno_in);    FETCH l_emp_cur INTO  l_emp;    CLOSE l_emp_cur;    --  get_new_salary (p_empno_in   => in_empno                                , p_sal_out       => l_new_sal);    -- Check whether salary has changed     IF l_emp.sal <> l_new_sal THEN       UPDATE emp               SET sal = l_new_sal         WHERE empno = p_empno_in;    END IF; END set_salary;
2.2 Some SQL Style Examples SELECT e.job, e.deptno FROM emp e WHERE e.name = 'SCOTT' AND e.sal > 100000 SELECT   e.job, e.deptno FROM      emp e WHERE        e.name = 'SCOTT'  AND     e.sal > 100000 SELECT   e.JOB ,e.deptno     FROM  emp e  WHERE  e.name = 'SCOTT'         AND  e.sal > 100000 SELECT  e.JOB ,  e.deptno FROM      emp e WHERE   e.name = 'SCOTT' AND         e.sal > 100000 SELECT  e.JOB, e.deptno FROM      emp e WHERE   e.name = 'SCOTT'  AND         e.sal > 100000
2.3 Tools that format PL/SQL ,[object Object]
Oracle SQL Developer,[object Object]
2.5 View/Trigger Comment Block CREATE OR REPLACE TRIGGER tri_mytab_bi BEFORE INSERT ON mytab FOR EACH ROW  DECLARE    -- local variables here  BEGIN       NULL; END tri_mytab_bi /*******************************************************************                    Copyright YYYY by <Company Name>                                All Rights Reserved.  <Short synopsis of trigger's purpose. Required.>  <Optional design notes.>  *******************************************************************/ ; /*******************************************************************                    Copyright YYYY by <Company Name>                                All Rights Reserved.  <Short synopsis of trigger's purpose. Required.>  <Optional design notes.>  *******************************************************************/  CREATE OR REPLACE TRIGGER tri_mytab_bi BEFORE INSERT ON mytab FOR EACH ROW  DECLARE    -- local variables here  BEGIN      NULL; END tri_mytab_bi;
PL/SQL Best Practices ,[object Object]
Data Types
Flow Control

More Related Content

What's hot

Sql loader good example
Sql loader good exampleSql loader good example
Sql loader good example
Aneel Swarna MBA ,PMP
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorJussi Pohjolainen
 
Les05 (Displaying Data from Multiple Table)
Les05 (Displaying Data from Multiple Table)Les05 (Displaying Data from Multiple Table)
Les05 (Displaying Data from Multiple Table)
Achmad Solichin
 
Operator Overloading & Function Overloading
Operator Overloading & Function OverloadingOperator Overloading & Function Overloading
Operator Overloading & Function Overloading
Meghaj Mallick
 
Hrms for beginners
Hrms for beginnersHrms for beginners
Hrms for beginners
sravan46
 
Oracle HCM Cloud Core HR Workforce Structures Table Relationships
Oracle HCM Cloud Core HR Workforce Structures Table RelationshipsOracle HCM Cloud Core HR Workforce Structures Table Relationships
Oracle HCM Cloud Core HR Workforce Structures Table Relationships
Sricharan
 
Top 40 sql queries for testers
Top 40 sql queries for testersTop 40 sql queries for testers
Top 40 sql queries for testers
tlvd
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Pranali Chaudhari
 
Stored-Procedures-Presentation
Stored-Procedures-PresentationStored-Procedures-Presentation
Stored-Procedures-PresentationChuck Walker
 
PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts
Bharat Kalia
 
C Pointers
C PointersC Pointers
C Pointers
omukhtar
 
SQL Tutorial for Beginners
SQL Tutorial for BeginnersSQL Tutorial for Beginners
SQL Tutorial for Beginners
Abdelhay Shafi
 
Resumen sql-oracle
Resumen sql-oracleResumen sql-oracle
Resumen sql-oracle
Heriberto Espino Martinez
 
SQL-RDBMS Queries and Question Bank
SQL-RDBMS Queries and Question BankSQL-RDBMS Queries and Question Bank
SQL-RDBMS Queries and Question Bank
Md Mudassir
 
Setup Oracle EBS R12 Performance Management
Setup Oracle EBS R12 Performance ManagementSetup Oracle EBS R12 Performance Management
Setup Oracle EBS R12 Performance Management
Feras Ahmad
 
Generic programming and concepts that should be in C++
Generic programming and concepts that should be in C++Generic programming and concepts that should be in C++
Generic programming and concepts that should be in C++
Anton Kolotaev
 
java programming- control statements
 java programming- control statements java programming- control statements
java programming- control statements
jyoti_lakhani
 
Best sql plsql material
Best sql plsql materialBest sql plsql material
Best sql plsql material
pitchaiah yechuri
 

What's hot (20)

Sql loader good example
Sql loader good exampleSql loader good example
Sql loader good example
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
 
Les05 (Displaying Data from Multiple Table)
Les05 (Displaying Data from Multiple Table)Les05 (Displaying Data from Multiple Table)
Les05 (Displaying Data from Multiple Table)
 
Operator Overloading & Function Overloading
Operator Overloading & Function OverloadingOperator Overloading & Function Overloading
Operator Overloading & Function Overloading
 
Hrms for beginners
Hrms for beginnersHrms for beginners
Hrms for beginners
 
Oracle HCM Cloud Core HR Workforce Structures Table Relationships
Oracle HCM Cloud Core HR Workforce Structures Table RelationshipsOracle HCM Cloud Core HR Workforce Structures Table Relationships
Oracle HCM Cloud Core HR Workforce Structures Table Relationships
 
Ref cursor
Ref cursorRef cursor
Ref cursor
 
Top 40 sql queries for testers
Top 40 sql queries for testersTop 40 sql queries for testers
Top 40 sql queries for testers
 
Les01
Les01Les01
Les01
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Stored-Procedures-Presentation
Stored-Procedures-PresentationStored-Procedures-Presentation
Stored-Procedures-Presentation
 
PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts
 
C Pointers
C PointersC Pointers
C Pointers
 
SQL Tutorial for Beginners
SQL Tutorial for BeginnersSQL Tutorial for Beginners
SQL Tutorial for Beginners
 
Resumen sql-oracle
Resumen sql-oracleResumen sql-oracle
Resumen sql-oracle
 
SQL-RDBMS Queries and Question Bank
SQL-RDBMS Queries and Question BankSQL-RDBMS Queries and Question Bank
SQL-RDBMS Queries and Question Bank
 
Setup Oracle EBS R12 Performance Management
Setup Oracle EBS R12 Performance ManagementSetup Oracle EBS R12 Performance Management
Setup Oracle EBS R12 Performance Management
 
Generic programming and concepts that should be in C++
Generic programming and concepts that should be in C++Generic programming and concepts that should be in C++
Generic programming and concepts that should be in C++
 
java programming- control statements
 java programming- control statements java programming- control statements
java programming- control statements
 
Best sql plsql material
Best sql plsql materialBest sql plsql material
Best sql plsql material
 

Similar to Plsql coding conventions

SQL Tuning Overview
SQL Tuning OverviewSQL Tuning Overview
SQL Tuning Overview
Kai Liu
 
Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09
Thuan Nguyen
 
e computer notes - Producing readable output with i sql plus
e computer notes - Producing readable output with i sql pluse computer notes - Producing readable output with i sql plus
e computer notes - Producing readable output with i sql plusecomputernotes
 
Sql Objects And PL/SQL
Sql Objects And PL/SQLSql Objects And PL/SQL
Sql Objects And PL/SQL
Gary Myers
 
Oracle sql tuning
Oracle sql tuningOracle sql tuning
Oracle sql tuning
bishnupriya Panda
 
Mysqlppt
MysqlpptMysqlppt
MysqlpptReka
 
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
Tony jambu   (obscure) tools of the trade for tuning oracle sq lsTony jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
InSync Conference
 
Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03
Thuan Nguyen
 
Oracle tips and tricks
Oracle tips and tricksOracle tips and tricks
Oracle tips and tricksYanli Liu
 
PLSQL
PLSQLPLSQL
Open Gurukul Language PL/SQL
Open Gurukul Language PL/SQLOpen Gurukul Language PL/SQL
Open Gurukul Language PL/SQL
Open Gurukul
 
Jsp And Jdbc
Jsp And JdbcJsp And Jdbc
Jsp And Jdbc
Roy Antony Arnold G
 
Presentation log4 j
Presentation log4 jPresentation log4 j
Presentation log4 j
Sylvain Bouchard
 
Presentation log4 j
Presentation log4 jPresentation log4 j
Presentation log4 j
Sylvain Bouchard
 
Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16
Thuan Nguyen
 
PL-SQL.pdf
PL-SQL.pdfPL-SQL.pdf
PL-SQL.pdf
Anas Nakash
 

Similar to Plsql coding conventions (20)

SQL Tuning Overview
SQL Tuning OverviewSQL Tuning Overview
SQL Tuning Overview
 
Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09
 
e computer notes - Producing readable output with i sql plus
e computer notes - Producing readable output with i sql pluse computer notes - Producing readable output with i sql plus
e computer notes - Producing readable output with i sql plus
 
Plsql
PlsqlPlsql
Plsql
 
Module04
Module04Module04
Module04
 
Sql Objects And PL/SQL
Sql Objects And PL/SQLSql Objects And PL/SQL
Sql Objects And PL/SQL
 
Oracle sql tuning
Oracle sql tuningOracle sql tuning
Oracle sql tuning
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Introduction to mysql part 3
Introduction to mysql part 3Introduction to mysql part 3
Introduction to mysql part 3
 
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
Tony jambu   (obscure) tools of the trade for tuning oracle sq lsTony jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
 
Oracle
OracleOracle
Oracle
 
Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03
 
Oracle tips and tricks
Oracle tips and tricksOracle tips and tricks
Oracle tips and tricks
 
PLSQL
PLSQLPLSQL
PLSQL
 
Open Gurukul Language PL/SQL
Open Gurukul Language PL/SQLOpen Gurukul Language PL/SQL
Open Gurukul Language PL/SQL
 
Jsp And Jdbc
Jsp And JdbcJsp And Jdbc
Jsp And Jdbc
 
Presentation log4 j
Presentation log4 jPresentation log4 j
Presentation log4 j
 
Presentation log4 j
Presentation log4 jPresentation log4 j
Presentation log4 j
 
Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16
 
PL-SQL.pdf
PL-SQL.pdfPL-SQL.pdf
PL-SQL.pdf
 

Recently uploaded

Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
Rohit Gautam
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Vladimir Iglovikov, Ph.D.
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
Alex Pruden
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
ThomasParaiso2
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 

Recently uploaded (20)

Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 

Plsql coding conventions

  • 1. PL/SQL Coding Conventions fang.yu@moodys.com Dec 28, 2010
  • 2. Agenda Naming Conventions Coding Style, Format and Comments PL/SQL Best Practices
  • 3.
  • 7.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14. 1.5 Naming Conventions for Variables
  • 15. 1.6 Naming Conventions for Cursors
  • 16. 1.7 Naming Conventions for Records and Collections
  • 17. 1.8 Naming Conventions for Database Objects
  • 18.
  • 22.
  • 23. Indention using tab or 3 spaces. Keep it consistent!
  • 25. Specify columns explicitly in SELECT and INSERT statements
  • 26. SQL keywords are right-aligned / left-aligned within a SQL command. Keep it consistent!PROCEDURE set_salary(p_empno_in IN emp.empno%TYPE) IS CURSOR l_emp_cur(p_empnoemp.empno%TYPE) IS SELECT ename ,sal FROM emp WHERE empno = p_empno ORDER BY ename; l_empl_emp_cur%ROWTYPE; l_new_salemp.sal%TYPE; BEGIN OPEN l_emp_cur(p_empno_in); FETCH l_emp_cur INTO l_emp; CLOSE l_emp_cur; -- get_new_salary (p_empno_in => in_empno , p_sal_out => l_new_sal); -- Check whether salary has changed IF l_emp.sal <> l_new_sal THEN UPDATE emp SET sal = l_new_sal WHERE empno = p_empno_in; END IF; END set_salary;
  • 27. 2.2 Some SQL Style Examples SELECT e.job, e.deptno FROM emp e WHERE e.name = 'SCOTT' AND e.sal > 100000 SELECT e.job, e.deptno FROM emp e WHERE e.name = 'SCOTT' AND e.sal > 100000 SELECT e.JOB ,e.deptno FROM emp e WHERE e.name = 'SCOTT' AND e.sal > 100000 SELECT e.JOB , e.deptno FROM emp e WHERE e.name = 'SCOTT' AND e.sal > 100000 SELECT e.JOB, e.deptno FROM emp e WHERE e.name = 'SCOTT' AND e.sal > 100000
  • 28.
  • 29.
  • 30. 2.5 View/Trigger Comment Block CREATE OR REPLACE TRIGGER tri_mytab_bi BEFORE INSERT ON mytab FOR EACH ROW DECLARE -- local variables here BEGIN NULL; END tri_mytab_bi /******************************************************************* Copyright YYYY by <Company Name> All Rights Reserved. <Short synopsis of trigger's purpose. Required.> <Optional design notes.> *******************************************************************/ ; /******************************************************************* Copyright YYYY by <Company Name> All Rights Reserved. <Short synopsis of trigger's purpose. Required.> <Optional design notes.> *******************************************************************/ CREATE OR REPLACE TRIGGER tri_mytab_bi BEFORE INSERT ON mytab FOR EACH ROW DECLARE -- local variables here BEGIN NULL; END tri_mytab_bi;
  • 31.
  • 37.
  • 38. Use a function to return a constant if the “constant” value may change at times
  • 39. Do not use public global variables, use getter/setter or parameters-passing
  • 40. Never put all of a system’s constants in a single package.
  • 41.
  • 42.
  • 43. Use SUBTYPE to avoid hard-coded variable length declarations.
  • 44. Avoid using CHAR data type.
  • 45. Never use zero-length strings to substitute NULL.
  • 46. (CLOB, BLOB or BFILE ) vs. (LONG or LONG RAW)
  • 47. NUMBER vs. PLS_INTEGER vs. SIMPLE_INTEGER (New in 11g, always not null)
  • 48.
  • 49.
  • 50. Always use a FOR loop to process the complete cursor results unless you are using bulk operation
  • 51. Always use a WHILE loop to process a loose array
  • 52. Always use FIRST..LAST when iterating through collections with a loop
  • 53. Always use EXIT WHEN instead of an IF statement to exit from a loop
  • 54. Never EXIT from within any FOR loop, use LOOP or WHILE loop instead
  • 55. Avoid hard-coded upper or lower bound values with FOR loops
  • 56.
  • 57.
  • 58. Never assign predefined exception names to user defined exceptions
  • 59. Avoid use of WHEN OTHERS clause in an exception section without any other specific handlers
  • 60. Avoid use of EXCEPTION_INIT pragma for -20, NNN error (-20,999 ~ -20,000)
  • 61. Use DBMS_UTILITY.format_error_stack instead of SQLERRM to obtain the full error message
  • 62.
  • 63.
  • 64. Use bind variables. Do not concatenate strings unless needed to identify schema or table/view.
  • 65.
  • 66.
  • 67. Always use forward declaration for private functions and procedures in packages.
  • 68. Avoid standalone procedures or functions – put them in packages
  • 69. Avoid using RETURN statements in a procedure(one way in one way out)
  • 70. Try to use no more than one RETURN statement within a function
  • 71.
  • 72. Try to use CASE rather than DECODE.
  • 73. Always use COALESCE instead of NVL, if parameter 2 of the NVL function is a function call or a SELECT statement.
  • 74. Always use CASE instead of NVL2 if parameter 2 or 3 of NVL2 is either a function call or a SELECT statement.
  • 75. The NVL function always evaluates both parameters before deciding which one to use. This can be harmful if parameter 2 is either a function call or a select statement, as it will be executed regardless of whether parameter 1 contains a NULL value or not.
  • 76. The COALESCE function does not have this drawback.
  • 77. NVL2 is similar.
  • 78.
  • 80. © 2009 Moody’s Analytics, Inc. and/or its licensors and affiliates (collectively, “MOODY’S”). All rights reserved. ALL INFORMATION CONTAINED HEREIN IS PROTECTED BY COPYRIGHT LAW AND NONE OF SUCH INFORMATION MAY BE COPIED OR OTHERWISE REPRODUCED, REPACKAGED, FURTHER TRANSMITTED, TRANSFERRED, DISSEMINATED, REDISTRIBUTED OR RESOLD, OR STORED FOR SUBSEQUENT USE FOR ANY SUCH PURPOSE, IN WHOLE OR IN PART, IN ANY FORM OR MANNER OR BY ANY MEANS WHATSOEVER, BY ANY PERSON WITHOUT MOODY’S PRIOR WRITTEN CONSENT. All information contained herein is obtained by MOODY’S from sources believed by it to be accurate and reliable. Because of the possibility of human or mechanical error as well as other factors, however, all information contained herein is provided “AS IS” without warranty of any kind. Under no circumstances shall MOODY’S have any liability to any person or entity for (a) any loss or damage in whole or in part caused by, resulting from, or relating to, any error (negligent or otherwise) or other circumstance or contingency within or outside the control of MOODY’S or any of its directors, officers, employees or agents in connection with the procurement, collection, compilation, analysis, interpretation, communication, publication or delivery of any such information, or (b) any direct, indirect, special, consequential, compensatory or incidental damages whatsoever (including without limitation, lost profits), even if MOODY’S is advised in advance of the possibility of such damages, resulting from the use of or inability to use, any such information. The ratings, financial reporting analysis, projections, and other observations, if any, constituting part of the information contained herein are, and must be construed solely as, statements of opinion and not statements of fact or recommendations to purchase, sell or hold any securities. NO WARRANTY, EXPRESS OR IMPLIED, AS TO THE ACCURACY, TIMELINESS, COMPLETENESS, MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OF ANY SUCH RATING OR OTHER OPINION OR INFORMATION IS GIVEN OR MADE BY MOODY’S IN ANY FORM OR MANNER WHATSOEVER. Each rating or other opinion must be weighed solely as one factor in any investment decision made by or on behalf of any user of the information contained herein, and each such user must accordingly make its own study and evaluation of each security and of each issuer and guarantor of, and each provider of credit support for, each security that it may consider purchasing, holding, or selling.