SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored...
Transcript of SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored...
![Page 1: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/1.jpg)
SQL Stored Proceduresand Application Modernization
John Valance
Division 1 Systems
© 2017 Division 1 Systems
<div1>www.div1sys.com
![Page 2: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/2.jpg)
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
![Page 3: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/3.jpg)
Stored Procedures:What, Why, Who?
![Page 4: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/4.jpg)
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
![Page 5: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/5.jpg)
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?
![Page 6: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/6.jpg)
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?
![Page 7: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/7.jpg)
7
Simple example - sp_Cust
![Page 8: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/8.jpg)
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
![Page 9: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/9.jpg)
9
•Stored procedures
•User Defined Functions (UDF)
•Triggers – before & after (add/change/delete)
What Can I Build with SQL/PL?
![Page 10: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/10.jpg)
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
![Page 11: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/11.jpg)
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
![Page 12: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/12.jpg)
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
![Page 13: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/13.jpg)
Building Procedureswith SQL/PL
![Page 14: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/14.jpg)
14
Anatomy of a Stored Procedure
![Page 15: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/15.jpg)
15
Anatomy of a Stored Procedure
Create procedure statement
JV1
![Page 16: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/16.jpg)
Slide 15
JV1 John Valance, 4/7/2017
![Page 17: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/17.jpg)
16
Anatomy of a Stored Procedure
Options(many available)
![Page 18: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/18.jpg)
17
Anatomy of a Stored Procedure
Procedure Body
![Page 19: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/19.jpg)
18
Anatomy of a Stored Procedure
Body is a compound SQL statement enclosed in
begin/end block
![Page 20: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/20.jpg)
19
Anatomy of a Stored Procedure
Declarations
![Page 21: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/21.jpg)
20
Anatomy of a Stored Procedure
Executable statements
![Page 22: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/22.jpg)
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
![Page 23: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/23.jpg)
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
![Page 24: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/24.jpg)
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?
![Page 25: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/25.jpg)
24
Run SQL Scripts in ACS
Connect to Database
![Page 26: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/26.jpg)
25
Run SQL Scripts in ACS
Position Cursor on Statement to RunThen…• Use Run Menu, or…• Click Run Icons , or…• Press Ctrl+R
![Page 27: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/27.jpg)
26
Run SQL Scripts in ACS
Run the CREATE
![Page 28: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/28.jpg)
27
Run SQL Scripts in ACS
Run the CALL
Results are displayed
![Page 29: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/29.jpg)
28
Running from PHP
![Page 30: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/30.jpg)
29
Running from PHP
![Page 31: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/31.jpg)
30
Use MS Query, and edit SQL directly: call sp_cust( )
Running from Excel
![Page 32: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/32.jpg)
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
![Page 33: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/33.jpg)
32
• IN = Input
•OUT = Output
• INOUT = Input and Output
Types of Parameters
![Page 34: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/34.jpg)
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
![Page 35: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/35.jpg)
34
Ignoring Blank Filter Parameters
![Page 36: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/36.jpg)
35
Ignoring Blank Filter Parameters
![Page 37: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/37.jpg)
36
Ignoring Blank Filter Parameters
![Page 38: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/38.jpg)
37
Ignoring Blank Filter Parameters
![Page 39: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/39.jpg)
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!!
![Page 40: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/40.jpg)
SQL/PL Basics
![Page 41: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/41.jpg)
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
![Page 42: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/42.jpg)
41
•Create variables to hold trimmed() input values
Declaring/Using Variables
![Page 43: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/43.jpg)
42
•Create variables to hold trimmed() input values
Declaring/Using Variables
Declare variables before cursors
![Page 44: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/44.jpg)
43
•Create variables to hold trimmed() input values
Declaring/Using Variables
Declaration statements
Executablestatements
![Page 45: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/45.jpg)
44
sp_Get_Cust_NameMore Features of SQL/PL
Output Parameters
No Result Sets
SELECT INTO
Conditional Logic (IF /THEN/ ELSE)
![Page 46: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/46.jpg)
45
Running sp_Get_Cust_Name
Use ?’s as placeholders for
output parameters
Output parmvalues are shown in
Messages panel
![Page 47: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/47.jpg)
46
Running sp_Get_Cust_Name
![Page 48: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/48.jpg)
47
Running sp_Get_Cust_Name - ERROR
![Page 49: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/49.jpg)
User Defined Functions(UDFs)
![Page 50: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/50.jpg)
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
![Page 51: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/51.jpg)
50
fn_Get_Cust_Name(in_cust_id)
![Page 52: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/52.jpg)
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
![Page 53: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/53.jpg)
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
![Page 54: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/54.jpg)
53
Format current date as dec(8,0) in YYYYMMDD format
Another Example - fn_CurrDate8()
![Page 55: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/55.jpg)
Looping
![Page 56: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/56.jpg)
55
•Loop infinitely
•Requires a conditional LEAVE or RETURN statement to exit the loop
•Like RPG DO
The LOOP loop
![Page 57: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/57.jpg)
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
![Page 58: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/58.jpg)
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
![Page 59: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/59.jpg)
58
The FOR loop
![Page 60: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/60.jpg)
59
Running procedure sp_Build_JVCUST
![Page 61: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/61.jpg)
IBM i Considerations
![Page 62: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/62.jpg)
61
Nomenclature
RPG / Native IBMi SQL / RDBMS
Library Schema
File Table
Record Row
Field Column
Naming ConventionSystem SQL
library/file schema.table
** Can use library lists ** Cannot use library lists
![Page 63: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/63.jpg)
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
![Page 64: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/64.jpg)
63
•Library view (green-screen)
Viewing SQL Stored Procedure objects
Use SPECIFIC option to set short object name
Automatically generated pgm object namesFirst 5 + seq number
create procedure sp_build_jvcust() specific BLDJVCUST
![Page 65: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/65.jpg)
Real-world Examples
•Time Permitting
![Page 66: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/66.jpg)
Thanks for Attending!
![Page 67: SQL Stored Procedures and Application … Stored...2018/01/31 · Microsoft PowerPoint - SQL Stored Procedures and Application Modernization 2.pptx Author John Created Date 10/17/2017](https://reader034.fdocuments.net/reader034/viewer/2022042622/5fabb5a3e6acb60c2a0f100c/html5/thumbnails/67.jpg)
66
Contact Information
John Valance
802-355-4024
Division 1 Systems
www.div1sys.com