SlideShare a Scribd company logo
SQL Stored Procedures
and Application Modernization
John Valance
Division 1 Systems
johnv@div1sys.com
© 2017 Division 1 Systems
<div1>
www.div1sys.com
2
• 30+ years IBM midrange experience (S/38 thru IBM i)
• 17+ years of web development experience
• Independent consultant since early 2000
• Community Involvement
 COMMON Board of Directors
 Presenter at IBM i groups nationwide
• Founder and CTO of Division 1 Systems
 Web / Mobile applications for IBMi
 Full SDLC - design, project management, coding, training, consulting
 Extended team - Can scale up/down to meet client needs
• Relationship with Zend / RogueWave
 Teacher, Reseller, Zend Certified Engineer
About John Valance
Stored Procedures:
What, Why, Who?
4
•Introduction
 Who, What, Why
 Application Modernization
•Creating Stored Procedures
•SQL Procedure Language
 Language Syntax and Capabilities
•Creating UDFs
•IBM i Considerations
•Examples from the trenches (time permitting)
What We Will Cover
5
• Any program object on IBM i
 Known to DB2 via CREATE PROCEDURE statement
• 2 types:
 SQL (written in SQL/PL)
 External (RPG, CL, any language)
 We will focus on SQL stored procedures
• Can be called from any environment that supports SQL
• Can have parameters for input / output
• Can return result sets
• Can be selected from the database repository
 SELECT * FROM QSYS2/SYSPROCS WHERE ROUTINE_SCHEMA = 'MYLIBR'
What Are Stored Procedures?
6
•SQL Procedure Language
•Allows SQL scripts to be built
 Any SQL statements, plus variables, conditions, loops, etc.
 Data-centric programming
•DB2 SQL/PL is proprietary
 but all major DB vendors have proprietary PL
•Compiled using an SQL client (ACS recommended)
•Generates an ILE/C language program, with embedded
SQL calls
What is SQL/PL?
7
Simple example - sp_Cust
8
•ACS = Access Client Solutions
 Formerly known as Client Access
 Has an SQL client which is perfect for developing stored
procedures on IBM i
Running sp_Cust from ACS
9
•Stored procedures
•User Defined Functions (UDF)
•Triggers – before & after (add/change/delete)
What Can I Build with SQL/PL?
10
New Redbook – April 2016!!
• SQL Procedures, Triggers, and Functions on IBM DB2 for i
 http://www.redbooks.ibm.com/abstracts/sg248326.html
 Download the PDF!!
• Also:
 DB2 for i SQL reference
https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_72/db2/rbafzprintthis.htm
 SQL Programming Guide:
https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_73/sqlp/rbafykickoff.htm
Redbooks / References
11
• Declarative, standardized
programming language
 Puts SQL in the driver's seat
• Centralize business logic
 Data-Centric programming
 Business logic close to the DBMS
• Simplicity of application code
 No embedded SQL
 No ORM issues
• Impact of change insulation
 No level checks
 Loose coupling between DB and apps
Benefits of SQL Stored Procedures
• Leverage SQL enhancements
 Features and performance
• Security
 No SQL injection
 Define authority on stored procedures
vs. tables/views
• Performance
 Execution plan / Precompiled
• Modernize DB interface
 Similar to a View
 Renamed fields, Derived fields
 Provide simple interface to complex
legacy DB structure
12
• IT Managers / Directors
 Don't replace – Refactor!
• RPG Programmers
 Modernize your database skills
 You probably know SQL already
• Database Administrators
 Play an active role in application
modernization
• Project Managers
 Focus on database issues
 Delegate programming
Whom Is This For?
• Web Application Developers
 Browser applications
 Ajax developers
 API / Web Service Developers
• Users of Analysis & Reporting Tools
 Excel spreadsheets / VBA apps
 Crystal Reports and other reporting tools
Building Procedures
with SQL/PL
14
Anatomy of a Stored Procedure
15
Anatomy of a Stored Procedure
Create procedure
statement
JV1
Slide 15
JV1 John Valance, 4/7/2017
16
Anatomy of a Stored Procedure
Options
(many available)
17
Anatomy of a Stored Procedure
Procedure Body
18
Anatomy of a Stored Procedure
Body is a compound SQL
statement enclosed in
begin/end block
19
Anatomy of a Stored Procedure
Declarations
20
Anatomy of a Stored Procedure
Executable
statements
21
•Procedural programming (RPG)
 Specify how to get the data
•Declarative programming (SQL)
 Specify what you want from the database
 Database will figure out the most efficient way to execute
•Always select from PF / Table, not LF or View
•Database will create an access plan – stored with object
•Create indices (LFs) to improve performance
 SQL Performance Center in ACS
 Help with DB tuning based on runtime analysis
SQL = Declarative Programming
22
Need to use an SQL Client to run the CREATE PROCEDURE
• SQL client choices
 ACS = Access Client Solutions ( ** best choice ** )
 Green screen STRSQL ( awkward ) $$
 Eclipse Data Tools plug-in for RDi, Zend Studio, etc. (pretty good)
 Other SQL Clients – JDBC, ODBC (ex.: http://www.sql-workbench.net/)
• Biggest issue is handling output parameters
 ACS or Client Access handles this well
 ACS / CA also gives best diagnostic messages
• ACS is FREE!!
 https://www-03.ibm.com/systems/power/software/i/access/solutions.html
Compiling and Running
23
Store your source code in an .sql file
• On your PC
 With ACS installed, double click to open and run
• On IBM i IFS
 Use an IDE like Eclipse, RDi, Zend Studio
• Includes SQL syntax highlighting
• May need to install Data Tools Platform SQL Dev Tools
(help menu… Install New Software)
 Can open IFS file in ACS
• Right click… Open With… System Editor
• SRCPF ? (maybe, but not for me)
 SEU? (really??)
 RUNSQLSTM or STRSQL (hmmm…)
Where To Put Source Code?
24
Run SQL Scripts in ACS
Connect to Database
25
Run SQL Scripts in ACS
Position Cursor on Statement to Run
Then…
• Use Run Menu, or…
• Click Run Icons , or…
• Press Ctrl+R
26
Run SQL Scripts in ACS
Run the CREATE
27
Run SQL Scripts in ACS
Run the CALL
Results are displayed
28
Running from PHP
29
Running from PHP
30
Use MS Query, and edit SQL directly: call sp_cust( )
Running from Excel
31
•Add 2 input parameters: State and Country filters
 Add to parameter list
 Add to WHERE clause to serve as result filters
Adding Parameters
 Separate multiple
parameters with commas
 Can all go on one line, but
easier to read on separate
lines
32
•IN = Input
•OUT = Output
•INOUT = Input and Output
Types of Parameters
33
Run / Test SP with Parameters
call jvalance.sp_cust_parm('HI', 'US');
call jvalance.sp_cust_parm('OR', '');
call jvalance.sp_cust_parm('', 'Canada');
May yield
unexpected results!!
These both
return 0 rows
34
Ignoring Blank Filter Parameters
35
Ignoring Blank Filter Parameters
36
Ignoring Blank Filter Parameters
37
Ignoring Blank Filter Parameters
38
• Procedure Overloading
 Two or more procedures with same name, but different signatures
• Procedure Signature
 Name + Number of Parameters (data type irrelevant)
• Ex.: these have different signatures
– MyProc(char(5), int)
– MyProc(int)
• these have same signature:
– MyProc(char(5))
– MyProc(int)
• Can cause a lot of confusion
• DROP PROCEDURE explicitly using Navigator… Databases
 Drill down to procedures
Beware Procedure Signatures!!
SQL/PL Basics
40
Trimming Input Search Parms
• All the trim() function calls make the code harder to read
• Let’s create some variables to hold the trimmed values for reuse
41
•Create variables to hold trimmed() input values
Declaring/Using Variables
42
•Create variables to hold trimmed() input values
Declaring/Using Variables
Declare variables
before cursors
43
•Create variables to hold trimmed() input values
Declaring/Using Variables
Declaration
statements
Executable
statements
44
sp_Get_Cust_Name
More Features of SQL/PL
Output Parameters
No Result Sets
SELECT INTO
Conditional Logic
(IF /THEN/ ELSE)
45
Running sp_Get_Cust_Name
Use ?’s as
placeholders for
output
parameters
Output parm
values are
shown in
Messages panel
46
Running sp_Get_Cust_Name
47
Running sp_Get_Cust_Name - ERROR
User Defined Functions
(UDFs)
49
• Two types:
 User Defined Scalar Functions
 User Defined Table Functions
• We will focus on scalar functions
 Like BIFs or SQL scalar functions
• SUBSTRING(‘hello world’, 7) => returns ‘world’
• STRIP(‘ blank on both ends ‘) => returns ‘blank on both ends‘
• UPPER(‘hello’) => returns ‘HELLO’
 Can be used in
• Expressions
– Ex: if upper(in_ShowPricing) = ‘Y’ then…
• Select lists as computed columns
– Ex: select substring(COMPANY, 1, 5) as COMP_SHORT from SP_CUST
User Defined Functions
50
fn_Get_Cust_Name(in_cust_id)
51
fn_Get_Cust_Name(in_cust_id)
Declare a
variable to hold
the return
value
Set the return
variable value
Use return
statement to return
the value
CREATE FUNCTION
Input parameters only!
(Don’t specify IN)
Declare returned
data type
52
•You can test your UDFs using the SYSDUMMY1 table
 IBM-supplied, single record table, for testing function calls
 In library SYSIBM
Testing fn_Get_Cust_Name(in_cust_id)
select 1384 as cust_id, fn_Get_Cust_Name(1384) as cust_name
from sysibm.sysdummy1
53
Format current date as dec(8,0) in YYYYMMDD format
Another Example - fn_CurrDate8()
Looping
55
•Loop infinitely
•Requires a conditional LEAVE or RETURN statement to exit
the loop
•Like RPG DO
The LOOP loop
56
•Loop until condition is false
•Condition tested at beginning of loop
 Like RPG DOW
 Set condition before loop, or it may never enter loop body
•Can also use LEAVE or RETURN statements to exit the loop
The WHILE loop
57
•Loop until condition is true
•Test condition at end of loop
 Like RPG DOU
 Always iterate at least once
•Can also use LEAVE or RETURN statements to exit the loop
The REPEAT UNTIL loop
58
The FOR loop
59
Running procedure sp_Build_JVCUST
IBM i Considerations
61
Nomenclature
RPG / Native IBMi SQL / RDBMS
Library Schema
File Table
Record Row
Field Column
Naming Convention
System SQL
library/file schema.table
** Can use library lists ** Cannot use library lists
62
• To use library lists, connect to DB with “System Naming”
 vs. “SQL Naming”, which only allows one library (aka schema)
 In CA/ACS: menu Connection… JDBC settings… Format tab
 In JDBC-based connections, set property: naming = system
• Run “SET PATH *LIBL ;” in ACS SQL client, before creating
procedures
 Path is stored in DB2 repository with the procedure object
• Do NOT hard-code library names in your stored procedure source
code (use un-qualified object names)
• Connect to DB with a USRPRF that has the proper library list
 via the USRPRF’s JOBD libl
 When compiling and running
Library List Considerations
63
•Library view (green-screen)
Viewing SQL Stored Procedure objects
Use SPECIFIC option to set short
object name
Automatically generated pgm object names
First 5 + seq number
create procedure sp_build_jvcust()
specific BLDJVCUST
Real-world Examples
•Time Permitting
Thanks for Attending!
66
Contact Information
John Valance
johnv@div1sys.com
802-355-4024
Division 1 Systems
www.div1sys.com

More Related Content

Recently uploaded

Mumbai Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service A...
Mumbai Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service A...Mumbai Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service A...
Mumbai Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service A...
3610stuck
 
High Girls Call Chennai 000XX00000 Provide Best And Top Girl Service And No1 ...
High Girls Call Chennai 000XX00000 Provide Best And Top Girl Service And No1 ...High Girls Call Chennai 000XX00000 Provide Best And Top Girl Service And No1 ...
High Girls Call Chennai 000XX00000 Provide Best And Top Girl Service And No1 ...
singhlata50dh
 
GT degree offer diploma Transcript
GT degree offer diploma TranscriptGT degree offer diploma Transcript
GT degree offer diploma Transcript
attueb
 
InflectraCON 360: Risk-Based Testing for Mission Critical Systems
InflectraCON 360: Risk-Based Testing for Mission Critical SystemsInflectraCON 360: Risk-Based Testing for Mission Critical Systems
InflectraCON 360: Risk-Based Testing for Mission Critical Systems
Inflectra
 
當測試開始左移
當測試開始左移當測試開始左移
當測試開始左移
Jersey (CHE-PING) Su
 
Predicting Test Results without Execution (FSE 2024)
Predicting Test Results without Execution (FSE 2024)Predicting Test Results without Execution (FSE 2024)
Predicting Test Results without Execution (FSE 2024)
andrehoraa
 
AI - Your Startup Sidekick (Leveraging AI to Bootstrap a Lean Startup).pdf
AI - Your Startup Sidekick (Leveraging AI to Bootstrap a Lean Startup).pdfAI - Your Startup Sidekick (Leveraging AI to Bootstrap a Lean Startup).pdf
AI - Your Startup Sidekick (Leveraging AI to Bootstrap a Lean Startup).pdf
Daniel Zivkovic
 
Old Tools, New Tricks: Unleashing the Power of Time-Tested Testing Tools
Old Tools, New Tricks: Unleashing the Power of Time-Tested Testing ToolsOld Tools, New Tricks: Unleashing the Power of Time-Tested Testing Tools
Old Tools, New Tricks: Unleashing the Power of Time-Tested Testing Tools
Benjamin Bischoff
 
Agra Girls Call Agra 0X0000000X Unlimited Short Providing Girls Service Avail...
Agra Girls Call Agra 0X0000000X Unlimited Short Providing Girls Service Avail...Agra Girls Call Agra 0X0000000X Unlimited Short Providing Girls Service Avail...
Agra Girls Call Agra 0X0000000X Unlimited Short Providing Girls Service Avail...
rachitkumar09887
 
Celebrity Girls Call Mumbai 9920725232 Unlimited Short Providing Girls Servic...
Celebrity Girls Call Mumbai 9920725232 Unlimited Short Providing Girls Servic...Celebrity Girls Call Mumbai 9920725232 Unlimited Short Providing Girls Servic...
Celebrity Girls Call Mumbai 9920725232 Unlimited Short Providing Girls Servic...
45unexpected
 
02. Ruby Basic slides - Ruby Core Teaching
02. Ruby Basic slides - Ruby Core Teaching02. Ruby Basic slides - Ruby Core Teaching
02. Ruby Basic slides - Ruby Core Teaching
quanhoangd129
 
Girls Call Jogeshwari 9967584737 Provide Best And Top Girl Service And No1 in...
Girls Call Jogeshwari 9967584737 Provide Best And Top Girl Service And No1 in...Girls Call Jogeshwari 9967584737 Provide Best And Top Girl Service And No1 in...
Girls Call Jogeshwari 9967584737 Provide Best And Top Girl Service And No1 in...
simran hot girls
 
🚂🚘 Premium Girls Call Ranchi 🛵🚡000XX00000 💃 Choose Best And Top Girl Service...
🚂🚘 Premium Girls Call Ranchi  🛵🚡000XX00000 💃 Choose Best And Top Girl Service...🚂🚘 Premium Girls Call Ranchi  🛵🚡000XX00000 💃 Choose Best And Top Girl Service...
🚂🚘 Premium Girls Call Ranchi 🛵🚡000XX00000 💃 Choose Best And Top Girl Service...
bahubalikumar09988
 
A Step-by-Step Guide to Selecting the Right Automated Software Testing Tools.pdf
A Step-by-Step Guide to Selecting the Right Automated Software Testing Tools.pdfA Step-by-Step Guide to Selecting the Right Automated Software Testing Tools.pdf
A Step-by-Step Guide to Selecting the Right Automated Software Testing Tools.pdf
kalichargn70th171
 
VVIP Girls Call Mumbai 9910780858 Provide Best And Top Girl Service And No1 i...
VVIP Girls Call Mumbai 9910780858 Provide Best And Top Girl Service And No1 i...VVIP Girls Call Mumbai 9910780858 Provide Best And Top Girl Service And No1 i...
VVIP Girls Call Mumbai 9910780858 Provide Best And Top Girl Service And No1 i...
jealousviolet
 
Russian Girls Call Mumbai 🛵🚡9833363713 💃 Choose Best And Top Girl Service And...
Russian Girls Call Mumbai 🛵🚡9833363713 💃 Choose Best And Top Girl Service And...Russian Girls Call Mumbai 🛵🚡9833363713 💃 Choose Best And Top Girl Service And...
Russian Girls Call Mumbai 🛵🚡9833363713 💃 Choose Best And Top Girl Service And...
dream girl
 
Busty Girls Call Mumbai 9930245274 Unlimited Short Providing Girls Service Av...
Busty Girls Call Mumbai 9930245274 Unlimited Short Providing Girls Service Av...Busty Girls Call Mumbai 9930245274 Unlimited Short Providing Girls Service Av...
Busty Girls Call Mumbai 9930245274 Unlimited Short Providing Girls Service Av...
revolutionary575
 
Independent Girls call Service Pune 000XX00000 Provide Best And Top Girl Serv...
Independent Girls call Service Pune 000XX00000 Provide Best And Top Girl Serv...Independent Girls call Service Pune 000XX00000 Provide Best And Top Girl Serv...
Independent Girls call Service Pune 000XX00000 Provide Best And Top Girl Serv...
bhumivarma35300
 
Russian Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service ...
Russian Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service ...Russian Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service ...
Russian Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service ...
shanihomely
 
06. Ruby Array & Hash - Ruby Core Teaching
06. Ruby Array & Hash - Ruby Core Teaching06. Ruby Array & Hash - Ruby Core Teaching
06. Ruby Array & Hash - Ruby Core Teaching
quanhoangd129
 

Recently uploaded (20)

Mumbai Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service A...
Mumbai Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service A...Mumbai Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service A...
Mumbai Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service A...
 
High Girls Call Chennai 000XX00000 Provide Best And Top Girl Service And No1 ...
High Girls Call Chennai 000XX00000 Provide Best And Top Girl Service And No1 ...High Girls Call Chennai 000XX00000 Provide Best And Top Girl Service And No1 ...
High Girls Call Chennai 000XX00000 Provide Best And Top Girl Service And No1 ...
 
GT degree offer diploma Transcript
GT degree offer diploma TranscriptGT degree offer diploma Transcript
GT degree offer diploma Transcript
 
InflectraCON 360: Risk-Based Testing for Mission Critical Systems
InflectraCON 360: Risk-Based Testing for Mission Critical SystemsInflectraCON 360: Risk-Based Testing for Mission Critical Systems
InflectraCON 360: Risk-Based Testing for Mission Critical Systems
 
當測試開始左移
當測試開始左移當測試開始左移
當測試開始左移
 
Predicting Test Results without Execution (FSE 2024)
Predicting Test Results without Execution (FSE 2024)Predicting Test Results without Execution (FSE 2024)
Predicting Test Results without Execution (FSE 2024)
 
AI - Your Startup Sidekick (Leveraging AI to Bootstrap a Lean Startup).pdf
AI - Your Startup Sidekick (Leveraging AI to Bootstrap a Lean Startup).pdfAI - Your Startup Sidekick (Leveraging AI to Bootstrap a Lean Startup).pdf
AI - Your Startup Sidekick (Leveraging AI to Bootstrap a Lean Startup).pdf
 
Old Tools, New Tricks: Unleashing the Power of Time-Tested Testing Tools
Old Tools, New Tricks: Unleashing the Power of Time-Tested Testing ToolsOld Tools, New Tricks: Unleashing the Power of Time-Tested Testing Tools
Old Tools, New Tricks: Unleashing the Power of Time-Tested Testing Tools
 
Agra Girls Call Agra 0X0000000X Unlimited Short Providing Girls Service Avail...
Agra Girls Call Agra 0X0000000X Unlimited Short Providing Girls Service Avail...Agra Girls Call Agra 0X0000000X Unlimited Short Providing Girls Service Avail...
Agra Girls Call Agra 0X0000000X Unlimited Short Providing Girls Service Avail...
 
Celebrity Girls Call Mumbai 9920725232 Unlimited Short Providing Girls Servic...
Celebrity Girls Call Mumbai 9920725232 Unlimited Short Providing Girls Servic...Celebrity Girls Call Mumbai 9920725232 Unlimited Short Providing Girls Servic...
Celebrity Girls Call Mumbai 9920725232 Unlimited Short Providing Girls Servic...
 
02. Ruby Basic slides - Ruby Core Teaching
02. Ruby Basic slides - Ruby Core Teaching02. Ruby Basic slides - Ruby Core Teaching
02. Ruby Basic slides - Ruby Core Teaching
 
Girls Call Jogeshwari 9967584737 Provide Best And Top Girl Service And No1 in...
Girls Call Jogeshwari 9967584737 Provide Best And Top Girl Service And No1 in...Girls Call Jogeshwari 9967584737 Provide Best And Top Girl Service And No1 in...
Girls Call Jogeshwari 9967584737 Provide Best And Top Girl Service And No1 in...
 
🚂🚘 Premium Girls Call Ranchi 🛵🚡000XX00000 💃 Choose Best And Top Girl Service...
🚂🚘 Premium Girls Call Ranchi  🛵🚡000XX00000 💃 Choose Best And Top Girl Service...🚂🚘 Premium Girls Call Ranchi  🛵🚡000XX00000 💃 Choose Best And Top Girl Service...
🚂🚘 Premium Girls Call Ranchi 🛵🚡000XX00000 💃 Choose Best And Top Girl Service...
 
A Step-by-Step Guide to Selecting the Right Automated Software Testing Tools.pdf
A Step-by-Step Guide to Selecting the Right Automated Software Testing Tools.pdfA Step-by-Step Guide to Selecting the Right Automated Software Testing Tools.pdf
A Step-by-Step Guide to Selecting the Right Automated Software Testing Tools.pdf
 
VVIP Girls Call Mumbai 9910780858 Provide Best And Top Girl Service And No1 i...
VVIP Girls Call Mumbai 9910780858 Provide Best And Top Girl Service And No1 i...VVIP Girls Call Mumbai 9910780858 Provide Best And Top Girl Service And No1 i...
VVIP Girls Call Mumbai 9910780858 Provide Best And Top Girl Service And No1 i...
 
Russian Girls Call Mumbai 🛵🚡9833363713 💃 Choose Best And Top Girl Service And...
Russian Girls Call Mumbai 🛵🚡9833363713 💃 Choose Best And Top Girl Service And...Russian Girls Call Mumbai 🛵🚡9833363713 💃 Choose Best And Top Girl Service And...
Russian Girls Call Mumbai 🛵🚡9833363713 💃 Choose Best And Top Girl Service And...
 
Busty Girls Call Mumbai 9930245274 Unlimited Short Providing Girls Service Av...
Busty Girls Call Mumbai 9930245274 Unlimited Short Providing Girls Service Av...Busty Girls Call Mumbai 9930245274 Unlimited Short Providing Girls Service Av...
Busty Girls Call Mumbai 9930245274 Unlimited Short Providing Girls Service Av...
 
Independent Girls call Service Pune 000XX00000 Provide Best And Top Girl Serv...
Independent Girls call Service Pune 000XX00000 Provide Best And Top Girl Serv...Independent Girls call Service Pune 000XX00000 Provide Best And Top Girl Serv...
Independent Girls call Service Pune 000XX00000 Provide Best And Top Girl Serv...
 
Russian Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service ...
Russian Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service ...Russian Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service ...
Russian Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service ...
 
06. Ruby Array & Hash - Ruby Core Teaching
06. Ruby Array & Hash - Ruby Core Teaching06. Ruby Array & Hash - Ruby Core Teaching
06. Ruby Array & Hash - Ruby Core Teaching
 

Featured

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
GetSmarter
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
Alireza Esmikhani
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
Project for Public Spaces & National Center for Biking and Walking
 

Featured (20)

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 

SQL Stored Procedures on Db2 for IBM i

  • 1. SQL Stored Procedures and Application Modernization John Valance Division 1 Systems johnv@div1sys.com © 2017 Division 1 Systems <div1> www.div1sys.com
  • 2. 2 • 30+ years IBM midrange experience (S/38 thru IBM i) • 17+ years of web development experience • Independent consultant since early 2000 • Community Involvement  COMMON Board of Directors  Presenter at IBM i groups nationwide • Founder and CTO of Division 1 Systems  Web / Mobile applications for IBMi  Full SDLC - design, project management, coding, training, consulting  Extended team - Can scale up/down to meet client needs • Relationship with Zend / RogueWave  Teacher, Reseller, Zend Certified Engineer About John Valance
  • 4. 4 •Introduction  Who, What, Why  Application Modernization •Creating Stored Procedures •SQL Procedure Language  Language Syntax and Capabilities •Creating UDFs •IBM i Considerations •Examples from the trenches (time permitting) What We Will Cover
  • 5. 5 • Any program object on IBM i  Known to DB2 via CREATE PROCEDURE statement • 2 types:  SQL (written in SQL/PL)  External (RPG, CL, any language)  We will focus on SQL stored procedures • Can be called from any environment that supports SQL • Can have parameters for input / output • Can return result sets • Can be selected from the database repository  SELECT * FROM QSYS2/SYSPROCS WHERE ROUTINE_SCHEMA = 'MYLIBR' What Are Stored Procedures?
  • 6. 6 •SQL Procedure Language •Allows SQL scripts to be built  Any SQL statements, plus variables, conditions, loops, etc.  Data-centric programming •DB2 SQL/PL is proprietary  but all major DB vendors have proprietary PL •Compiled using an SQL client (ACS recommended) •Generates an ILE/C language program, with embedded SQL calls What is SQL/PL?
  • 8. 8 •ACS = Access Client Solutions  Formerly known as Client Access  Has an SQL client which is perfect for developing stored procedures on IBM i Running sp_Cust from ACS
  • 9. 9 •Stored procedures •User Defined Functions (UDF) •Triggers – before & after (add/change/delete) What Can I Build with SQL/PL?
  • 10. 10 New Redbook – April 2016!! • SQL Procedures, Triggers, and Functions on IBM DB2 for i  http://www.redbooks.ibm.com/abstracts/sg248326.html  Download the PDF!! • Also:  DB2 for i SQL reference https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_72/db2/rbafzprintthis.htm  SQL Programming Guide: https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_73/sqlp/rbafykickoff.htm Redbooks / References
  • 11. 11 • Declarative, standardized programming language  Puts SQL in the driver's seat • Centralize business logic  Data-Centric programming  Business logic close to the DBMS • Simplicity of application code  No embedded SQL  No ORM issues • Impact of change insulation  No level checks  Loose coupling between DB and apps Benefits of SQL Stored Procedures • Leverage SQL enhancements  Features and performance • Security  No SQL injection  Define authority on stored procedures vs. tables/views • Performance  Execution plan / Precompiled • Modernize DB interface  Similar to a View  Renamed fields, Derived fields  Provide simple interface to complex legacy DB structure
  • 12. 12 • IT Managers / Directors  Don't replace – Refactor! • RPG Programmers  Modernize your database skills  You probably know SQL already • Database Administrators  Play an active role in application modernization • Project Managers  Focus on database issues  Delegate programming Whom Is This For? • Web Application Developers  Browser applications  Ajax developers  API / Web Service Developers • Users of Analysis & Reporting Tools  Excel spreadsheets / VBA apps  Crystal Reports and other reporting tools
  • 14. 14 Anatomy of a Stored Procedure
  • 15. 15 Anatomy of a Stored Procedure Create procedure statement JV1
  • 16. Slide 15 JV1 John Valance, 4/7/2017
  • 17. 16 Anatomy of a Stored Procedure Options (many available)
  • 18. 17 Anatomy of a Stored Procedure Procedure Body
  • 19. 18 Anatomy of a Stored Procedure Body is a compound SQL statement enclosed in begin/end block
  • 20. 19 Anatomy of a Stored Procedure Declarations
  • 21. 20 Anatomy of a Stored Procedure Executable statements
  • 22. 21 •Procedural programming (RPG)  Specify how to get the data •Declarative programming (SQL)  Specify what you want from the database  Database will figure out the most efficient way to execute •Always select from PF / Table, not LF or View •Database will create an access plan – stored with object •Create indices (LFs) to improve performance  SQL Performance Center in ACS  Help with DB tuning based on runtime analysis SQL = Declarative Programming
  • 23. 22 Need to use an SQL Client to run the CREATE PROCEDURE • SQL client choices  ACS = Access Client Solutions ( ** best choice ** )  Green screen STRSQL ( awkward ) $$  Eclipse Data Tools plug-in for RDi, Zend Studio, etc. (pretty good)  Other SQL Clients – JDBC, ODBC (ex.: http://www.sql-workbench.net/) • Biggest issue is handling output parameters  ACS or Client Access handles this well  ACS / CA also gives best diagnostic messages • ACS is FREE!!  https://www-03.ibm.com/systems/power/software/i/access/solutions.html Compiling and Running
  • 24. 23 Store your source code in an .sql file • On your PC  With ACS installed, double click to open and run • On IBM i IFS  Use an IDE like Eclipse, RDi, Zend Studio • Includes SQL syntax highlighting • May need to install Data Tools Platform SQL Dev Tools (help menu… Install New Software)  Can open IFS file in ACS • Right click… Open With… System Editor • SRCPF ? (maybe, but not for me)  SEU? (really??)  RUNSQLSTM or STRSQL (hmmm…) Where To Put Source Code?
  • 25. 24 Run SQL Scripts in ACS Connect to Database
  • 26. 25 Run SQL Scripts in ACS Position Cursor on Statement to Run Then… • Use Run Menu, or… • Click Run Icons , or… • Press Ctrl+R
  • 27. 26 Run SQL Scripts in ACS Run the CREATE
  • 28. 27 Run SQL Scripts in ACS Run the CALL Results are displayed
  • 31. 30 Use MS Query, and edit SQL directly: call sp_cust( ) Running from Excel
  • 32. 31 •Add 2 input parameters: State and Country filters  Add to parameter list  Add to WHERE clause to serve as result filters Adding Parameters  Separate multiple parameters with commas  Can all go on one line, but easier to read on separate lines
  • 33. 32 •IN = Input •OUT = Output •INOUT = Input and Output Types of Parameters
  • 34. 33 Run / Test SP with Parameters call jvalance.sp_cust_parm('HI', 'US'); call jvalance.sp_cust_parm('OR', ''); call jvalance.sp_cust_parm('', 'Canada'); May yield unexpected results!! These both return 0 rows
  • 39. 38 • Procedure Overloading  Two or more procedures with same name, but different signatures • Procedure Signature  Name + Number of Parameters (data type irrelevant) • Ex.: these have different signatures – MyProc(char(5), int) – MyProc(int) • these have same signature: – MyProc(char(5)) – MyProc(int) • Can cause a lot of confusion • DROP PROCEDURE explicitly using Navigator… Databases  Drill down to procedures Beware Procedure Signatures!!
  • 41. 40 Trimming Input Search Parms • All the trim() function calls make the code harder to read • Let’s create some variables to hold the trimmed values for reuse
  • 42. 41 •Create variables to hold trimmed() input values Declaring/Using Variables
  • 43. 42 •Create variables to hold trimmed() input values Declaring/Using Variables Declare variables before cursors
  • 44. 43 •Create variables to hold trimmed() input values Declaring/Using Variables Declaration statements Executable statements
  • 45. 44 sp_Get_Cust_Name More Features of SQL/PL Output Parameters No Result Sets SELECT INTO Conditional Logic (IF /THEN/ ELSE)
  • 46. 45 Running sp_Get_Cust_Name Use ?’s as placeholders for output parameters Output parm values are shown in Messages panel
  • 50. 49 • Two types:  User Defined Scalar Functions  User Defined Table Functions • We will focus on scalar functions  Like BIFs or SQL scalar functions • SUBSTRING(‘hello world’, 7) => returns ‘world’ • STRIP(‘ blank on both ends ‘) => returns ‘blank on both ends‘ • UPPER(‘hello’) => returns ‘HELLO’  Can be used in • Expressions – Ex: if upper(in_ShowPricing) = ‘Y’ then… • Select lists as computed columns – Ex: select substring(COMPANY, 1, 5) as COMP_SHORT from SP_CUST User Defined Functions
  • 52. 51 fn_Get_Cust_Name(in_cust_id) Declare a variable to hold the return value Set the return variable value Use return statement to return the value CREATE FUNCTION Input parameters only! (Don’t specify IN) Declare returned data type
  • 53. 52 •You can test your UDFs using the SYSDUMMY1 table  IBM-supplied, single record table, for testing function calls  In library SYSIBM Testing fn_Get_Cust_Name(in_cust_id) select 1384 as cust_id, fn_Get_Cust_Name(1384) as cust_name from sysibm.sysdummy1
  • 54. 53 Format current date as dec(8,0) in YYYYMMDD format Another Example - fn_CurrDate8()
  • 56. 55 •Loop infinitely •Requires a conditional LEAVE or RETURN statement to exit the loop •Like RPG DO The LOOP loop
  • 57. 56 •Loop until condition is false •Condition tested at beginning of loop  Like RPG DOW  Set condition before loop, or it may never enter loop body •Can also use LEAVE or RETURN statements to exit the loop The WHILE loop
  • 58. 57 •Loop until condition is true •Test condition at end of loop  Like RPG DOU  Always iterate at least once •Can also use LEAVE or RETURN statements to exit the loop The REPEAT UNTIL loop
  • 62. 61 Nomenclature RPG / Native IBMi SQL / RDBMS Library Schema File Table Record Row Field Column Naming Convention System SQL library/file schema.table ** Can use library lists ** Cannot use library lists
  • 63. 62 • To use library lists, connect to DB with “System Naming”  vs. “SQL Naming”, which only allows one library (aka schema)  In CA/ACS: menu Connection… JDBC settings… Format tab  In JDBC-based connections, set property: naming = system • Run “SET PATH *LIBL ;” in ACS SQL client, before creating procedures  Path is stored in DB2 repository with the procedure object • Do NOT hard-code library names in your stored procedure source code (use un-qualified object names) • Connect to DB with a USRPRF that has the proper library list  via the USRPRF’s JOBD libl  When compiling and running Library List Considerations
  • 64. 63 •Library view (green-screen) Viewing SQL Stored Procedure objects Use SPECIFIC option to set short object name Automatically generated pgm object names First 5 + seq number create procedure sp_build_jvcust() specific BLDJVCUST