Download presentation
Presentation is loading. Please wait.
Published by烘 戴 Modified over 5 years ago
1
Special Registers, Date functions, Case and User Defined Functions!!
2
Agenda Review ISeries installed products
Special Registers – System Values Create Tables Date Functions Case Building our own SQL Functions
3
Review Table vs *FILE Library vs Collection What is a schema?
Constraints?
4
ISeries installed Products
Display Software Resources
5
Build the following Database
Students Student Number Student Name Address Enrolled Date Student Marks Student Number Course Code Final Grade
6
Insert Data
7
Add 1 year, 2 months and 3 days to enrolled date
Select Statement As?
8
How many days has a student been at Seneca?
Select Statement
9
Case Statements Provides a multiconditional test Eg: select orderid,
when totamt <= 100 then ‘Small’ when totamt <= 500 then ‘Medium’ else ‘Large’ end as SalesSize From Sale
10
Case Statements select orderid, case ShipCity
when ‘Toronto’ then ‘Ontario’ when ‘Montreal’ then ‘Quebec’ else ‘Invalid’ end as CustProv From Sale
11
Write a select statement that produces a student marks report and lists the letter grade
12
Build your own SQL function
First line: Create Function fname Where fname is the name for the function. Second set of lines is used to define input parameters Eg: (Str char(10), StrLen int) defines the parameter Str as a 10 characters and defines the parameter StrLen as an integer
13
UDF cont’d Third line declares the one variable returned
eg: RETURNS CHAR(10) Means return a 10 character value Fourth Line declares the language of the function: Language SQL
14
UDF Cont’d Fifth line: Returns null on null input
Means that if there are no input values to the function, then NULL will be returned. Sixth line: BEGIN Starts the code section of the function
15
UDF Cont’d Last line END Comments start with --
16
Coding functions DECLARE Used to declare variables SET
used to assign values to variables RETURN used to return the output parameter
17
Coding Functions Most built-in SQL functions can be used including case Case statements must end in end case Each statement must end in an ‘;’
18
Build a function that calculates a Letter Grade based on a Final Mark
19
Coding UDF create function dbpract.gradeStr (grade dec(5,2))
returns char(2) language sql returns null on null input begin -- declare variables declare chargrade char(2); -- calculate char grade case when grade >= 90 then set chargrade = 'A+'; when grade >= 80 then set chargrade = 'A'; else set chargrade = 'F'; end case; return chargrade; end
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.