SlideShare una empresa de Scribd logo
1 de 33
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

Más contenido relacionado

La actualidad más candente

Firestoreでマスタ取得を
効率化するいくつかの方法
Firestoreでマスタ取得を
効率化するいくつかの方法Firestoreでマスタ取得を
効率化するいくつかの方法
Firestoreでマスタ取得を
効率化するいくつかの方法Hirotaka Nishimiya
 
ユーザの LockoutTime 属性の値の確認
ユーザの LockoutTime 属性の値の確認ユーザの LockoutTime 属性の値の確認
ユーザの LockoutTime 属性の値の確認Michio Koyama
 
지리정보체계(GIS) - [2] 좌표계 이해하기
지리정보체계(GIS) - [2] 좌표계 이해하기지리정보체계(GIS) - [2] 좌표계 이해하기
지리정보체계(GIS) - [2] 좌표계 이해하기Byeong-Hyeok Yu
 
Querying the Wikidata Knowledge Graph
Querying the Wikidata Knowledge GraphQuerying the Wikidata Knowledge Graph
Querying the Wikidata Knowledge GraphIoan Toma
 
Apache Superset - open source data exploration and visualization (Conclusion ...
Apache Superset - open source data exploration and visualization (Conclusion ...Apache Superset - open source data exploration and visualization (Conclusion ...
Apache Superset - open source data exploration and visualization (Conclusion ...Lucas Jellema
 
FIWARE Global Summit - The Scorpio NGSI-LD Broker: Features and Supported Arc...
FIWARE Global Summit - The Scorpio NGSI-LD Broker: Features and Supported Arc...FIWARE Global Summit - The Scorpio NGSI-LD Broker: Features and Supported Arc...
FIWARE Global Summit - The Scorpio NGSI-LD Broker: Features and Supported Arc...FIWARE
 
Architecting Snowflake for High Concurrency and High Performance
Architecting Snowflake for High Concurrency and High PerformanceArchitecting Snowflake for High Concurrency and High Performance
Architecting Snowflake for High Concurrency and High PerformanceSamanthaBerlant
 
EC2でマルチキャスト
EC2でマルチキャストEC2でマルチキャスト
EC2でマルチキャストKenta Yasukawa
 
NOSQLの基礎知識(講義資料)
NOSQLの基礎知識(講義資料)NOSQLの基礎知識(講義資料)
NOSQLの基礎知識(講義資料)CLOUDIAN KK
 
OpenLineage による Airflow のデータ来歴の収集と可視化(Airflow Meetup Tokyo #3 発表資料)
OpenLineage による Airflow のデータ来歴の収集と可視化(Airflow Meetup Tokyo #3 発表資料)OpenLineage による Airflow のデータ来歴の収集と可視化(Airflow Meetup Tokyo #3 発表資料)
OpenLineage による Airflow のデータ来歴の収集と可視化(Airflow Meetup Tokyo #3 発表資料)NTT DATA Technology & Innovation
 
개발자들이 흔히 실수하는 SQL 7가지
개발자들이 흔히 실수하는 SQL 7가지개발자들이 흔히 실수하는 SQL 7가지
개발자들이 흔히 실수하는 SQL 7가지JungGeun Lee
 
DB2の使い方 管理ツール編
DB2の使い方 管理ツール編DB2の使い方 管理ツール編
DB2の使い方 管理ツール編Akira Shimosako
 
PostgreSQL 15の新機能を徹底解説
PostgreSQL 15の新機能を徹底解説PostgreSQL 15の新機能を徹底解説
PostgreSQL 15の新機能を徹底解説Masahiko Sawada
 
PostgreSQLの実行計画を読み解こう(OSC2015 Spring/Tokyo)
PostgreSQLの実行計画を読み解こう(OSC2015 Spring/Tokyo)PostgreSQLの実行計画を読み解こう(OSC2015 Spring/Tokyo)
PostgreSQLの実行計画を読み解こう(OSC2015 Spring/Tokyo)Satoshi Yamada
 
OpenStackによる、実践オンプレミスクラウド
OpenStackによる、実践オンプレミスクラウドOpenStackによる、実践オンプレミスクラウド
OpenStackによる、実践オンプレミスクラウドMasanori Itoh
 
Keycloak & midPoint の紹介
Keycloak & midPoint の紹介Keycloak & midPoint の紹介
Keycloak & midPoint の紹介Hiroyuki Wada
 

La actualidad más candente (20)

Firestoreでマスタ取得を
効率化するいくつかの方法
Firestoreでマスタ取得を
効率化するいくつかの方法Firestoreでマスタ取得を
効率化するいくつかの方法
Firestoreでマスタ取得を
効率化するいくつかの方法
 
ユーザの LockoutTime 属性の値の確認
ユーザの LockoutTime 属性の値の確認ユーザの LockoutTime 属性の値の確認
ユーザの LockoutTime 属性の値の確認
 
지리정보체계(GIS) - [2] 좌표계 이해하기
지리정보체계(GIS) - [2] 좌표계 이해하기지리정보체계(GIS) - [2] 좌표계 이해하기
지리정보체계(GIS) - [2] 좌표계 이해하기
 
Querying the Wikidata Knowledge Graph
Querying the Wikidata Knowledge GraphQuerying the Wikidata Knowledge Graph
Querying the Wikidata Knowledge Graph
 
Apache Superset - open source data exploration and visualization (Conclusion ...
Apache Superset - open source data exploration and visualization (Conclusion ...Apache Superset - open source data exploration and visualization (Conclusion ...
Apache Superset - open source data exploration and visualization (Conclusion ...
 
How good was he?
How good was he?How good was he?
How good was he?
 
FIWARE Global Summit - The Scorpio NGSI-LD Broker: Features and Supported Arc...
FIWARE Global Summit - The Scorpio NGSI-LD Broker: Features and Supported Arc...FIWARE Global Summit - The Scorpio NGSI-LD Broker: Features and Supported Arc...
FIWARE Global Summit - The Scorpio NGSI-LD Broker: Features and Supported Arc...
 
Architecting Snowflake for High Concurrency and High Performance
Architecting Snowflake for High Concurrency and High PerformanceArchitecting Snowflake for High Concurrency and High Performance
Architecting Snowflake for High Concurrency and High Performance
 
Introduction to OData
Introduction to ODataIntroduction to OData
Introduction to OData
 
EC2でマルチキャスト
EC2でマルチキャストEC2でマルチキャスト
EC2でマルチキャスト
 
PostgreSQLレプリケーション徹底紹介
PostgreSQLレプリケーション徹底紹介PostgreSQLレプリケーション徹底紹介
PostgreSQLレプリケーション徹底紹介
 
NOSQLの基礎知識(講義資料)
NOSQLの基礎知識(講義資料)NOSQLの基礎知識(講義資料)
NOSQLの基礎知識(講義資料)
 
OpenLineage による Airflow のデータ来歴の収集と可視化(Airflow Meetup Tokyo #3 発表資料)
OpenLineage による Airflow のデータ来歴の収集と可視化(Airflow Meetup Tokyo #3 発表資料)OpenLineage による Airflow のデータ来歴の収集と可視化(Airflow Meetup Tokyo #3 発表資料)
OpenLineage による Airflow のデータ来歴の収集と可視化(Airflow Meetup Tokyo #3 発表資料)
 
개발자들이 흔히 실수하는 SQL 7가지
개발자들이 흔히 실수하는 SQL 7가지개발자들이 흔히 실수하는 SQL 7가지
개발자들이 흔히 실수하는 SQL 7가지
 
Hadoop fault-tolerance
Hadoop fault-toleranceHadoop fault-tolerance
Hadoop fault-tolerance
 
DB2の使い方 管理ツール編
DB2の使い方 管理ツール編DB2の使い方 管理ツール編
DB2の使い方 管理ツール編
 
PostgreSQL 15の新機能を徹底解説
PostgreSQL 15の新機能を徹底解説PostgreSQL 15の新機能を徹底解説
PostgreSQL 15の新機能を徹底解説
 
PostgreSQLの実行計画を読み解こう(OSC2015 Spring/Tokyo)
PostgreSQLの実行計画を読み解こう(OSC2015 Spring/Tokyo)PostgreSQLの実行計画を読み解こう(OSC2015 Spring/Tokyo)
PostgreSQLの実行計画を読み解こう(OSC2015 Spring/Tokyo)
 
OpenStackによる、実践オンプレミスクラウド
OpenStackによる、実践オンプレミスクラウドOpenStackによる、実践オンプレミスクラウド
OpenStackによる、実践オンプレミスクラウド
 
Keycloak & midPoint の紹介
Keycloak & midPoint の紹介Keycloak & midPoint の紹介
Keycloak & midPoint の紹介
 

Similar a Plsql coding conventions

SQL Tuning Overview
SQL Tuning OverviewSQL Tuning Overview
SQL Tuning OverviewKai 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 09Thuan 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/SQLGary Myers
 
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 lsInSync 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 03Thuan Nguyen
 
Oracle tips and tricks
Oracle tips and tricksOracle tips and tricks
Oracle tips and tricksYanli Liu
 
Open Gurukul Language PL/SQL
Open Gurukul Language PL/SQLOpen Gurukul Language PL/SQL
Open Gurukul Language PL/SQLOpen Gurukul
 
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 16Thuan Nguyen
 

Similar a 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
 
PLSQL Cursors
PLSQL CursorsPLSQL Cursors
PLSQL Cursors
 
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
 

Último

[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 

Último (20)

[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 

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.