SlideShare a Scribd company logo
1 of 29
PL/SQL & SQL
CODING GUIDELINES – PART 8
Larry Nung
AGENDA
Dynamic SQL
Stored Objects
Reference
Q&A
2
DYNAMIC SQL
58. ALWAYS USE A STRING VARIABLE TO
EXECUTE DYNAMIC SQL.
BAD
-- Bad
DECLARE
l_empno emp.empno%TYPE := 4711;
BEGIN
EXECUTE IMMEDIATE 'DELETE FROM emp
WHERE epno = :p_empno' USING l_empno;
END;
GOOD
-- Good
DECLARE
l_empno emp.empno%TYPE := 4711;
l_sql VARCHAR2(32767);
BEGIN
l_sql := 'DELETE FROM emp WHERE epno = :p_empno';
EXECUTE IMMEDIATE l_sql USING l_empno;
EXCEPTION
WHEN others
THEN
DBMS_OUTPUT.PUT_LINE(l_sql);
END;
59. TRY TO USE OUTPUT BIND ARGUMENTS IN THE
RETURNING INTO CLAUSE OF DYNAMIC
INSERT, UPDATE, OR DELETE STATEMENTS
RATHER THAN THE USING CLAUSE.
BAD
CREATE OR REPLACE PACKAGE BODY employee_api IS
PROCEDURE upd_salary (in_employee_id IN
employees.employee_id%TYPE ,in_increase_pct IN
types_up.percentage ,out_new_salary OUT
employees.salary%TYPE)
IS
co_sql_stmt CONSTANT types_up.big_string_type := '
UPDATE employees SET salary = salary + (salary / 100 * :1)
WHERE employee_id = :2 RETURNING salary INTO :3';
BEGIN
EXECUTE IMMEDIATE co_sql_stmt
USING in_increase_pct, in_employee_id, OUT
out_new_salary;
END upd_salary;
END employee_api; /
GOOD
CREATE OR REPLACE PACKAGE BODY employee_api IS
PROCEDURE upd_salary (in_employee_id IN
employees.employee_id%TYPE ,in_increase_pct IN
types_up.percentage ,out_new_salary OUT
employees.salary%TYPE)
IS
co_sql_stmt CONSTANT types_up.big_string_type :=
'UPDATE employees SET salary = salary + (salary / 100 * :1)
WHERE employee_id = :2 RETURNING salary INTO :3';
BEGIN
EXECUTE IMMEDIATE co_sql_stmt
USING in_increase_pct, in_employee_id
RETURNING INTO out_new_salary;
END upd_salary;
END employee_api; /
STORED OBJECTS
60. TRY TO USE NAMED NOTATION WHEN
CALLING PROGRAM UNITS.
BAD
DECLARE
r_employee employees%rowtype;
co_id CONSTANT employees.employee_id%type :=
107;
BEGIN
r_employee :=
employee_api.employee_by_id(co_id);
END; /
GOOD
DECLARE
r_employee employees%rowtype;
co_id CONSTANT employees.employee_id%type :=
107;
BEGIN
r_employee :=
employee_api.employee_by_id(in_employee_id =>
co_id);
END; /
61. ALWAYS ADD THE NAME OF THE PROGRAM
UNIT TO ITS END KEYWORD.
BAD
CREATE OR REPLACE PACKAGE BODY employee_api IS
FUNCTION employee_by_id (in_employee_id IN employees.employee_id%TYPE)
RETURN employees%rowtype IS
r_employee employees%rowtype;
BEGIN
SELECT *
INTO r_employee
FROM employees
WHERE employee_id = in_employee_id;
RETURN r_employee;
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
WHEN TOO_MANY_ROWS THEN
RAISE;
END;
END; /
GOOD
CREATE OR REPLACE PACKAGE BODY employee_api IS
FUNCTION employee_by_id (in_employee_id IN employees.employee_id%TYPE)
RETURN employees%rowtype IS
r_employee employees%rowtype;
BEGIN
SELECT *
INTO r_employee
FROM employees
WHERE employee_id = in_employee_id;
RETURN r_employee;
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
WHEN TOO_MANY_ROWS THEN
RAISE;
END employee_by_id;
END employee_api; /
62. ALWAYS USE PARAMETERS OR PULL IN
DEFINITIONS RATHER THAN REFERENCING
EXTERNAL VARIABLES IN A LOCAL PROGRAM UNIT.
BAD
CREATE OR REPLACE PACKAGE BODY EMPLOYEE_API IS
PROCEDURE calc_salary (in_employee_id IN employees.employee_id%TYPE) IS
r_emp employees%rowtype;
FUNCTION commission RETURN NUMBER IS
l_commission employees.salary%TYPE := 0;
BEGIN
IF r_emp.commission_pct IS NOT NULL
THEN
l_commission := r_emp.salary * r_emp.commission_pct;
END IF;
RETURN l_commission;
END commission;
BEGIN
SELECT * INTO r_emp FROM employees WHERE employee_id = in_employee_id;
SYS.DBMS_OUTPUT.PUT_LINE(r_emp.salary + commission());
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
WHEN TOO_MANY_ROWS THEN
NULL;
END calc_salary;
END employee_api; /
GOOD
CREATE OR REPLACE PACKAGE BODY EMPLOYEE_API IS
PROCEDURE calc_salary (in_employee_id IN employees.employee_id%TYPE) IS
r_emp employees%rowtype;
FUNCTION commission (in_salary IN employees.salary%TYPE ,in_comm_pct IN employees.commission_pct%TYPE)
RETURN NUMBER IS
l_commission employees.salary%TYPE := 0;
BEGIN
IF in_comm_pct IS NOT NULL THEN
l_commission := in_salary * in_comm_pct;
END IF;
RETURN l_commission;
END commission;
BEGIN
SELECT * INTO r_emp FROM employees WHERE employee_id = in_employee_id;
SYS.DBMS_OUTPUT.PUT_LINE( r_emp.salary + commission(in_salary => r_emp.salary ,in_comm_pct =>
r_emp.commission_pct) );
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
WHEN TOO_MANY_ROWS THEN
NULL;
END calc_salary;
END employee_api; /
63. ALWAYS ENSURE THAT LOCALLY DEFINED
PROCEDURES OR FUNCTIONS ARE
REFERENCED.
BAD
CREATE OR REPLACE PACKAGE BODY
my_package IS
PROCEDURE my_procedure IS
FUNCTION my_func RETURN NUMBER IS
co_true CONSTANT INTEGER := 1;
BEGIN
RETURN co_true;
END my_func;
BEGIN
NULL;
END my_procedure;
END my_package; /
GOOD
CREATE OR REPLACE PACKAGE BODY
my_package IS
PROCEDURE my_procedure IS
FUNCTION my_func RETURN NUMBER IS
co_true CONSTANT INTEGER := 1;
BEGIN
RETURN co_true;
END my_func;
BEGIN
sys.dbms_output.put_line(my_func());
END my_procedure;
END my_package; /
64. TRY TO REMOVE UNUSED PARAMETERS
OR MODIFY CODE TO USE THE PARAMETER.
BAD
CREATE OR REPLACE PACKAGE BODY department_api IS
FUNCTION name_by_id (in_department_id IN
departments.department_id%TYPE,in_manager_id IN
departments.manager_id%TYPE)
RETURN departments.department_name%TYPE IS
l_department_name departments.department_name%TYPE;
BEGIN
<<find_department>>
BEGIN
SELECT department_name INTO l_department_name FROM
departments WHERE department_id = in_department_id;
EXCEPTION
WHEN NO_DATA_FOUND OR TOO_MANY_ROWS THEN
l_department_name := NULL;
END find_department;
RETURN l_department_name;
END name_by_id;
END department_api; /
GOOD
CREATE OR REPLACE PACKAGE BODY department_api IS
FUNCTION name_by_id (in_department_id IN
departments.department_id%TYPE)
RETURN departments.department_name%TYPE IS
l_department_name departments.department_name%TYPE;
BEGIN
<<find_department>>
BEGIN
SELECT department_name INTO l_department_name FROM
departments WHERE department_id = in_department_id;
EXCEPTION
WHEN NO_DATA_FOUND OR TOO_MANY_ROWS THEN
l_department_name := NULL;
END find_department;
RETURN l_department_name;
END name_by_id;
END department_api; /
REFERENCE
26
REFERENCE
 Trivadis PL/SQL & SQL Coding Guidelines Version
2.0
 http://www.trivadis.com/sites/default/files/downloads/PL
SQL_and_SQL_Coding_Guidelines_2_0_HiRes.pdf
27
Q&A
28
QUESTION & ANSWER
29

More Related Content

What's hot

Immutability and pure functions
Immutability and pure functionsImmutability and pure functions
Immutability and pure functionssparkfabrik
 
Apex 5 plugins for everyone version 2018
Apex 5 plugins for everyone   version 2018Apex 5 plugins for everyone   version 2018
Apex 5 plugins for everyone version 2018Alan Arentsen
 
Building Maintainable Applications in Apex
Building Maintainable Applications in ApexBuilding Maintainable Applications in Apex
Building Maintainable Applications in ApexJeffrey Kemp
 
Procedure and Functions in pl/sql
Procedure and Functions in pl/sqlProcedure and Functions in pl/sql
Procedure and Functions in pl/sqlÑirmal Tatiwal
 
Functional Reactive Programming dengan ReactiveCocoa
Functional Reactive Programming dengan ReactiveCocoaFunctional Reactive Programming dengan ReactiveCocoa
Functional Reactive Programming dengan ReactiveCocoaDidiet Noor
 
Basic cursors in oracle
Basic cursors in oracleBasic cursors in oracle
Basic cursors in oracleSuhel Firdus
 
Types of operators in C
Types of operators in CTypes of operators in C
Types of operators in CPrabhu Govind
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements Tarun Sharma
 

What's hot (18)

Immutability and pure functions
Immutability and pure functionsImmutability and pure functions
Immutability and pure functions
 
Les06
Les06Les06
Les06
 
Apex 5 plugins for everyone version 2018
Apex 5 plugins for everyone   version 2018Apex 5 plugins for everyone   version 2018
Apex 5 plugins for everyone version 2018
 
Perl_Part2
Perl_Part2Perl_Part2
Perl_Part2
 
Building Maintainable Applications in Apex
Building Maintainable Applications in ApexBuilding Maintainable Applications in Apex
Building Maintainable Applications in Apex
 
Functions
FunctionsFunctions
Functions
 
Savitch ch 05
Savitch ch 05Savitch ch 05
Savitch ch 05
 
PLSQL Cursors
PLSQL CursorsPLSQL Cursors
PLSQL Cursors
 
Functions
FunctionsFunctions
Functions
 
Savitch Ch 05
Savitch Ch 05Savitch Ch 05
Savitch Ch 05
 
Procedure and Functions in pl/sql
Procedure and Functions in pl/sqlProcedure and Functions in pl/sql
Procedure and Functions in pl/sql
 
Meet the CBO in Version 11g
Meet the CBO in Version 11gMeet the CBO in Version 11g
Meet the CBO in Version 11g
 
Functional Reactive Programming dengan ReactiveCocoa
Functional Reactive Programming dengan ReactiveCocoaFunctional Reactive Programming dengan ReactiveCocoa
Functional Reactive Programming dengan ReactiveCocoa
 
Basic cursors in oracle
Basic cursors in oracleBasic cursors in oracle
Basic cursors in oracle
 
Types of operators in C
Types of operators in CTypes of operators in C
Types of operators in C
 
Procedures andcursors
Procedures andcursorsProcedures andcursors
Procedures andcursors
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements
 
Savitch Ch 11
Savitch Ch 11Savitch Ch 11
Savitch Ch 11
 

Similar to PL/SQL & SQL CODING GUIDELINES – Part 8

A New View of Database Views
A New View of Database ViewsA New View of Database Views
A New View of Database ViewsMichael Rosenblum
 
Plsql coding conventions
Plsql coding conventionsPlsql coding conventions
Plsql coding conventionsFang Yu
 
Oracle PL/SQL - Creative Conditional Compilation
Oracle PL/SQL - Creative Conditional CompilationOracle PL/SQL - Creative Conditional Compilation
Oracle PL/SQL - Creative Conditional CompilationScott Wesley
 
Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07Thuan Nguyen
 
9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPTTheVerse1
 
Common Coding and Design mistakes (that really mess up performance)
Common Coding and Design mistakes (that really mess up performance)Common Coding and Design mistakes (that really mess up performance)
Common Coding and Design mistakes (that really mess up performance)Sage Computing Services
 
Oracle - Program with PL/SQL - Lession 10
Oracle - Program with PL/SQL - Lession 10Oracle - Program with PL/SQL - Lession 10
Oracle - Program with PL/SQL - Lession 10Thuan Nguyen
 
Programming in Oracle with PL/SQL
Programming in Oracle with PL/SQLProgramming in Oracle with PL/SQL
Programming in Oracle with PL/SQLlubna19
 
Oracle PL/SQL Bulk binds
Oracle PL/SQL Bulk bindsOracle PL/SQL Bulk binds
Oracle PL/SQL Bulk bindsScott Wesley
 
Plsql task answers
Plsql task answersPlsql task answers
Plsql task answersNawaz Sk
 
pl/sql Procedure
pl/sql Procedurepl/sql Procedure
pl/sql ProcedurePooja Dixit
 
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
 

Similar to PL/SQL & SQL CODING GUIDELINES – Part 8 (20)

A New View of Database Views
A New View of Database ViewsA New View of Database Views
A New View of Database Views
 
PLSQL.docx
PLSQL.docxPLSQL.docx
PLSQL.docx
 
Plsql coding conventions
Plsql coding conventionsPlsql coding conventions
Plsql coding conventions
 
PLSQLIV.ppt
PLSQLIV.pptPLSQLIV.ppt
PLSQLIV.ppt
 
Substitution
SubstitutionSubstitution
Substitution
 
Oracle PL/SQL - Creative Conditional Compilation
Oracle PL/SQL - Creative Conditional CompilationOracle PL/SQL - Creative Conditional Compilation
Oracle PL/SQL - Creative Conditional Compilation
 
Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07
 
Pl sql guide
Pl sql guidePl sql guide
Pl sql guide
 
9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT
 
February0504 pm
February0504 pmFebruary0504 pm
February0504 pm
 
Common Coding and Design mistakes (that really mess up performance)
Common Coding and Design mistakes (that really mess up performance)Common Coding and Design mistakes (that really mess up performance)
Common Coding and Design mistakes (that really mess up performance)
 
Oracle - Program with PL/SQL - Lession 10
Oracle - Program with PL/SQL - Lession 10Oracle - Program with PL/SQL - Lession 10
Oracle - Program with PL/SQL - Lession 10
 
Programming in Oracle with PL/SQL
Programming in Oracle with PL/SQLProgramming in Oracle with PL/SQL
Programming in Oracle with PL/SQL
 
Trig
TrigTrig
Trig
 
Oracle PL/SQL Bulk binds
Oracle PL/SQL Bulk bindsOracle PL/SQL Bulk binds
Oracle PL/SQL Bulk binds
 
Plsql task answers
Plsql task answersPlsql task answers
Plsql task answers
 
pl/sql Procedure
pl/sql Procedurepl/sql Procedure
pl/sql Procedure
 
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
 
Plsql programs
Plsql programsPlsql programs
Plsql programs
 
SQL Procedures & Functions
SQL Procedures & FunctionsSQL Procedures & Functions
SQL Procedures & Functions
 

More from Larry Nung

Ansible - simple it automation
Ansible - simple it automationAnsible - simple it automation
Ansible - simple it automationLarry Nung
 
sonarwhal - a linting tool for the web
sonarwhal - a linting tool for the websonarwhal - a linting tool for the web
sonarwhal - a linting tool for the webLarry Nung
 
LiteDB - A .NET NoSQL Document Store in a single data file
LiteDB - A .NET NoSQL Document Store in a single data fileLiteDB - A .NET NoSQL Document Store in a single data file
LiteDB - A .NET NoSQL Document Store in a single data fileLarry Nung
 
MessagePack - An efficient binary serialization format
MessagePack - An efficient binary serialization formatMessagePack - An efficient binary serialization format
MessagePack - An efficient binary serialization formatLarry Nung
 
PL/SQL & SQL CODING GUIDELINES – Part 7
PL/SQL & SQL CODING GUIDELINES – Part 7PL/SQL & SQL CODING GUIDELINES – Part 7
PL/SQL & SQL CODING GUIDELINES – Part 7Larry Nung
 
BenchmarkDotNet - Powerful .NET library for benchmarking
BenchmarkDotNet  - Powerful .NET library for benchmarkingBenchmarkDotNet  - Powerful .NET library for benchmarking
BenchmarkDotNet - Powerful .NET library for benchmarkingLarry Nung
 
PLSQL Coding Guidelines - Part 6
PLSQL Coding Guidelines - Part 6PLSQL Coding Guidelines - Part 6
PLSQL Coding Guidelines - Part 6Larry Nung
 
SonarQube - The leading platform for Continuous Code Quality
SonarQube - The leading platform for Continuous Code QualitySonarQube - The leading platform for Continuous Code Quality
SonarQube - The leading platform for Continuous Code QualityLarry Nung
 
Visual studio 2017
Visual studio 2017Visual studio 2017
Visual studio 2017Larry Nung
 
Web deploy command line
Web deploy command lineWeb deploy command line
Web deploy command lineLarry Nung
 
Topshelf - An easy service hosting framework for building Windows services us...
Topshelf - An easy service hosting framework for building Windows services us...Topshelf - An easy service hosting framework for building Windows services us...
Topshelf - An easy service hosting framework for building Windows services us...Larry Nung
 
Common.logging
Common.loggingCommon.logging
Common.loggingLarry Nung
 
protobuf-net - Protocol Buffers library for idiomatic .NET
protobuf-net - Protocol Buffers library for idiomatic .NETprotobuf-net - Protocol Buffers library for idiomatic .NET
protobuf-net - Protocol Buffers library for idiomatic .NETLarry Nung
 
Regular expression
Regular expressionRegular expression
Regular expressionLarry Nung
 
PL/SQL & SQL CODING GUIDELINES – Part 4
PL/SQL & SQL CODING GUIDELINES – Part 4PL/SQL & SQL CODING GUIDELINES – Part 4
PL/SQL & SQL CODING GUIDELINES – Part 4Larry Nung
 
Fx.configuration
Fx.configurationFx.configuration
Fx.configurationLarry Nung
 
StackExchange.redis
StackExchange.redisStackExchange.redis
StackExchange.redisLarry Nung
 

More from Larry Nung (20)

Ansible - simple it automation
Ansible - simple it automationAnsible - simple it automation
Ansible - simple it automation
 
sonarwhal - a linting tool for the web
sonarwhal - a linting tool for the websonarwhal - a linting tool for the web
sonarwhal - a linting tool for the web
 
LiteDB - A .NET NoSQL Document Store in a single data file
LiteDB - A .NET NoSQL Document Store in a single data fileLiteDB - A .NET NoSQL Document Store in a single data file
LiteDB - A .NET NoSQL Document Store in a single data file
 
MessagePack - An efficient binary serialization format
MessagePack - An efficient binary serialization formatMessagePack - An efficient binary serialization format
MessagePack - An efficient binary serialization format
 
PL/SQL & SQL CODING GUIDELINES – Part 7
PL/SQL & SQL CODING GUIDELINES – Part 7PL/SQL & SQL CODING GUIDELINES – Part 7
PL/SQL & SQL CODING GUIDELINES – Part 7
 
BenchmarkDotNet - Powerful .NET library for benchmarking
BenchmarkDotNet  - Powerful .NET library for benchmarkingBenchmarkDotNet  - Powerful .NET library for benchmarking
BenchmarkDotNet - Powerful .NET library for benchmarking
 
PLSQL Coding Guidelines - Part 6
PLSQL Coding Guidelines - Part 6PLSQL Coding Guidelines - Part 6
PLSQL Coding Guidelines - Part 6
 
SonarQube - The leading platform for Continuous Code Quality
SonarQube - The leading platform for Continuous Code QualitySonarQube - The leading platform for Continuous Code Quality
SonarQube - The leading platform for Continuous Code Quality
 
Visual studio 2017
Visual studio 2017Visual studio 2017
Visual studio 2017
 
Web deploy command line
Web deploy command lineWeb deploy command line
Web deploy command line
 
Web deploy
Web deployWeb deploy
Web deploy
 
SikuliX
SikuliXSikuliX
SikuliX
 
Topshelf - An easy service hosting framework for building Windows services us...
Topshelf - An easy service hosting framework for building Windows services us...Topshelf - An easy service hosting framework for building Windows services us...
Topshelf - An easy service hosting framework for building Windows services us...
 
Common.logging
Common.loggingCommon.logging
Common.logging
 
protobuf-net - Protocol Buffers library for idiomatic .NET
protobuf-net - Protocol Buffers library for idiomatic .NETprotobuf-net - Protocol Buffers library for idiomatic .NET
protobuf-net - Protocol Buffers library for idiomatic .NET
 
Regular expression
Regular expressionRegular expression
Regular expression
 
PL/SQL & SQL CODING GUIDELINES – Part 4
PL/SQL & SQL CODING GUIDELINES – Part 4PL/SQL & SQL CODING GUIDELINES – Part 4
PL/SQL & SQL CODING GUIDELINES – Part 4
 
Fx.configuration
Fx.configurationFx.configuration
Fx.configuration
 
StackExchange.redis
StackExchange.redisStackExchange.redis
StackExchange.redis
 
Disruptor
DisruptorDisruptor
Disruptor
 

Recently uploaded

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
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
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
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
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 

Recently uploaded (20)

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
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...
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
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
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 

PL/SQL & SQL CODING GUIDELINES – Part 8

  • 1. PL/SQL & SQL CODING GUIDELINES – PART 8 Larry Nung
  • 4. 58. ALWAYS USE A STRING VARIABLE TO EXECUTE DYNAMIC SQL.
  • 5. BAD -- Bad DECLARE l_empno emp.empno%TYPE := 4711; BEGIN EXECUTE IMMEDIATE 'DELETE FROM emp WHERE epno = :p_empno' USING l_empno; END;
  • 6. GOOD -- Good DECLARE l_empno emp.empno%TYPE := 4711; l_sql VARCHAR2(32767); BEGIN l_sql := 'DELETE FROM emp WHERE epno = :p_empno'; EXECUTE IMMEDIATE l_sql USING l_empno; EXCEPTION WHEN others THEN DBMS_OUTPUT.PUT_LINE(l_sql); END;
  • 7. 59. TRY TO USE OUTPUT BIND ARGUMENTS IN THE RETURNING INTO CLAUSE OF DYNAMIC INSERT, UPDATE, OR DELETE STATEMENTS RATHER THAN THE USING CLAUSE.
  • 8. BAD CREATE OR REPLACE PACKAGE BODY employee_api IS PROCEDURE upd_salary (in_employee_id IN employees.employee_id%TYPE ,in_increase_pct IN types_up.percentage ,out_new_salary OUT employees.salary%TYPE) IS co_sql_stmt CONSTANT types_up.big_string_type := ' UPDATE employees SET salary = salary + (salary / 100 * :1) WHERE employee_id = :2 RETURNING salary INTO :3'; BEGIN EXECUTE IMMEDIATE co_sql_stmt USING in_increase_pct, in_employee_id, OUT out_new_salary; END upd_salary; END employee_api; /
  • 9. GOOD CREATE OR REPLACE PACKAGE BODY employee_api IS PROCEDURE upd_salary (in_employee_id IN employees.employee_id%TYPE ,in_increase_pct IN types_up.percentage ,out_new_salary OUT employees.salary%TYPE) IS co_sql_stmt CONSTANT types_up.big_string_type := 'UPDATE employees SET salary = salary + (salary / 100 * :1) WHERE employee_id = :2 RETURNING salary INTO :3'; BEGIN EXECUTE IMMEDIATE co_sql_stmt USING in_increase_pct, in_employee_id RETURNING INTO out_new_salary; END upd_salary; END employee_api; /
  • 11. 60. TRY TO USE NAMED NOTATION WHEN CALLING PROGRAM UNITS.
  • 12. BAD DECLARE r_employee employees%rowtype; co_id CONSTANT employees.employee_id%type := 107; BEGIN r_employee := employee_api.employee_by_id(co_id); END; /
  • 13. GOOD DECLARE r_employee employees%rowtype; co_id CONSTANT employees.employee_id%type := 107; BEGIN r_employee := employee_api.employee_by_id(in_employee_id => co_id); END; /
  • 14. 61. ALWAYS ADD THE NAME OF THE PROGRAM UNIT TO ITS END KEYWORD.
  • 15. BAD CREATE OR REPLACE PACKAGE BODY employee_api IS FUNCTION employee_by_id (in_employee_id IN employees.employee_id%TYPE) RETURN employees%rowtype IS r_employee employees%rowtype; BEGIN SELECT * INTO r_employee FROM employees WHERE employee_id = in_employee_id; RETURN r_employee; EXCEPTION WHEN NO_DATA_FOUND THEN NULL; WHEN TOO_MANY_ROWS THEN RAISE; END; END; /
  • 16. GOOD CREATE OR REPLACE PACKAGE BODY employee_api IS FUNCTION employee_by_id (in_employee_id IN employees.employee_id%TYPE) RETURN employees%rowtype IS r_employee employees%rowtype; BEGIN SELECT * INTO r_employee FROM employees WHERE employee_id = in_employee_id; RETURN r_employee; EXCEPTION WHEN NO_DATA_FOUND THEN NULL; WHEN TOO_MANY_ROWS THEN RAISE; END employee_by_id; END employee_api; /
  • 17. 62. ALWAYS USE PARAMETERS OR PULL IN DEFINITIONS RATHER THAN REFERENCING EXTERNAL VARIABLES IN A LOCAL PROGRAM UNIT.
  • 18. BAD CREATE OR REPLACE PACKAGE BODY EMPLOYEE_API IS PROCEDURE calc_salary (in_employee_id IN employees.employee_id%TYPE) IS r_emp employees%rowtype; FUNCTION commission RETURN NUMBER IS l_commission employees.salary%TYPE := 0; BEGIN IF r_emp.commission_pct IS NOT NULL THEN l_commission := r_emp.salary * r_emp.commission_pct; END IF; RETURN l_commission; END commission; BEGIN SELECT * INTO r_emp FROM employees WHERE employee_id = in_employee_id; SYS.DBMS_OUTPUT.PUT_LINE(r_emp.salary + commission()); EXCEPTION WHEN NO_DATA_FOUND THEN NULL; WHEN TOO_MANY_ROWS THEN NULL; END calc_salary; END employee_api; /
  • 19. GOOD CREATE OR REPLACE PACKAGE BODY EMPLOYEE_API IS PROCEDURE calc_salary (in_employee_id IN employees.employee_id%TYPE) IS r_emp employees%rowtype; FUNCTION commission (in_salary IN employees.salary%TYPE ,in_comm_pct IN employees.commission_pct%TYPE) RETURN NUMBER IS l_commission employees.salary%TYPE := 0; BEGIN IF in_comm_pct IS NOT NULL THEN l_commission := in_salary * in_comm_pct; END IF; RETURN l_commission; END commission; BEGIN SELECT * INTO r_emp FROM employees WHERE employee_id = in_employee_id; SYS.DBMS_OUTPUT.PUT_LINE( r_emp.salary + commission(in_salary => r_emp.salary ,in_comm_pct => r_emp.commission_pct) ); EXCEPTION WHEN NO_DATA_FOUND THEN NULL; WHEN TOO_MANY_ROWS THEN NULL; END calc_salary; END employee_api; /
  • 20. 63. ALWAYS ENSURE THAT LOCALLY DEFINED PROCEDURES OR FUNCTIONS ARE REFERENCED.
  • 21. BAD CREATE OR REPLACE PACKAGE BODY my_package IS PROCEDURE my_procedure IS FUNCTION my_func RETURN NUMBER IS co_true CONSTANT INTEGER := 1; BEGIN RETURN co_true; END my_func; BEGIN NULL; END my_procedure; END my_package; /
  • 22. GOOD CREATE OR REPLACE PACKAGE BODY my_package IS PROCEDURE my_procedure IS FUNCTION my_func RETURN NUMBER IS co_true CONSTANT INTEGER := 1; BEGIN RETURN co_true; END my_func; BEGIN sys.dbms_output.put_line(my_func()); END my_procedure; END my_package; /
  • 23. 64. TRY TO REMOVE UNUSED PARAMETERS OR MODIFY CODE TO USE THE PARAMETER.
  • 24. BAD CREATE OR REPLACE PACKAGE BODY department_api IS FUNCTION name_by_id (in_department_id IN departments.department_id%TYPE,in_manager_id IN departments.manager_id%TYPE) RETURN departments.department_name%TYPE IS l_department_name departments.department_name%TYPE; BEGIN <<find_department>> BEGIN SELECT department_name INTO l_department_name FROM departments WHERE department_id = in_department_id; EXCEPTION WHEN NO_DATA_FOUND OR TOO_MANY_ROWS THEN l_department_name := NULL; END find_department; RETURN l_department_name; END name_by_id; END department_api; /
  • 25. GOOD CREATE OR REPLACE PACKAGE BODY department_api IS FUNCTION name_by_id (in_department_id IN departments.department_id%TYPE) RETURN departments.department_name%TYPE IS l_department_name departments.department_name%TYPE; BEGIN <<find_department>> BEGIN SELECT department_name INTO l_department_name FROM departments WHERE department_id = in_department_id; EXCEPTION WHEN NO_DATA_FOUND OR TOO_MANY_ROWS THEN l_department_name := NULL; END find_department; RETURN l_department_name; END name_by_id; END department_api; /
  • 27. REFERENCE  Trivadis PL/SQL & SQL Coding Guidelines Version 2.0  http://www.trivadis.com/sites/default/files/downloads/PL SQL_and_SQL_Coding_Guidelines_2_0_HiRes.pdf 27