Teach Yourself PL/SQL in 21 Days- P1

50 367 0
Tài liệu đã được kiểm tra trùng lặp
Teach Yourself PL/SQL in 21 Days- P1

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

A Division of Macmillan USA 800 Easty 96th St.,Indianapolis,Indiana,46240 USA PL/SQLin 21 Days Teach Yourself SECOND EDITION 00 7982 fm 11/30/99 1:24 PM Page i Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Sams Teach Yourself PL/SQL in 21 Days, Second Edition Copyright ©2000 by Sams Publishing All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photo- copying, recording, or otherwise, without written permission from the publish- er. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Neither is any liability assumed for damages resulting from the use of the information contained herein. International Standard Book Number: 0-672-31798-2 Library of Congress Catalog Card Number: 99-65434 Printed in the United States of America First Printing: December 1999 06 05 04 03 7 6 5 4 Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark. PL/SQL is a trademark of Oracle Corporation. Warning and Disclaimer Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied. The information provided is on an “as is” basis. The authors and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book. A SSOCIATE P UBLISHER Bradley L. Jones A CQUISITIONS E DITOR Chris Webb D EVELOPMENT E DITOR Steve Rowe M ANAGING E DITOR Lisa Wilson P ROJECT E DITOR Heather Talbot C OPY E DITOR Kitty Jarrett I NDEXER Chris Wilcox P ROOFREADER Megan Wade T ECHNICAL E DITOR Jeff Shockley T EAM C OORDINATOR Meggo Barthlow M EDIA D EVELOPER Dave Carson I NTERIOR D ESIGN Gary Adair C OVER D ESIGN Aren Howell C OPY W RITER Eric Borgert P RODUCTION Cyndi Davis-Hubler 00 7982 fm 2/25/03 10:48 AM Page ii Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Contents at a Glance Introduction 1 W EEK 1 At a Glance 5 Day 1 Learning the Basics of PL/SQL 7 2Writing Declarations and Blocks 33 3Writing PL/SQL Expressions 63 4Using Functions, IF Statements, and Loops 99 5Implementing Loops and GOTO s 127 6Using Oracle’s Built-In Functions 155 7Procedures, Packages, Errors, and Exceptions 189 W EEK 1 In Review 217 W EEK 2 At a Glance 219 8Using SQL 221 9 Manipulating Data with Cursors 245 10 Collections 265 11 Writing Database Triggers 293 12 Using Oracle8i Objects for Object-Oriented Programming 333 13 Debugging Your Code and Preventing Errors 371 14 Leveraging Large Object Types 401 W EEK 2 In Review 433 W EEK 3 At a Glance 435 15 Managing Transactions and Locks 437 16 Generating Dynamic SQL 451 17 Writing to Files and the Display 483 18 Managing Database Jobs 509 19 Alerting and Communicating with Other Procedures: The DBMS_ALERT and DBMS_PIPE Packages 531 20 PL/SQL and Java 559 21 Advanced Queuing 585 00 7982 fm 11/30/99 1:24 PM Page iii Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. W EEK 3 In Review 613 A Answers 615 Index 659 00 7982 fm 11/30/99 1:24 PM Page iv Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Contents Introduction 1 W EEK 1 At a Glance 5 D AY 1 Learning the Basics of PL/SQL 7 What Is PL/SQL? 8 Why Learn PL/SQL? 9 SQL, SQL*Plus, PL/SQL: What’s the Difference? 9 SQL 9 PL/SQL 10 SQL*Plus 10 What You Need to Finish This Book 12 Getting Started with PL/SQL 13 PL/SQL Is Block Structured 13 Compiling and Executing a Simple Block 15 What About Some Output? 17 Alternatives to Retyping 18 Writing Your First Function 20 Finding Compilation Errors 21 Displaying the Function’s Return Value 23 Can Even This Simple Function Be Useful? 23 Executing PL/SQL Using Developer 2000’s Procedure Builder 24 Starting Procedure Builder 24 Creating the SS_THRESH Function 26 Connecting to a Database 27 Using SQLPlus Worksheet 28 Executing a PL/SQL Block Using SQLPlus Worksheet 28 Summary 29 Q&A 30 Workshop 30 Quiz 31 Exercises 31 D AY 2 Writing Declarations and Blocks 33 Exploring Datatypes 34 VARCHAR2 35 CHAR 36 NUMBER 38 BINARY_INTEGER 41 00 7982 fm 11/30/99 1:24 PM Page v Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. PLS_INTEGER 43 DATE 44 BOOLEAN 45 LONG 46 RAW 46 LONG RAW 47 ROWID 47 UROWID 48 MSLABEL 48 Using Block Structure 49 Anonymous Blocks 49 Function and Procedure Blocks 51 Trigger Blocks 53 Nested Blocks 54 Nesting Functions and Procedures 55 Scope Rules 58 Summary 59 Q&A 60 Workshop 60 Quiz 60 Exercises 61 D AY 3 Writing PL/SQL Expressions 63 Understanding Operators, Expressions, and Conversions 64 Building Expressions with PL/SQL Operators 64 Arithmetic Operators 65 Comparison Operators 68 Logical Operators 76 String Operators 78 Using Comparison Operators with Strings 79 The Effect of Character Set on String Comparisons 79 The Datatype’s Effect on String Comparisons 79 The Effect of Case on String Comparisons 81 Use of Comparison Operators with Dates 82 Exploring Expressions 83 Expressions Defined 83 Operator Precedence 84 Types of Expressions 86 Null Values in Expressions 86 Converting Datatypes 92 Implicit Conversion 92 Explicit Conversion 94 vi Sams Teach Yourself PL/SQL in 21 Days, Second Edition 00 7982 fm 11/30/99 1:24 PM Page vi Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Summary 95 Q&A 95 Workshop 97 Quiz 97 Exercise 97 D AY 4 Using Functions, IF Statements, and Loops 99 Exploring PL/SQL Functions 100 Defining Parameters: Formal and Actual 102 Return Types 105 Function Violations 105 Exception Handling 106 Creating a Stored Function 106 Finding Errors 108 The NULL Statement 110 Why Use the NULL Statement? 110 How to Call a Function from PL/SQL with or Without Parameters 110 Invoker and Definer Rights 111 Using PL/SQL Statements 111 The IF Statement 112 The IF .THEN .ELSE Statement 113 Nested IF Statements 114 Using IF .ELSIF 117 Nested IF Versus ELSIF 118 Formatting IF Statements 118 Avoiding Common Mistakes When Using IF 119 Looping Statements 120 Reversing the Loop 122 Incrementing Through a Loop Differently 122 Summary 123 Q&A 124 Workshop 125 Quiz 125 Exercises 125 D AY 5 Implementing Loops and GOTOs 127 Exploring Labels and the GOTO Statement 128 Statement Labels 128 The GOTO Statement 128 Scoping Rules for the GOTO Statement 129 An Example of the GOTO Statement in Action 133 Why Use the GOTO Statement? 133 Contents vii 00 7982 fm 11/30/99 1:24 PM Page vii Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. WHILE Loops 134 Examples of WHILE Loops 134 The EXIT and EXIT WHEN Statements 138 Examples Using EXIT and EXIT WHEN 139 Using Labels and EXIT Statements with Loops 142 Simple LOOP s 143 Sample Simple LOOP s 144 Creating a REPEAT .UNTIL Loop 144 An Example of a Simulated REPEAT .UNTIL Loop 145 What Loop Should I Use? 146 Loop Guidelines 146 Style Tips for Loops 148 Recursion 149 Guidelines 149 Practicing Recursion 150 Why Use Recursion? 152 Summary 152 Q&A 153 Workshop 153 Quiz 153 Exercises 154 D AY 6 Using Oracle’s Built-In Functions 155 Comparing SQL Functions and PL/SQL Functions 156 The Major Categories of Functions 156 Experimenting with Functions 161 Using Conversion Functions 161 Using TO_DATE 161 Using TO_CHAR for Dates 166 Using TO_NUMBER 168 Using TO_CHAR for Numbers 174 DATE Functions 175 SYSDATE 175 The TRUNC Function 176 The ADD_MONTHS Function 178 The NEXT_DAY Function 179 The LAST_DAY Function 181 The MONTHS_BETWEEN Function 182 The NEW_TIME Function 183 The ROUND Function 184 The TRIM Function 185 viii Sams Teach Yourself PL/SQL in 21 Days, Second Edition 00 7982 fm 11/30/99 1:24 PM Page viii Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Summary 185 Q&A 186 Workshop 186 Quiz 186 Exercises 187 D AY 7 Procedures, Packages, Errors, and Exceptions 189 Using Procedures 190 Why Use Procedures? 190 Procedures Versus Functions 190 Creating Procedures 190 The RETURN Statement 192 Procedure Dependencies 192 Recompiling Stored Procedures 193 Re-creating and Modifying Procedures 194 Invoking Stored Procedures 194 Using Parameters 195 Parameter Definitions 195 Listing Stored Procedure Information 195 Dropping a Stored Procedure 197 Overloading Procedures 197 Using Recursive Procedures 197 Procedure Invocation Security 198 Exploring Packages 199 Why Use Packages? 199 The Package Specification 200 The Package Body 201 Creating Packages 202 Calling Package Subprograms 203 Recompiling Packages 204 Private Versus Public Package Objects 205 Package Invocation Security 205 Variables, Cursors, and Constant Persistence 205 Package States 205 Package Dependency 206 Trapping Errors and Exceptions 206 Exception-Handling Structures 207 SQLCODE and SQLERRM 211 Continuing Processing After an Exception 212 Retrying After an Exception 213 Re-raising Exceptions 213 Exception Scope Rules 214 Propagating Exceptions 214 Contents ix 00 7982 fm 11/30/99 1:24 PM Page ix Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Summary 215 Q&A 215 Workshop 215 Quiz 215 Exercises 216 W EEK 1 In Review 217 W EEK 2 At a Glance 219 D AY 8 Using SQL 221 Types of SQL DML Statements 223 Creating Some Oracle Tables 223 Using the INSERT Statement 226 Inserting Some Data 227 Singleton SELECT Statement 229 Some SELECT Command Examples 229 Exploring the UPDATE and DELETE Statements 230 Handling Types of Exceptions 232 Using the LOCK TABLE Statement 233 Transaction Control Statements 234 Using Records 234 Declaring a Record Variable 234 Using the %TYPE Attribute 235 Using Record Variables Based on Database Tables 236 Using Record Variables and SELECT Statements 237 Using Record Variables Based on Cursor Definitions 239 Declarations at the Package Level 240 Pseudocolumns 241 CURRVAL and NEXTVAL Pseudocolumns 241 ROWID Pseudocolumns 242 ROWNUM Pseudocolumns 242 Summary 242 Q&A 243 Workshop 243 Quiz 243 Exercises 243 D AY 9 Manipulating Data with Cursors 245 What Is a Cursor? 245 Explicit Cursors 246 Declaring a Cursor 246 Declaring Cursor Parameters 248 x Sams Teach Yourself PL/SQL in 21 Days, Second Edition 00 7982 fm 11/30/99 1:24 PM Page x Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... 384 Using DBMS_OUTPUT as a Debugging Tool 385 Writing a DEBUG Package 385 xiv Sams Teach Yourself PL/SQL in 21 Days, Second Edition Preventing Errors and Planning for Debugging in the Future 391 Defining Requirements and Planning Projects 391 Using a Modular Approach to Coding 392 Commenting Code 392 Writing Assertions into Code 394 Formatting Code... seemed to be causing some confusion We believe that we’ve succeeded on both counts The new PL/SQL features introduced in Oracle8i, which you will read about in this book, include the following: • Use of invoker’s rights when executing stored code • PL/SQL bulk binds • Embedded dynamic SQL within PL/SQL • Parameter passing by reference • Autonomous PL/SQL blocks • Calling Java from PL/SQL, and vice... SQL*Plus generated line numbers and the listing line numbers, the listings have been edited to show only one set of line numbers For example, when typing in a simple PL/SQL block, SQL*Plus would generate line numbers as shown here: SQL>BEGIN 2 NULL; 3 END; 4 / In the book, a listing such as the above will usually be shown as 1: BEGIN 2: NULL; 3: END; 4: / Introduction In cases where a listing consists of... 296 The Syntax for Defining a Database Trigger 298 Uses for Triggers 300 Maintaining Data Integrity 300 Maintaining History 304 Managing Triggers 307 Listing Triggers 308 Viewing Trigger Code 309 Enabling and Disabling Triggers 310 Nontraditional Triggers 311 The Syntax for Defining a Trigger on a Database... Debugging Your Code and Preventing Errors 371 Locating and Eliminating Syntax Errors 372 Identifying Syntax Errors 372 An Example of Identifying Syntax Errors 373 Preventing Syntax Errors 377 Handling Logic Errors 378 Order of Operations 379 Nonterminating Loops 380 Debugging Approaches for Logic Errors 380 Using Tools to Help in Debugging... Deleting Table Entries 270 PL/SQL Table Methods 271 Using Nested Tables 274 Declaring a Nested Table 275 Adding Entries to a Nested Table 275 Removing Entries from a Nested Table 279 Using Variable-Sized Arrays 281 Declaring and Initializing a Varray 282 Adding and Removing Data from a Varray 283 Taking Advantage of Bulk Binding... Job Queue by Using SUBMIT 512 Using RUN to Execute Jobs Immediately 514 Exploring the Job Environment 515 Viewing Jobs 517 Samples for Viewing Jobs 519 Exploring Job Management 519 Removing a Job 520 Altering a Job 520 Importing and Exporting Jobs 521 Handling Broken Jobs 521 Hands-on Practice in Job Management... Much of his time is spent providing database administration services, as well as server-side PL/SQL support, to clients and internal project teams When he is not working with Oracle, Jonathan can often be found writing about it Since 1997 he has been involved with a number of writing projects He coauthored the first edition of this book, Sams Teach Yourself PL/SQL in 21 Days He was the technical editor... development editor for Sams Teach Yourself Access 2000 in 24 Hours (Sams, 1999) His second book, SQL*Plus, The Definitive Guide, was published in 1999 by O’Reilly & Associates In addition to coauthoring the second edition of this book, Jonathan continues to be involved in various other writing and editing projects Writing has given Jonathan the opportunity to pursue several interests outside his normal... PMP, is a principle consultant with a leading international information technology consulting firm Over the past 16 years, he has worked with clients and business partners in Europe, North America, and Asia Tom specializes in Project Management, Oracle technologies, and implementing IT solutions to meet business, manufacturing, and engineering needs He is also the author of Sams Publishing’s Essential . Sams Teach Yourself PL/SQL in 21 Days, Second Edition Preventing Errors and Planning for Debugging in the Future 391 Defining. Glance Introduction 1 W EEK 1 At a Glance 5 Day 1 Learning the Basics of PL/SQL 7 2Writing Declarations and Blocks 33 3Writing PL/SQL Expressions 63 4Using

Ngày đăng: 07/11/2013, 20:15

Tài liệu cùng người dùng

Tài liệu liên quan