Calling Stored Procedures from PL/SQL Version 1.1Contents: Using Stubs to Talk to Server−Side PL/SQL Restrictions on Calling Stored Procedures The Oracle Developer/2000 tools use PL/SQL
Trang 2Table of Contents
A What's on the Companion Disk? 2
A.1 Installing the Guide 2
3
A.2 Using the Guide 3
5
B Calling Stored Procedures from PL/SQL Version 1.1 6
B.1 Using Stubs to Talk to Server−Side PL/SQL 7
9
B.2 Restrictions on Calling Stored Procedures 9
B.2.1 No Server−Side PL/SQL Datatypes 9
B.2.2 No Direct Stored Package Variable References 10
B.2.3 No Direct Remote Procedure Calls 12
B.2.4 No Default Parameter Values 12
14
C Built−In Packages 15
C.1 Using the Built−in Packages 16
18
C.2 DBMS_ALERT 18
C.2.1 The REGISTER procedure 18
C.2.2 The REMOVE procedure 18
C.2.3 The REMOVEALL procedure 18
C.2.4 The SET_DEFAULTS procedure 18
C.2.5 The SIGNAL procedure 18
C.2.6 The WAITANY procedure 19
C.2.7 The WAITONE procedure 19
20
C.3 Oracle AQ, the Advanced Queueing Facility 20
C.3.1 DBMS_AQ (PL/SQL 8 Only) 20
C.3.2 DBMS_AQADM (PL/SQL 8 Only) 21
24
C.4 DBMS_DDL 24
C.4.1 The ALTER_COMPILE procedure 24
C.4.2 The ANALYZE_OBJECT procedure 24
25
C.5 DBMS_ JOB 25
C.5.1 The BROKEN procedure 25
C.5.2 The CHANGE procedure 25
C.5.3 The INTERVAL procedure 25
C.5.4 The ISUBMIT procedure 25
C.5.5 The NEXT_DATE procedure 26
C.5.6 The REMOVE procedure 26
C.5.7 The RUN procedure 26
C.5.8 The SUBMIT procedure 26
C.5.9 The USER_EXPORT procedure 26
C.5.10 The WHAT procedure 26
28
C.6 DBMS_LOB (PL/SQL8 Only) 28
C.6.1 The APPEND procedure 28
C.6.2 The COMPARE function 28
C.6.3 The COPY procedure 29
[Appendix A] What's on the Companion Disk?
i
Trang 3Table of Contents
C.6.4 The ERASE procedure 29
C.6.5 The FILECLOSE procedure 29
C.6.6 The FILECLOSEALL procedure 29
C.6.7 The FILEEXISTS function 29
C.6.8 The FILEGETNAME procedure 29
C.6.9 The FILEISOPEN function 30
C.6.10 The FILEOPEN procedure 30
C.6.11 The GETLENGTH function 30
C.6.12 The INSTR function 30
C.6.13 The READ procedure 30
C.6.14 The SUBSTR function 31
C.6.15 The TRIM procedure 31
C.6.16 The WRITE procedure 31
33
C.7 DBMS_LOCK 33
C.7.1 The ALLOCATE_UNIQUE procedure 33
C.7.2 The CONVERT function 33
C.7.3 The RELEASE function 34
C.7.4 The REQUEST function 34
C.7.5 The SLEEP procedure 34
36
C.8 DBMS_MAIL 36
C.8.1 The SEND procedure 36
37
C.9 DBMS_OUTPUT 37
C.9.1 The DISABLE procedure 37
C.9.2 The ENABLE procedure 37
C.9.3 The GET_LINE procedure 37
C.9.4 The GET_LINES procedure 37
C.9.5 The NEW_LINE procedure 37
C.9.6 The PUT procedure 38
C.9.7 The PUT_LINE procedure 38
39
C.10 DBMS_PIPE 39
C.10.1 The CREATE_PIPE function 39
C.10.2 The NEXT_ITEM_TYPE function 39
C.10.3 The PACK_MESSAGE procedure 40
C.10.4 The PURGE procedure 40
C.10.5 The RECEIVE_MESSAGE function 40
C.10.6 The REMOVE_PIPE function 40
C.10.7 The RESET_BUFFER procedure 40
C.10.8 The SEND_MESSAGE function 41
C.10.9 The UNIQUE_SESSION_NAME function 41
C.10.10 The UNPACK_MESSAGE procedure 41
42
C.11 DBMS_ROWID (PL/SQL8 Only) 42
C.11.1 The ROWID_CREATE function 42
C.11.2 The ROWID_INFO procedure 42
C.11.3 The ROWID_TYPE function 42
C.11.4 The ROWID_OBJECT function 42
C.11.5 The ROWID_RELATIVE_FNO function 43
C.11.6 The ROWID_BLOCK_NUMBER function 43
ii
Trang 4Table of Contents
C.11.7 The ROWID_ROW_NUMBER function 43
C.11.8 The ROWID_TO_ABSOLUTE_FNO function 43
C.11.9 The ROWID_TO_EXTENDED function 43
C.11.10 The ROWID_TO_RESTRICTED function 43
C.11.11 The ROWID_VERIFY function 43
45
C.12 DBMS_SESSION 45
C.12.1 The CLOSE_DATABASE_LINK procedure 45
C.12.2 The IS_ROLE_ENABLED function 45
C.12.3 The RESET_PACKAGE procedure 45
C.12.4 The SET_LABEL procedure 45
C.12.5 The SET_NLS_LABEL procedure 45
C.12.6 The SET_NLS procedure 45
C.12.7 The SET_ROLE procedure 46
C.12.8 The SET_SQL_TRACE procedure 46
C.12.9 The UNIQUE_SESSION_ID function 46
47
C.13 DBMS_SNAPSHOT 47
C.13.1 The DROP_SNAPSHOT procedure 47
C.13.2 The GET_LOG_AGE procedure 47
C.13.3 The PURGE_LOG procedure 47
C.13.4 The REFRESH procedure 47
C.13.5 The REFRESH_ALL procedure 48
C.13.6 The SET_UP procedure 48
C.13.7 The WRAP_UP procedure 48
49
C.14 DBMS_SQL 49
C.14.1 The BIND_ARRAY procedure 49
C.14.2 The BIND_VARIABLE procedure 49
C.14.3 The CLOSE_CURSOR procedure 50
C.14.4 The COLUMN_VALUE procedure 50
C.14.5 The DEFINE_COLUMN procedure 51
C.14.6 The EXECUTE function 51
C.14.7 The EXECUTE_AND_FETCH function 52
C.14.8 The FETCH_ROWS function 52
C.14.9 The IS_OPEN function 52
C.14.10 The LAST_ERROR_POSITION function 52
C.14.11 The LAST_ROW_COUNT function 52
C.14.12 The LAST_ROW_ID function 52
C.14.13 The LAST_SQL_FUNCTION_CODE function 52
C.14.14 The OPEN_CURSOR function 53
C.14.15 The PARSE procedure 53
C.14.16 The VARIABLE_VALUE procedure 53
54
C.15 DBMS_TRANSACTION 54
C.15.1 The ADVISE_COMMIT procedure 54
C.15.2 The ADVISE_NOTHING procedure 54
C.15.3 The ADVISE_ROLLBACK procedure 54
C.15.4 The COMMIT procedure 55
C.15.5 The COMMIT_COMMENT procedure 55
C.15.6 The COMMIT_FORCE procedure 55
C.15.7 The READ_ONLY procedure 55
[Appendix A] What's on the Companion Disk?
iii
Trang 5Table of Contents
C.15.8 The READ_WRITE procedure 55
C.15.9 The ROLLBACK procedure 56
C.15.10 The ROLLBACK_FORCE procedure 56
C.15.11 The ROLLBACK_SAVEPOINT procedure 56
C.15.12 The SAVEPOINT procedure 56
C.15.13 The USE_ROLLBACK_SEGMENT procedure 56
C.15.14 The BEGIN_DISCRETE_TRANSACTION procedure 56
C.15.15 The PURGE_MIXED procedure 57
C.15.16 The PURGE_LOST_DB procedure 57
C.15.17 The LOCAL_TRANSACTION_ID function 57
C.15.18 The STEP_ID function 57
59
C.16 DBMS_UTILITY 59
C.16.1 The ANALYZE_SCHEMA procedure 59
C.16.2 The COMMA_TO_TABLE procedure 59
C.16.3 The COMPILE_SCHEMA procedure 59
C.16.4 The FORMAT_CALL_STACK function 59
C.16.5 The FORMAT_ERROR_STACK function 59
C.16.6 The GET_TIME function 60
C.16.7 The IS_PARALLEL_SERVER function 60
C.16.8 The NAME_RESOLVE procedure 60
C.16.9 The NAME_TOKENIZE procedure 60
C.16.10 The PORT_STRING function 61
C.16.11 The TABLE_TO_COMMA procedure 61
62
C.17 UTL_FILE 62
C.17.1 Setting Up UTL_FILE 62
65
1 Introduction to PL/SQL 66
1.1 What Is PL/SQL? 66
68
1.2 The Concept of Programming in Oracle Applications 68
70
1.3 The Origins of PL/SQL 70
1.3.1 Improved Application Portability with PL/SQL 70
1.3.2 Improved Execution Authority and Transaction Integrity with PL/SQL 71
72
1.4 PL/SQL Versions 72
1.4.1 Working with Multiple Versions of PL/SQL 72
1.4.2 How This Book Handles Different Versions of PL/SQL 73
1.4.3 PL/SQL Version 2.0 73
1.4.4 PL/SQL Release 2.1 80
1.4.5 PL/SQL Release 2.2 82
1.4.6 PL/SQL Release 2.3 83
1.4.7 PL/SQL Version 8.0 84
1.4.8 PL/SQL Release 1.1 86
88
1.5 Advice for Oracle Programmers 88
1.5.1 Take a Creative, Even Radical Approach 88
1.5.2 Get Ready to Establish New Habits 88
1.5.3 Assume that PL/SQL Has What You Need 89
iv
Trang 6Table of Contents
1.5.4 Share Your Ideas 90
91
1.6 A Few of My Favorite (PL/SQL) Things 91
1.6.1 Anchored declarations 91
1.6.2 Built−in functions 91
1.6.3 Built−in packages 91
1.6.4 The cursor FOR loop 92
1.6.5 Scoping with nested blocks 92
1.6.6 Module overloading 92
1.6.7 Local modules 93
1.6.8 Packages 93
95
1.7 Best Practices for PL/SQL Excellence 95
1.7.1 Write as Little Code as Possible 95
1.7.2 Synchronize Program and Data Structures 96
1.7.3 Center All Development Around Packages 97
1.7.4 Standardize Your PL/SQL Development Environment 98
1.7.5 Structured Code and Other Best Practices 98
101
2 PL/SQL Language Fundamentals 102
2.1 The PL/SQL Character Set 102
104
2.2 Identifiers 104
2.2.1 Reserved Words 105
2.2.2 Whitespace and Keywords 106
107
2.3 Literals 107
2.3.1 Embedding Single Quotes Inside a String 107
2.3.2 Numeric Literals 108
2.3.3 Boolean Literals 108
110
2.4 The Semicolon Delimiter 110
111
2.5 Comments 111
2.5.1 Single−Line Comment Syntax 111
2.5.2 Multiline Comment Syntax 111
113
2.6 The PRAGMA Keyword 113
114
2.7 Block Structure 114
2.7.1 Sections of the PL/SQL Block 114
2.7.2 Scope of a Block 115
2.7.3 Nested Blocks 115
117
3 Effective Coding Style 118
3.1 Fundamentals of Effective Layout 118
3.1.1 Revealing Logical Structure with Indentation 119
3.1.2 Using Case to Aid Readability 120
3.1.3 The UPPER−lower Style 120
3.1.4 Formatting Single Statements 121
[Appendix A] What's on the Companion Disk?
v
Trang 7Table of Contents
3 Effective Coding Style
3.1.5 Formatting Your Declarations 122
3.1.6 Formatting Multiline Statements 123
126
3.2 Formatting SQL Statements 126
129
3.3 Formatting Control Structures 129
3.3.1 Formatting IF Statements 129
3.3.2 Formatting Loops 130
3.3.3 Formatting Exception Handlers 131
133
3.4 Formatting PL/SQL Blocks 133
135
3.5 Formatting Packages 135
137
3.6 Using Comments Effectively 137
3.6.1 Comment As You Code 138
3.6.2 Explain the Why −− Not the How −− of Your Program 138
3.6.3 Make Comments Easy to Enter and Maintain 139
3.6.4 Maintain Indentation 140
3.6.5 Comment Declaration Statements 141
143
3.7 Documenting the Entire Package 143
3.7.1 Document the Package Specification 143
3.7.2 Document the Package Body 144
146
4 Variables and Program Data 147
4.1 Identifiers 147
4.1.1 Choose the Right Name 147
4.1.2 Select Readable Names 148
149
4.2 Scalar Datatypes 149
4.2.1 Numeric Datatypes 150
4.2.2 Numeric Subtypes 152
4.2.3 Character Datatypes 153
4.2.4 The Boolean Datatype 160
4.2.5 The Date−Time Datatype 160
4.2.6 NLS Character Datatypes 161
4.2.7 LOB Datatypes 162
4.2.8 Conversion Between Datatypes 166
169
4.3 NULLs in PL/SQL 169
4.3.1 NULL Values in Comparisons 170
4.3.2 Checking for NULL Values 170
4.3.3 Function Results with NULL Arguments 171
173
4.4 Variable Declarations 173
4.4.1 Constrained Declarations 173
4.4.2 Declaration Examples 173
4.4.3 Default Values 174
4.4.4 NOT NULL Clause 175
vi
Trang 8Table of Contents
4.5.1 Benefits of Anchored Declarations 176
4.5.2 Anchoring at Compile Time 176
4.5.3 Nesting Usages of the %TYPE Attribute 177
4.5.4 Anchoring to Variables in Other PL/SQL Blocks 178
4.5.5 Anchoring to NOT NULL Datatypes 179
179
4.5 Anchored Declarations 179
181
4.6 Programmer−Defined Subtypes 181
4.6.1 Declaring Subtypes 181
4.6.2 Examples of Subtype Declarations 182
4.6.3 Emulating Constrained Subtypes 183
185
4.7 Tips for Creating and Using Variables 185
4.7.1 Establish Clear Variable Naming Conventions 185
4.7.2 Name Subtypes to Self−Document Code 187
4.7.3 Avoid Recycling Variables 188
4.7.4 Use Named Constants to Avoid Hardcoding Values 188
4.7.5 Convert Variables into Named Constants 189
4.7.6 Remove Unused Variables from Programs 190
4.7.7 Use %TYPE When a Variable Represents a Column 190
4.7.8 Use %TYPE to Standardize Nondatabase Declarations 191
4.7.9 Use Variables to Hide Complex Logic 192
196
5 Conditional and Sequential Control 197
5.1 Conditional Control Statements 197
5.1.1 The IF−THEN Combination 197
5.1.2 The IF−THEN−ELSE Combination 198
5.1.3 The IF−ELSIF Combination 199
5.1.4 Nested IF Statements 203
205
5.2 Sequential Control Statements 205
5.2.1 The GOTO Statement 205
5.2.2 The NULL Statement 208
211
6 Database Interaction and Cursors 212
6.1 Transaction Management 212
6.1.1 The COMMIT Statement 213
6.1.2 The ROLLBACK Statement 213
6.1.3 The SAVEPOINT Statement 214
6.1.4 The SET TRANSACTION Statement 214
6.1.5 The LOCK TABLE Statement 215
217
6.2 Cursors in PL/SQL 217
6.2.1 Types of Cursors 218
6.2.2 Cursor Operations 218
220
6.3 Implicit and Explicit Cursors 220
6.3.1 Implicit Cursors 220
6.3.2 Drawbacks of Implicit Cursors 220
[Appendix A] What's on the Companion Disk?
vii
Trang 9Table of Contents
6.3.3 Explicit Cursors 222
224
6.4 Declaring Cursors 224
6.4.1 The Cursor Name 224
6.4.2 PL/SQL Variables in a Cursor 225
6.4.3 Identifier Precedence in a Cursor 225
6.4.4 The Cursor RETURN Clause 226
229
6.5 Opening Cursors 229
231
6.6 Fetching from Cursors 231
6.6.1 Matching Column List with INTO Clause 231
6.6.2 Fetching Past the Last Row 233
234
6.7 Column Aliases in Cursors 234
236
6.8 Closing Cursors 236
6.8.1 Maximum Number of Cursors 236
6.8.2 Closing Local Cursors 237
238
6.9 Cursor Attributes 238
6.9.1 The %FOUND Attribute 239
6.9.2 The %NOTFOUND Attribute 240
6.9.3 The %ROWCOUNT Attribute 240
6.9.4 The %ISOPEN Attribute 241
6.9.5 Implicit SQL Cursor Attributes 241
6.9.6 Differences Between Implicit and Explicit Cursor Attributes 241
243
6.10 Cursor Parameters 243
6.10.1 Generalizing Cursors with Parameters 244
6.10.2 Opening Cursors with Parameters 244
6.10.3 Scope of Cursor Parameters 245
6.10.4 Cursor Parameter Modes 245
6.10.5 Default Values for Parameters 245
246
6.11 SELECT FOR UPDATE in Cursors 246
6.11.1 Releasing Locks with COMMIT 247
6.11.2 The WHERE CURRENT OF Clause 248
6.12.1 Features of Cursor Variables 250
6.12.2 Similarities to Static Cursors 250
6.12.3 Declaring REF CURSOR Types and Cursor Variables 251
6.12.4 Opening Cursor Variables 252
6.12.5 Fetching from Cursor Variables 253
6.12.6 Rules for Cursor Variables 254
6.12.7 Passing Cursor Variables as Arguments 255
6.12.8 Cursor Variable Restrictions 257
260
6.12 Cursor Variables 262
264
6.13 Working with Cursors 264
6.13.1 Validating Foreign Key Entry with Cursors 264
6.13.2 Managing a Work Queue with SELECT FOR UPDATE 266
270viii
Trang 10Table of Contents
7 Loops 271
7.1 Loop Basics 271
7.1.1 Examples of Different Loops 271
7.1.2 Structure of PL/SQL Loops 272
274
7.2 The Simple Loop 274
7.2.1 Terminating a Simple Loop: EXIT and EXIT WHEN 275
7.2.2 Emulating a REPEAT UNTIL Loop 276
277
7.3 The Numeric FOR Loop 277
7.3.1 Rules for Numeric FOR Loops 277
7.3.2 Examples of Numeric FOR Loops 278
7.3.3 Handling Nontrivial Increments 279
280
7.4 The Cursor FOR Loop 280
7.4.1 Example of Cursor FOR Loops 280
7.4.2 The Cursor FOR Loop Record 281
7.4.3 When to Use the Cursor FOR Loop 282
284
7.5 The WHILE Loop 284
7.5.1 The Infinite WHILE Loop 285
286
7.6 Managing Loop Execution 286
7.6.1 Loop Labels 286
7.6.2 Loop Scope 288
290
7.7 Tips for PL/SQL Loops 290
7.7.1 Naming Loop Indexes 290
7.7.2 The Proper Way to Say Goodbye 291
7.7.3 Avoiding the Phony Loop 293
7.7.4 PL/SQL Loops Versus SQL Processing 293
8.1 Why Exception Handling? 296
297
8 Exception Handlers 298
300
8.2 The Exception Section 300
302
8.3 Types of Exceptions 302
8.3.1 Named System Exceptions 302
8.3.2 Named Programmer−Defined Exceptions 304
8.3.3 Unnamed System Exceptions 305
8.3.4 Unnamed Programmer−Defined Exceptions 306
308
8.4 Determining Exception−Handling Behavior 308
8.4.1 Scope of an Exception 308
8.4.2 Propagation of an Exception 312
315
8.5 Raising an Exception 315
8.5.1 Who Raises the Exception? 315
8.5.2 Re−Raising an Exception 316
8.5.3 Exceptions Raised in a Declaration 317
[Appendix A] What's on the Companion Disk?
ix
Trang 11Table of Contents
8.5.4 Exceptions Raised in an Exception Handler 317
320
8.6 Handling Exceptions 320
8.6.1 Combining Multiple Exceptions in a Single Handler 320
8.6.2 Unhandled Exceptions 321
8.6.3 Using SQLCODE and SQLERRM in WHEN OTHERS Clause 321
8.6.4 Continuing Past Exceptions 322
325
8.7 Client−Server Error Communication 325
8.7.1 Using RAISE_APPLICATION_ERROR 325
8.7.2 RAISE_APPLICATION_ERROR in a database trigger 325
327
8.8 NO_DATA_FOUND: Multipurpose Exception 327
329
8.9 Exception Handler as IF Statement 329
331
8.10 RAISE Nothing but Exceptions 331
334
9 Records in PL/SQL 335
9.1 Record Basics 335
9.1.1 Different Types of Records 335
9.1.2 Accessing Record−Based Data 336
9.1.3 Benefits of Using Records 336
9.1.4 Guidelines for Using Records 337
9.1.5 Referencing a Record and its Fields 338
9.1.6 Comparing Two Records 338
340
9.2 Table−Based Records 340
9.2.1 Declaring Records with the %ROWTYPE Attribute 340
342
9.3 Cursor−Based Records 342
9.3.1 Choosing Columns for a Cursor Record 342
9.3.2 Setting the Record's Column Names 343
345
9.4 Programmer−Defined Records 345
9.4.1 Declaring Programmer−Defined Record TYPEs 345
9.4.2 Declaring the Record 346
9.4.3 Examples of Programmer−Defined Record Declarations 347
349
9.5 Assigning Values to and from Records 349
9.5.1 Direct Field Assignment 349
9.5.2 SELECT INTO from an Implicit Cursor 350
9.5.3 FETCH INTO from an Explicit Cursor 350
9.5.4 Aggregate Assignment 351
352
9.6 Record Types and Record Compatibility 352
9.6.1 Assignment Restrictions 353
9.6.2 Record Initialization 353
355x
Trang 12Table of Contents
9.7 Nested Records 355
9.7.1 Example of Nested Records 355
9.7.2 Dot Notation with Nested Records 356
9.7.3 Aggregate Assignments of Nested Records 356
9.7.4 Denormalizing Program Data with Nested Records 357
10.1 PL/SQL Tables and Other Collections 360
10.1.1 PL/SQL Tables 361
10.1.2 Nested Tables and VARRAYs 362
362
10 PL/SQL Tables 362
363
10.2 Characteristics of PL/SQL Tables 363
365
10.3 PL/SQL Tables and DML Statements 365
366
10.4 Declaring a PL/SQL Table 366
10.4.1 Defining the Table TYPE 366
10.4.2 Declaring the PL/SQL Table 367
368
10.5 Referencing and Modifying PL/SQL Table Rows 368
10.5.1 Automatic Conversion of Row Number Expressions 368
10.5.2 Referencing an Undefined Row 368
10.5.3 Nonsequential Use of PL/SQL Table 369
10.5.4 Passing PL/SQL Tables as Parameters 370
372
10.6 Filling the Rows of a PL/SQL Table 372
10.6.1 Direct Assignment 372
10.6.2 Iterative Assignment 372
10.6.3 Aggregate Assignment 373
374
10.7 Clearing the PL/SQL Table 374
376
10.8 PL/SQL Table Enhancements in PL/SQL Release 2.3 376
10.8.1 PL/SQL Tables of Records 377
10.8.2 PL/SQL Table Built−ins 379
383
10.9 Working with PL/SQL Tables 383
10.9.1 Transferring Database Information to PL/SQL Tables 383
10.9.2 Data−Smart Row Numbers in PL/SQL Tables 384
10.9.3 Displaying a PL/SQL Table 386
10.9.4 Building Traditional Arrays with PL/SQL Tables 391
10.9.5 Optimizing Foreign Key Lookups with PL/SQL Tables 397
404
11 Character Functions 405
11.1 Character Function Descriptions 406
11.1.1 The ASCII function 406
11.1.2 The CHR function 406
11.1.3 The CONCAT function 407
11.1.4 The INITCAP function 408
11.1.5 The INSTR function 409
[Appendix A] What's on the Companion Disk?
xi
Trang 13Table of Contents
11 Character Functions
11.1.6 The LENGTH function 412
11.1.7 The LOWER function 412
11.1.8 The LPAD function 413
11.1.9 The LTRIM function 414
11.1.10 The REPLACE function 415
11.1.11 The RPAD function 418
11.1.12 The RTRIM function 419
11.1.13 The SOUNDEX function 420
11.1.14 The SUBSTR function 420
11.1.15 The TRANSLATE function 424
11.1.16 The UPPER function 425
426
11.2 Character Function Examples 426
11.2.1 Parsing a Name 426
11.2.2 Implementing Word Wrap for Long Text 431
11.2.3 Filling Text to Fit a Line 434
11.2.4 Counting Substring Occurrences in Strings 436
11.2.5 Verifying String Formats with TRANSLATE 438
441
12 Date Functions 442
12.1 Date Function Descriptions 443
12.1.1 The ADD_MONTHS function 443
12.1.2 The LAST_DAY function 444
12.1.3 The MONTHS_BETWEEN function 445
12.1.4 The NEW_TIME function 446
12.1.5 The NEXT_DAY function 447
12.1.6 The ROUND function 448
12.1.7 The SYSDATE function 450
12.1.8 The TRUNC function 450
453
12.2 Date Function Examples 453
12.2.1 Customizing the Behavior of ADD_MONTHS 453
12.2.2 Using NEW_TIME in Client−Server Environments 454
459
13 Numeric, LOB, and Miscellaneous Functions 460
13.1 Numeric Function Descriptions 461
13.1.1 The ABS function 461
13.1.2 The ACOS function 462
13.1.3 The ASIN function 462
13.1.4 The ATAN function 462
13.1.5 The ATAN2 function 462
13.1.6 The CEIL function 463
13.1.7 The COS function 464
13.1.8 The COSH function 464
13.1.9 The EXP function 464
13.1.10 The FLOOR function 464
13.1.11 The LN function 465
13.1.12 The LOG function 465
13.1.13 The MOD function 465
xii
Trang 14Table of Contents
13 Numeric, LOB, and Miscellaneous Functions
13.1.14 The POWER function 465
13.1.15 The ROUND function 466
13.1.16 The SIGN function 466
13.1.17 The SIN function 466
13.1.18 The SINH function 467
13.1.19 The SQRT function 467
13.1.20 The TAN function 467
13.1.21 The TANH function 467
13.1.22 The TRUNC function 467
13.1.23 Rounding and Truncation with PL/SQL 468
469
13.2 LOB Function Descriptions 469
13.2.1 The BFILENAME function 469
13.2.2 The EMPTY_BLOB function 471
13.2.3 The EMPTY_CLOB function 471
472
13.3 Miscellaneous Function Descriptions 472
13.3.1 The DUMP function 472
13.3.2 The GREATEST function 473
13.3.3 The LEAST function 473
13.3.4 The NVL function 473
13.3.5 The SQLCODE function 475
13.3.6 The SQLERRM function 475
13.3.7 The UID function 476
13.3.8 The USER function 476
13.3.9 The USERENV function 477
13.3.10 The VSIZE function 478
479
14 Conversion Functions 480
14.1 Conversion Formats 480
14.1.1 Date Format Models 481
14.1.2 Number Format Models 483
486
14.2 Conversion Function Descriptions 486
14.2.1 The CHARTOROWID function 486
14.2.2 The CONVERT function 486
14.2.3 The HEXTORAW function 486
14.2.4 The RAWTOHEX function 487
14.2.5 The ROWIDTOCHAR function 487
14.2.6 The TO_CHAR function (date conversion) 487
14.2.7 The TO_CHAR function (number conversion) 488
14.2.8 The TO_DATE function 488
14.2.9 The TO_NUMBER function 490
491
14.3 Conversion Function Examples 491
14.3.1 FM: Suppressing Blanks and Zeros 491
14.3.2 FX: Matching Formats Exactly 492
14.3.3 RR: Changing Millenia 493
14.3.4 Using TO_CHAR to Create a Date Range 494
14.3.5 Building a Date Manager 498
[Appendix A] What's on the Companion Disk?
xiii
Trang 15Table of Contents
15.2.1 Sequence of Section Construction 508
15.2.2 PL/SQL Block Structure Examples 509
15.3.1 The Structure of an Anonymous Block 509
15.3.2 Examples of Anonymous Blocks 512
15.3.3 Anonymous Blocks in the Oracle Tools 512
15.3.4 Nested Blocks 513
15.3.5 Scope and Visibility 513
15.3.6 Block Labels 515
15.4.1 Calling a Procedure 515
15.4.2 Procedure Header 515
15.4.3 Procedure Body 516
15.4.4 The END Label 516
517
15 Procedures and Functions 521
15.1 Modular Code 525
527
15.2 Review of PL/SQL Block Structure 527
528
15.3 The Anonymous PL/SQL Block 528
529
15.4 Procedures 529
530
15.5 Functions 530
15.5.1 Structure of a Function 530
15.5.2 The RETURN Datatype 531
15.5.3 The END Label 532
15.5.4 Calling a Function 532
15.5.5 Function Header 533
15.5.6 Function Body 534
15.5.7 A Tiny Function 534
15.5.8 The RETURN Statement 534
537
15.6 Parameters 537
15.6.1 Defining the Parameters 537
15.6.2 Parameter Modes 538
15.6.3 Actual and Formal Parameters 541
15.6.4 Matching Actual and Formal Parameters in PL/SQL 542
15.6.5 Default Values 544
545
15.7 Local Modules 545
15.7.1 Benefits of Local Modularization 545
15.7.2 Reducing Code Volume 546
15.7.3 Improving Readability 546
15.7.4 BottomưUp Reading 548
15.7.5 Scope of Local Modules 548
15.7.6 Spruce Up Your Code with Local Modules! 548
549
15.8 Module Overloading 549
15.8.1 Overloading in PL/SQL BuiltưIns 549
15.8.2 Benefits of Overloading 550
15.8.3 Where to Overload Modules 550
xiv
Trang 16Table of Contents
15.8.4 Restrictions on Overloading 551
554
15.9 Forward Declarations 554
556
15.10 Go Forth and Modularize! 556
557
16 Packages 558
16.1 The Benefits of Packages 559
16.1.1 Enforced Information Hiding 559
16.1.2 Object−Oriented Design 559
16.1.3 Top−Down Design 559
16.1.4 Object Persistence 559
16.1.5 Performance Improvement 560
561
16.2 Overview of Package Structure 561
16.2.1 The Specification 561
16.2.2 The Body 562
16.2.3 Package Syntax 562
16.2.4 Public and Private Package Elements 563
16.2.5 How to Reference Package Elements 564
16.2.6 Quick Tour of a Package 565
569
16.3 The Package Specification 569
16.3.1 Packages Without Bodies 570
16.3.2 Declaring Package Cursors 573
575
16.4 The Package Body 575
16.4.1 Declare in Specification or Body 575
16.4.2 Synchronize Body with Package 576
578
16.5 Package Data 578
16.5.1 Architecture of Package−Based Data 578
16.5.2 Global Within a Single Oracle Session 579
16.5.3 Global Public Data 579
16.5.4 Global Private Data 580
16.5.5 Providing an Interface to Global Data 581
583
16.6 Package Initialization 583
16.6.1 Drawbacks of Package Initialization 583
16.6.2 Use Initialization Section for Complex Logic 583
16.6.3 Side Effects 584
16.6.4 Load Session Data in Initialization Section 584
586
17 Calling PL/SQL Functions in SQL 587
17.1 Looking at the Problem 587
590
17.2 Syntax for Calling Stored Functions in SQL 590
592
[Appendix A] What's on the Companion Disk?
xv
Trang 17Table of Contents
17.3 Requirements for Stored Functions in SQL 592
594
17.4 Restrictions on PL/SQL Functions in SQL 594
596
17.5 Calling Packaged Functions in SQL 596
17.5.1 The RESTRICT_REFERENCES Pragma 596
17.5.2 Asserting Purity Level with Package Initialization Section 598
600
17.6 Column/Function Name Precedence 600
601
17.7 Realities: Calling PL/SQL Functions in SQL 601
17.7.1 Manual Application of Pragmas 601
17.7.2 Read Consistency Model Complications 602
604
17.8 Examples of Embedded PL/SQL 604
17.8.1 Encapsulating Calculations 604
17.8.2 Combining Scalar and Aggregate Values 605
17.8.3 Replacing Correlated Subqueries 607
17.8.4 Replacing DECODEs with IF Statements 609
17.8.5 GROUP BY Partial Column Values 611
17.8.6 Sequential Processing Against a Column's Value 612
17.8.7 Recursive Processing in a SQL Statement 613
616
18 Object Types 617
18.1 Introduction to Oracle8 Objects 618
18.1.1 Terminology 618
18.1.2 Some Simple Examples 619
18.1.3 Comparison: Oracle8 Objects and Earlier Features 620
18.1.4 Characteristics of Objects 621
18.1.5 Object Programming Themes 623
627
18.2 Oracle Objects Example 627
18.2.1 Defining the Object Type Specification 627
18.2.2 Defining the Object Type Body 627
18.2.3 Adding Complex Data Structures 631
634
18.3 Syntax for Creating Object Types 634
18.3.1 About Object Types 634
18.3.2 CREATE TYPE and DROP TYPE: Creating and Dropping Types 634
18.3.3 CREATE TYPE BODY: Creating a Body 636
18.3.4 Dot Notation 636
18.3.5 SELF: The Implied Parameter 639
18.3.6 Comparing Objects 640
18.3.7 Privileges 643
645
18.4 Manipulating Objects in PL/SQL and SQL 645
18.4.1 The Need to Initialize 645
18.4.2 OID, VALUE, REF, and DEREF 647
655xvi
Trang 18Table of Contents
18.5 Modifying Persistent Objects 655
18.5.1 Approach 1: Permit Full Use of Conventional SQL 656
18.5.2 Approach 2: Define Methods and Permit Limited Use of Conventional SQL 657
18.5.3 Approach 3: Do Everything via Methods 658
18.5.4 Approach 4: Use an Object and a PL/SQL Container Package 664
18.5.5 Implications for Developer/2000 667
668
18.6 Object Housekeeping 668
18.6.1 Data Dictionary 668
18.6.2 SQL*Plus "Describe" Command 669
18.6.3 Schema Evolution 669
672
18.7 Making the Objects Option Work 672
674
19 Nested Tables and VARRAYs 675
19.1 Types of Collections 675
680
19.2 Creating the New Collections 680
19.2.1 Collections "In the Database" 680
19.2.2 Collections in PL/SQL 682
687
19.3 Syntax for Declaring Collection Datatypes 687
689
19.4 Using Collections 689
19.4.1 Initializing Collection Variables 689
19.4.2 Assigning Values to Elements: Index (Subscript) Considerations 693
19.4.3 Adding and Removing Elements 693
19.4.4 Comparing Collections 695
697
19.5 Collection Pseudo−Functions 697
19.5.1 The THE Pseudo−function 697
19.5.2 The CAST Pseudo−function 699
19.5.3 The MULTISET Pseudo−function 700
19.5.4 The TABLE Pseudo−function 702
704
19.6 Collection Built−Ins 704
19.6.1 COUNT 705
19.6.2 DELETE [ ( i [ , j ] ) ] 705
19.6.3 EXISTS(i) 706
19.6.4 EXTEND [ (n [,i] ) ] 706
19.6.5 FIRST, LAST 707
19.6.6 LIMIT 707
19.6.7 PRIOR(i), NEXT(i) 708
19.6.8 TRIM [ (n ) ] 708
710
19.7 Example: PL/SQL−to−Server Integration 710
713
19.8 Collections Housekeeping 713
19.8.1 Privileges 713
19.8.2 Data Dictionary 713
19.8.3 Call by Reference or Call by Value 714
715
[Appendix A] What's on the Companion Disk?
xvii
Trang 19Table of Contents
19.9 Which Collection Type Should I Use? 715
20.1 Example: Using Object Views 716
717
20 Object Views 718
723
20.2 INSTEAD OF Triggers 723
20.2.1 INSTEAD OF Triggers: To Use or Not to Use? 724
727
20.3 Syntax for Object Views 727
20.3.1 CREATE VIEW: Creating an Object View 727
20.3.2 DROP: Dropping Views and Triggers 728
20.3.3 MAKE_REF: Returning a Virtual REF 728
730
20.4 Differences Between Object Views and Object Tables 730
20.4.1 OID Uniqueness 730
20.4.2 Using REFs with Object Views 732
20.4.3 Storage of Virtual REFs 737
20.4.4 REFs to Nonunique OIDs 737
738
20.5 Not All Views with Objects Are Object Views 738
739
20.6 Schema Evolution 739
741
20.7 Object Views Housekeeping 741
20.7.1 Data Dictionary 741
20.7.2 Privileges 742
20.7.3 Forcing Compilation 742
743
20.8 Postscript: Using the BFILE Datatype 743
746
21 External Procedures 747
21.1 Introduction to External Procedures 748
21.1.1 Example: Determining Free Disk Space on Windows NT 748
21.1.2 Architecture 750
21.1.3 Advantages 750
21.1.4 Limitations 751
753
21.2 Steps in Creating an External Procedure 753
21.2.1 Step 1: Set Up the Listener 753
21.2.2 Step 2: Identify or Create the Shared Library 755
21.2.3 Step 3: Issue CREATE LIBRARY Statement 755
21.2.4 Step 4: Create the PL/SQL Body 756
21.2.5 Using the rand External Procedure 757
759
21.3 Syntax for External Procedures 759
21.3.1 CREATE LIBRARY: Creating the External Procedure Library 759
21.3.2 EXTERNAL: Creating the PL/SQL Body 760
21.3.3 DROP: Dropping Libraries 761
762xviii
Trang 20Table of Contents
21.4 Mapping Parameters 762
21.4.1 Datatype Conversion 762
21.4.2 More Syntax: The PARAMETERS Clause 764
21.4.3 Properties 765
21.4.4 Correct Declaration of Properties 767
769
21.5 OCI Service Routines 769
770
21.6 External Procedure Housekeeping 770
21.6.1 Data Dictionary 770
21.6.2 Rules and Warnings About External Procedures 770
773
21.7 Examples 773
21.7.1 Example: Retrieving the Time Zone 773
21.7.2 Example: Sending Email 776
781
22 Code Design Tips 782
22.1 Select Meaningful Module and Parameter Names 782
22.1.1 Make Sure the Module Name Explains the Module 782
22.1.2 Develop Consistent Naming Conventions for Your Formal Parameters 784
22.1.3 Name Packages and Their Elements to Reflect the Packaged Structure 785
787
22.2 Build the Most Functional Functions 787
22.2.1 Avoid Side Effects in Functions 787
22.2.2 Use a Single RETURN Statement for Successful Termination 790
22.2.3 Avoid Exception Handlers for Normal Program Exits 793
22.2.4 Use Assertion Modules to Validate Parameters and Assumptions 794
798
22.3 Take Full Advantage of Local Modularization 798
801
22.4 Be Wary of Modules Without Any Parameters 801
803
22.5 Create Independent Modules 803
22.5.1 Stretch the Possibilities of the Module 804
22.5.2 Keep the Focus of Your Module 804
22.5.3 Use Parameters Liberally 804
22.5.4 Avoid Global Variables and Data Structures 805
807
22.6 Construct Abstract Data Types (ADTs) 807
22.6.1 Build an ADT in Phases 807
22.6.2 Some ADT Guidelines 808
22.6.3 Progress Box as ADT 808
22.6.4 Price Paid for Code Dispersion 810
814
22.7 Tips for Parameter Design 814
22.7.1 Document All Parameters and Their Functions 814
22.7.2 Use Self−Identifying Parameters (Avoid Boolean Values) 815
22.7.3 Assign Values to All OUT and IN OUT Parameters 816
22.7.4 Ensure Case Consistency of Parameters 818
22.7.5 Default Values and Remote Procedure Calls 820
822
[Appendix A] What's on the Companion Disk?
xix
Trang 21Table of Contents
23 Managing Code in the Database 823
23.1 Executing Stored Code 824
23.1.1 Executing Procedures 824
23.1.2 Executing Functions 824
23.1.3 Memory−Based Architecture of PL/SQL Code 825
23.1.4 Key Concepts for Program Execution 826
829
23.2 Transaction Integrity and Execute Authority 829
23.2.1 Execute Authority on Stored Objects 829
23.2.2 Creating Synonyms for Stored Objects 831
832
23.3 Module Validation and Dependency Management 832
23.3.1 Interdependencies of Stored Objects 832
835
23.4 Remote Procedure Calls 835
836
23.5 Managing Stored Objects with SQL*Plus 836
23.5.1 Creating Stored Objects 836
23.5.2 Tips for Storing Code in Files 837
23.5.3 Changing Stored Objects 837
23.5.4 Viewing Compilation Errors in SQL*Plus 839
840
23.6 Using SQL to Examine Stored Objects 840
23.6.1 Displaying Object Dependencies 840
23.6.2 Displaying Information About Stored Objects 841
23.6.3 Analyzing the Size of PL/SQL Code 843
23.6.4 Displaying and Searching Source Code 844
23.6.5 Cross−Referencing Source Code 844
23.6.6 Finding the Code for a Line Number 845
23.6.7 Changing Source Code in the Database 846
848
23.7 Encrypting Stored Code 848
23.7.1 How to Encrypt Code 848
23.7.2 Working with Encrypted Code 849
23.7.3 Impact of Encrypting Code 849
852
24 Debugging PL/SQL 853
24.1 The Wrong Way to Debug 853
24.1.1 Disorganized Debugging 854
24.1.2 Irrational Debugging 854
856
24.2 Debugging Tips and Strategies 856
24.2.1 Gather Data 856
24.2.2 Remain Logical at All Times 857
24.2.3 Analyze Instead of Trying 858
24.2.4 Take Breaks and Ask for Help 859
24.2.5 Change and Test One Area of Code at a Time 859
24.2.6 Document and Back Up Your Efforts 860
24.2.7 Test All Assumptions 861
24.2.8 Leverage Existing Utilities −− Or Build Your Own 862
24.2.9 Build Debugging Messages into Your Packages 863
866xx
Trang 22Table of Contents
25 Tuning PL/SQL Applications 867
25.1 Analyzing Program Performance 867
25.1.1 Use the DBMS_UTILITY.GET_TIME Function 868
871
25.2 Tuning Access to Compiled Code 871
25.2.1 Tune the Size of the Shared Pool of the SGA 87125.2.2 Pin Critical Code into the SGA 87125.2.3 Tune ACCESS$ Table to Reduce First Execution Time of Code 87325.2.4 Creating Packages with Minimal Interdependencies 87425.2.5 Reducing Memory Usage of Package Variables 874
876
25.3 Tuning Access to Your Data 876
25.3.1 Use Package Data to Minimize SQL Access 87625.3.2 Call PL/SQL Functions in SQL to Reduce I/O 87725.3.3 Avoid Client−Side SQL 88025.3.4 Take Advantage of DBMS_SQL Batch Processing 88125.3.5 Avoid Procedural Code When Possible 88125.3.6 Use PL/SQL to Improve Performance of IO−Intensive SQL 88225.3.7 Keep Database Triggers Small 883
886
25.4 Tuning Your Algorithms 886
25.4.1 There Are No Sacred Cows 88625.4.2 Zen and the Art of PL/SQL Tuning 88925.4.3 Rely on Local Variables to Improve Performance 89525.4.4 Use Package Data to Avoid Passing "Bulky" Parameter Values 89825.4.5 Use PLS_INTEGER for All Integer Operations 90025.4.6 Avoid NOT NULL Constraints 90125.4.7 Avoid Type Conversions When Possible 90125.4.8 Use Index−By Tables of Records and Objects 902
903
25.5 Overview of PL/SQL8 Enhancements 903
905
26 Tracing PL/SQL Execution 906
26.1 The PL/SQL Trace Facility 906
26.1.1 Enabling Program Units for Tracing 90626.1.2 Turning On the Trace 90726.1.3 A Sample Tracing Session 908
910
26.2 Tracing for Production Support 910
26.2.1 Features of a Real−Time Support Mechanism 91026.2.2 Starting and Stopping a Support Session 91126.2.3 Filtering Trace Information 912
914
26.3 Free Format Filtering 914
916
26.4 Structured Interface Filtering 916
26.4.1 From Idea to Implementation 916
918
26.5 Quick−and−Dirty Tracing 918
Index 922Table of Contents 922
[Appendix A] What's on the Companion Disk?
xxi
Trang 23Table of Contents
Part I: Programming in PL/SQL 922Part II: PL/SQL Language Elements 922Part III: Built−In Functions 922Part IV: Modular Code 922Part V: New PL/SQL8 Features 922Part VI: Making PL/SQL Programs Work 923Part VII: Appendixes 923
923 924
Part I: Programming in PL/SQL 925 926
Part II: PL/SQL Language Elements 927 928
Part III: Built−In Functions 929 930
Part IV: Modular Code 931 932
Part V: New PL/SQL8 Features 933 934
Part VI: Making PL/SQL Programs Work 935 936
Part VII: Appendixes 937 938
Dedication 939 940
Foreword 941 943
Preface 944
Objectives of This Book 946
947
Structure of This Book 947
About the Second Edition 947About the Contents 947
Trang 24[Appendix A] What's on the Companion Disk?
xxiii
Trang 251
Trang 26A What's on the Companion Disk?
Contents:
Installing the Guide
Using the Guide
The content of the companion Windows disk that accompanies this book has been included on this CD, in the
/prog2/disk/ directory It contains the Oracle PL/SQL Programming Companion Utilities Guide, an online tool
designed to help you easily find additional resources The guide offers point−and−click access to nearly 100files of source code and documentation prepared by the authors The goal of providing this material in
electronic form is to give you a leg up on the development of your own PL/SQL programs Providing material
on disk also helps us keep the size of this book under (some) control
A.1 Installing the Guide
In a Microsoft Windows environment, you begin installation by double−clicking on the setup.exe file to run
the installation program If you are working in a non−Windows environment, please visit the RevealNetPL/SQL Pipeline Archives (http://www.revealnet.com/plsql−pipeline) to obtain a compressed file containingthe utilities on this disk
The installation script will lead you through the necessary steps The first screen you will see is the installscreen shown in Figure A.1
Figure A.1: Installing the companion utilities guide
You can change the default directory in which the files will be placed Once this step is compete and thesoftware has been copied to your drive, an icon will appear in the folder you specified Double−click on theicon to start using the Companion Utilities Guide You will then see the main menu shown in Figure A.2
Figure A.2: The main menu
Copyright (c) 2000 O'Reilly & Associates All rights reserved.
Trang 27A.2 Using the Guide
The three buttons on the main menu take you to the companion information for this book:
About the Utility Guide
A brief description of the contents of this disk
Package Examples
Extended text and examples that explore the construction of complex packages in PL/SQL FigureA.3 shows buttons available to you for exploration if you choose this Once you select an example,you can scroll through text explaining how the package was designed and built You can also choose
to view the package specification and body, and copy either to the clipboard
Figure A.3: The package examples
Source Code Index
The guide gives you point−and−click access to each of the files on the companion disk The files arelisted in chapter order to make it easy for you to move between the book and guide Source codelistings in the book begin with comment lines keyed to the filenames on the disk Figure A.4 shows aportion of the Source Code Index
Figure A.4: The Source Code Index
Procedures from PL/SQL
3
Trang 28Version 1.1
Copyright (c) 2000 O'Reilly & Associates All rights reserved.
[Appendix A] What's on the Companion Disk?
4
Trang 295
Trang 30B Calling Stored Procedures from PL/SQL Version 1.1
Contents:
Using Stubs to Talk to Server−Side PL/SQL
Restrictions on Calling Stored Procedures
The Oracle Developer/2000 tools use PL/SQL Version 1.1, while the PL/SQL versions on the server rangefrom 2.0 to 8.x The PL/SQL version inside the Oracle Developer/2000 tools will be upgraded to PL/SQLRelease 2.3 sometime in 1998 with Release 2 of Oracle Developer/2000 In the meantime, the tools group atOracle Corporation has had to come up with a way to allow applications based on Oracle Developer/2000 tocall stored procedures in the most transparent fashion possible The end result is a mechanism which:
Does not require any special syntax to distinguish between stored and local PL/SQL modules
Achieving this effect, however, imposes several restrictions on the use of stored procedures:
•
You cannot look up a remote subprogram via a synonym until RDBMS Version 7.1.1
Trang 31B.1 Using Stubs to Talk to Server−Side PL/SQL
The mechanism employed by the Oracle Developer/2000 tools to handle stored procedures is called stubgeneration A stub is a PL/SQL procedure or function which has the same header as the actual procedure orfunction A stub for a package contains a stub or specification for each module in the package
When the Oracle Developer/2000 tool encounters an identifier in a PL/SQL code segment, it checks to see if it
is a local PL/SQL variable, then a tool bind variable, table/view, synonym, sequence, and so on through theprecedence order of object name resolution If it is unable to resolve the reference, the PL/SQL compiler calls
a stub generator to see if it can resolve the identifier as a stored function or procedure In that case, a stub isgenerated for syntactical checking, and the compiler continues Because the stub looks the same to the OracleDeveloper/2000 tool as the stored module, the tool can continue to perform syntactical checks using that stub.Stub generation only occurs at compile time
You can see what kind of stub PL/SQL generates in the Oracle Developer/2000 tool by executing the stubgenerator directly from SQL*Plus, as shown below:
VARIABLE not_needed VARCHAR2(2000);
VARIABLE stubtext VARCHAR2(2000);
DELETE FROM SYS.PSTUBTBL;
EXECUTE SYS.PSTUB ('&1', NULL, :not_needed, :stubtext);
PRINT stubtext;
DELETE FROM SYS.PSTUBTBL;
where "&1" is a substitution variable Notice that I delete from the stub table, SYS.PSTUBTBL, before andafter my call to the SYS pstub generator program This is a temporary table and must be cleaned up manually
if you are going to call the PSTUB program yourself
Place this code in a file named showstub.sql and you can then call it as follows to show a module's stub:
SQL> start showstub calc_totals
The following is an example of the output from this showstub program:
SQL> CREATE PROCEDURE calc_totals
SQL> (company_id_in IN NUMBER, type_inout IN OUT VARCHAR2)
procedure calc_totals (COMPANY_ID_IN NUMBER, TYPE_INOUT in out CHAR) is
begin stproc.in('begin calc_totls(:COMPANY_ID_IN, :TYPE_INOUT); end;');
stproc.bind_i(COMPANY_ID_IN); stproc.bind_io(TYPE_INOUT);
stproc.execute;
stproc.retrieve(2, TYPE_INOUT); end;
If the output from showstub is `$$$ s_notv6Compat', you may be trying to use parameters with the %TYPEattribute, which are not allowed in the parameter list of a stored procedure called from Oracle Developer/2000tools If the output is `$$$ s_subp not found', the problem is that the stub generator cannot find the module.This will happen if you have not created a synonym for a module which is owned by another user The stubgenerator cannot search across different users to resolve a named object reference You will have to create a
Trang 32public synonym for this module and then grant EXECUTE authority on that module to PUBLIC.
Stored Procedures
Copyright (c) 2000 O'Reilly & Associates All rights reserved.
[Appendix A] What's on the Companion Disk?
Trang 33from PL/SQL Version 1.1
B.2 Restrictions on Calling Stored Procedures
It's good to know why and how the Oracle Developer/2000 tools make stored procedures available, but therestrictions on use of those objects has the most direct impact on developers Let's examine each restrictionand the steps you can take to work around those restrictions
B.2.1 No Server−Side PL/SQL Datatypes
Parameters in stored procedures, as well as the RETURN datatype of stored functions, can only have one ofthese datatypes if they are to be used by Oracle Forms This rule applies both to standalone and packagemodules Remember: when you work in a Oracle Developer/2000 tool, you are using PL/SQL Version 1.1;any code you write, including calls to stored modules, must conform to Version 1.1 compiler syntax rules.The specification or "public" side of a stored module must look like a Version 1.1 module Behind the
specification −− in other words, the implementation of that module −− can have all the server−side PL/SQLfeatures you can pack into it You just can't let any of that show outside of the body of the module or package.Suppose you define a stored procedure as follows:
FUNCTION get_row RETURN BINARY_INTEGER;
If you try to execute the function inside an Oracle Forms PL/SQL block, the compiler will tell you:
Identifier 'GET_ROW' must be declared
It literally cannot even find a match for the function if the datatype does not conform to PL/SQL Version 1valid datatypes The SYS.PSTUB procedure will have been unable to generate a stub and so the name remainsunresolved
There are two ways to work around this problem:
no other options
In many situations, you won't have the opportunity to change the specification (parameter list) of a storedmodule It will have been written by others, perhaps for another application, and cannot be modified without
9
Trang 34possibly affecting those other applications.
In this case, the second workaround is annoying but thoroughly able to be implemented If your Oracle
Developer/2000 code must call the get_row function, for example, you can create another stored object (let'scall it Oracle Developer/2000_get_row) which does not return a BINARY_INTEGER, but instead returns aNUMBER The specification and body for Oracle Developer/2000_get_row would be:
FUNCTION Oracle Developer 2000_get_row RETURN INTEGER IS
B.2.2 No Direct Stored Package Variable References
This is the most serious drawback of the implementation for accessing stored objects from Oracle
Developer/2000 You simply cannot use the dot notation to reference a package variable, whether it be astring, exception, or cursor Consider the pet maintenance package which we discussed earlier in this book
FUNCTION next_pet_shots (pet_id_in IN NUMBER) RETURN DATE;
PROCEDURE set_schedule (pet_id_in IN NUMBER);
PROCEDURE check_activity (pet_id_in IN NUMBER);
END pet_maint;
You can call the modules using dot notation, as in:
pet_maint.check_activity (1503);
but you cannot make reference to any of the package variables using this same dot notation All of the
statements shown in boldface will fail to compile:
[Appendix A] What's on the Companion Disk?
Trang 35pet_maint.max_pets_in_facility in their code, you can never change how you store that value If you hide itbehind modules, you could decide to store that value in a PL/SQL table or record and not have any impactoutside of the package.
The following example shows a new specification for the pet_maint package In this version the
max_pets_in_facility variable has been moved to the body of the package and is replaced by the get_max andset_max modules
FUNCTION get_max_pets RETURN INTEGER;
PROCEDURE set_max_pets (max_in IN INTEGER);
FUNCTION next_pet_shots (pet_id_in IN NUMBER) RETURN DATE;
PROCEDURE set_schedule (pet_id_in IN NUMBER);
PROCEDURE check_activity (pet_id_in IN NUMBER);
|| Fetch next record from the cursor Also set a status variable
|| to indicate if a record was fetched (corresponds to
|| the %FOUND attribute).
*/
IS
BEGIN
FETCH pet_maint.pet_cur INTO pet_rec_out;
Trang 36user−defined exception number between −20000 and −20999.
B.2.3 No Direct Remote Procedure Calls
This very powerful feature is unavailable from Oracle Developer/2000 Instead, you will have to create asynonym for the remote procedure and then execute the synonym rather than the procedure directly (Don'tforget to grant EXECUTE authority on the synonym!)
Suppose I want to execute the following procedure from an Oracle Forms application:
Grant EXECUTE authority on that synonym:
GRANT EXECUTE ON HQ_new_schedule TO <user_or_role>;
3
Call the synonym in my Oracle Forms trigger or program unit:
HQ_new_schedule;
B.2.4 No Default Parameter Values
PL/SQL Version 2.0 does not allow you to use default parameter values when you are performing a remoteprocedure call (You must leave any arguments which have default values in the specification out of themodule execution.) Even if you do not append an @ symbol on a call to a stored procedure, PL/SQL doesconsider that a remote procedure call because the client−side application is "remote" from the server
[Appendix A] What's on the Companion Disk?
Trang 37Unfortunately, if you do try to call a stored object from a Oracle Developer/2000 component and rely on adefault value, the PL/SQL error does not offer much help It will not ask you to include values for all
parameters It will simply tell you:
Error at line N:
Identifier 'STORED_OBJECT' must be declared
The first time I encountered this error, I panicked Why couldn't Oracle Forms find the stored procedure? Ilogged in through SQL*Plus and could run the module there So I knew it was defined and stored in thedatabase Was it a security issue within Oracle Forms? Did I have to do something special to get Oracle Forms
to realize it was looking for a stored object and not a local program unit? In the end, I discovered that OracleForms could find the object, it just couldn't use it (create a stub for it) because I hadn't passed the full set ofarguments
So when your Oracle Developer/2000 PL/SQL compiler tells you that an identifier "must be declared", makesure you that you included an argument for each parameter in the stored module parameter list You shouldnot consider this too much of a hardship; good programming practice dictates that you never rely on thedefault values anyway
B.1 Using Stubs to Talk to
Server−Side PL/SQL
C Built−In Packages
Copyright (c) 2000 O'Reilly & Associates All rights reserved.
Trang 38Appendix C
14
Trang 39Table C.1: Built−In Packages Stored in the Oracle Database
Registers a process with an alert and then waits for a signal from that alert
(PL/SQL8 only)
(PL/SQL Release 2.1)
shared memory
for conversion between restricted and extended ROWIDs (PL/SQL8 only)
commands and other session−level commands
Trang 40DBMS_SNAPSHOT Provides a programmatic interface through which you can manage snapshots
and purge snapshot logs You might use modules in this package to build scripts
to automate maintenance of snapshots
statements that are not prewritten into your programs They are, instead,constructed at run time as character strings and then passed to the SQL enginefor execution (PL/SQL Release 2.1)
DBMS_ TRANSACTION Provides a programmatic interface to a number of the SQL transaction
statements, such as SET TRANSACTION
FORMAT_CALL_STACK, which returns the current stack of called modules
(PL/SQL Release 2.3)All of the packages in Table C.1 are stored in the database and can be executed both by client− and
server−based PL/SQL programs In addition to these packages, many of the development tools, like OracleForms, offer their own specific package extensions, such as packages to manage OLE2 objects and DDEcommunication.[1]
[1] For more detailed information about these built−in packages, see my book, Oracle
Built−in Packages
C.1 Using the Built−in Packages
In this appendix, I've provided a brief overview of each package, followed by a description and header foreach program in the package These headers are structured as follows:
PROCEDURE pkg.procname (<parameter list>);
FUNCTION pkg.funcname (<parameter list>) RETURN <return datatype>;
where pkg is the name of the package, procname and funcname are the names of the programs, <parameterlist> is the list of parameters (if there are no parameters, then you do not provide parentheses either) and
<return datatype> is the datatype of the value returned by the function
Let's look at an example Suppose that you want to receive a message from a pipe The header for the built−infunction which does this is:
When I want to call a packaged program, I must use dot notation For example to make use of the
RECEIVE_MESSAGE built−in, I would write code like this:
DECLARE
pipe_status INTEGER;
BEGIN
pipe_status DBMS_PIPE.RECEIVE_MESSAGE (mypipe, 10);
[Appendix A] What's on the Companion Disk?