33
PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T - SQL) Database Programming Instructor: Michael Kremer, Ph.D. Technology & Information Management Class 5

Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

  • Upload
    vanngoc

  • View
    249

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

PROCEDURAL DATABASE PROGRAMMING

( PL/SQL AND T-SQL)

Database Programming

Instructor: Michael Kremer, Ph.D.Technology & Information Management

Class 5

Page 2: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

AGENDA

7. Stored Procedures

7.1 Introduction to Stored Procedures

7.2 Basic Stored Procedures

7.3 Parameters

7.4 Overloading Procedures

Page 3: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

Procedural Database Programming (PL/SQL and T-SQL)

7. STORED PROCEDURES

Page 4: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

7.1 INTRODUCTION TO STORED PROCEDURES

Stored procedures are the main modules holding the majority of

database code

A human mind is not a massively parallel computer

We need to break down huge, intimidating projects into smaller,

more manageable components, then further decompose those

components into individual programs

Therefore, you want to write modular code The benefits are:

More reusable: Small programs are more likely to be reused

More manageable: Focus on smaller tasks is easier, therefore, programs

should be small

More readable: Modules have names, and names describe behavior

Code behind a well described programmatic interface becomes self-

documenting

More reliable: Code in smaller program units will have fewer errors, and if

errors occur, they are easier to find

123

Page 5: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

7.1 INTRODUCTION TO STORED PROCEDURES

Module refers to either a function, stored procedure or trigger

Stored procedures(SP) are executable server-side routines

Unlike functions, you can issue DML statements in a SP

Furthermore, stored procedures can be used as a security layer (You control access to objects by granting permissions)

Perform input validation, and allow activities only if they make sense as a whole unit

SP give you the benefit of encapsulation

You can simply change a SP by using the ALTER PROCEDURE statement to modify

As long as the procedure’s interface (name, parameters) does not change, users and applications are not affected

SP also provide many important performance benefits. They are cached on the database server

124

Page 6: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

7.2 BASIC STORED PROCEDURES

Stored Procedures accept parameters from the caller

Parameters are contained in comma-separated list in

parentheses following the procedure name

SP can also return more than one OUT parameter

SQL Server only, RETURN statement can return integer datatype,

if not used, it returns error number (0 means no error)

;number can be used to

group procedures

together

having the same name

125

Page 7: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

7.2 BASIC STORED PROCEDURES

Values of each declared parameter must be supplied when procedure

is called

Parameters can be assigned a value, this serves as a default value

Parameters are local to the procedure, same name can be reused in

another stored procedure

Datatype can be any valid T-SQL datatype

Also user-defined datatypes such as table types, must be set to

ReadOnly

Cursor datatype can only be used as OUTPUT parameter

Varying keyword must be used with Cursor datatype:

It means that the datatype is dynamically constructed at runtime and its content

changes

126

Page 8: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

7.2 BASIC STORED PROCEDURES

If a default value is defined, the procedure can be called without

specifying a value for that parameter

Default value must be a constant/literal or null, cannot be an

expression

OUTPUT designates parameter as a return value:

Text, ntext, and image datatypes cannot be used as output

READONLY specifies that that the parameter cannot be updated or

modified within the body of the procedure

Procedure options:

RECOMPILE: Execution plan is not cached and procedure is compiled at run time

ENCRYPTION: Convert original text (DDL statement) into an obfuscated format

127

Page 9: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

7.2 BASIC STORED PROCEDURES

Oracle syntax is similar

Procedure name can be at most

30 characters long

Parameters can be IN, OUT

and IN OUT. This is different

compared to SQL Server

AUTHID defines whether procedure will be executed with privileges of

the definer(owner) or the current user

Declarations is used to declare local variables to be used in stored

procedure

Syntax for calling stored procedures is similar in both db platforms

when no out/output parameters are involved

Use keyword exec followed by procedure name and arguments when

called from query/worksheet interface

Calling from another SP: SQL Server(exec SP_name), Oracle(SP_name)

128

Page 10: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

7.2 BASIC STORED PROCEDURES

129

Page 11: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

7.2 BASIC STORED PROCEDURES

130

Page 12: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

7.2 BASIC STORED PROCEDURES

131

Page 13: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

7.2 BASIC STORED PROCEDURES

132

Page 14: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

7.3 PARAMETERS

Parameters are used to pass information back and forth between

calling environment and procedure/function

Procedure name and parameters form the procedure’s interface or

contract

It is crucial to carefully design and implement the list of parameters

Once a program is in use, changes to the procedure will not break any

calls from other units unless the parameters are changed!

Important design decisions for parameters:

Number of parameters: Balance between too few (limits reusability) and too many

(maintenance)

Types of parameters: IN, OUT, or IN OUT (Oracle), also datatypes. VARCHAR is the

most flexible, can always convert within procedure

Names of parameters: Important for understanding program unit

Default values: When and when not to use default values

Oracle: unconstrained datatypes, VARCHAR2 and not VARCHAR2(100)

Oracle: IN, OUT, IN OUT SQL Server: IN, IN OUT only

133

Page 15: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

7.3 PARAMETERS

Different types

of parameters

Note: Only IN parameters can have

default values

134

Page 16: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

7.3 PARAMETERS

When using parameters, there are two sides:

Calling environment: Arguments

Receiving environment: Parameters

Names of arguments and parameters do not have to match

IN Mode

Default mode, IN keyword

optional in Oracle, does

not exist in SQL Server (is

always assumed)

IN parameter acts like a

constant, cannot be changed

135

Page 17: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

7.3 PARAMETERS

In SQL Server, only literals or

local variables are allowed as

IN parameters

OUT Mode (Oracle only)

To return values to calling

environment, a function can

only return one value, a

stored procedure many

However, stored procedure

cannot be used in declarative

SQL call

OUT parameter acts like a variable that has not been initialized

OUT parameter value is assigned only when program terminates

successfully

136

Page 18: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

7.3 PARAMETERS

Rules for

OUT

parameters:

IN OUT Mode

Oracle: IN OUT, SQL Server: OUTPUT (IN is assumed)

Rules for

IN OUT

parameters

Calling Syntax:

137

Page 19: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

7.3 PARAMETERS

138

Page 20: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

7.3 PARAMETERS

139

Page 21: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

7.3 PARAMETERS

140

Page 22: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

7.3 PARAMETERS

141

Page 23: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

7.3 PARAMETERS

Parameter Association

Positional notation, matching arguments with parameters by position

Works well when there are few parameters and no defaults

In case of many parameters and some of them are defaults, use

named notation

In the procedure call, you match argument with parameter

Named notation:

Difference is only association operator

142

Page 24: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

7.3 PARAMETERS

Named Notation is self documenting:

Clearly describes which argument is mapped to which parameter

Parameter names should be self-explanatory

Named Notation is more flexible:

List arguments/parameters in any order

Only use parameters that you want/need

143

Page 25: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

7.3 PARAMETERS

144

Page 26: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

7.3 PARAMETERS

145

Page 27: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

7.3 PARAMETERS

146

Page 28: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

7.3 PARAMETERS

147

Page 29: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

7.4 OVERLOADING PROCEDURES

When more than one program in the same scope share the same

name, the programs are said to be overloaded

Oracle supports overloading, SQL Server does not (simulate by using

default values)

Benefits of overloading:

Supporting many data combinations: Same action to different kinds of data,

overloading provides different ways of requesting the same activity (most common

motivation for overloading)

Fitting the program to the user: Construct different versions of the same program

correspond to different patterns of use

Overloading by type, not by value: Use different datatypes for the same kind of

activity of a program.

148

Page 30: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

7.4 OVERLOADING PROCEDURES

Next example we need to calculate the number of years an employee

worked for a startup company. Company was founded on May 1, 1999

Functionality:

The first two functions are designed for employees who worked

for the company since the inception, therefore, they get the

additional benefit of the rounding up to the next higher number

149

Page 31: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

7.4 OVERLOADING PROCEDURES

150

Page 32: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

7.4 OVERLOADING PROCEDURES

151

Page 33: Class 5 Database Programming PROCEDURAL … Database Programming (PL/SQL and T-SQL) 7. ... Oracle syntax is similar. ... Important for understanding program unit

7.4 OVERLOADING PROCEDURES

SQL Server

function seems

simpler

But just think that

these different

functionalities were

developed over time

In the beginning, there was only one function since all employees were

part of the founding team

Over time, more employees were hired having a different start date.

That calls for a new procedure

Rather than changing the existing one, add an overloaded function

In SQL Server, you have to change the existing function More

regression testing is needed to ensure no existing functionality is

broken!

152