A Guide to SQL, Seventh Edition

Slides:



Advertisements
Similar presentations
AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
Advertisements

PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g.
Concepts of Database Management Sixth Edition
Concepts of Database Management Seventh Edition
Chapter 8 Embedded SQL.
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
Chapter 4B: More Advanced PL/SQL Programming
A Guide to SQL, Seventh Edition. Objectives Create a new table from an existing table Change data using the UPDATE command Add new data using the INSERT.
VBA Modules, Functions, Variables, and Constants
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
1 Chapter 4 The Fundamentals of VBA, Macros, and Command Bars.
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
Fundamentals, Design, and Implementation, 9/e Chapter 7 Using SQL in Applications.
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Concepts of Database Management Sixth Edition
A Guide to SQL, Seventh Edition. Objectives Embed SQL commands in PL/SQL programs Retrieve single rows using embedded SQL Update a table using embedded.
Introduction to PL/SQL
A Guide to MySQL 7. 2 Objectives Understand, define, and drop views Recognize the benefits of using views Use a view to update data Grant and revoke users’
Chapter 14: Advanced Topics: DBMS, SQL, and ASP.NET
A Guide to MySQL 3. 2 Objectives Start MySQL and learn how to use the MySQL Reference Manual Create a database Change (activate) a database Create tables.
A Guide to SQL, Seventh Edition. Objectives Understand, create, and drop views Recognize the benefits of using views Grant and revoke user’s database.
Concepts of Database Management Sixth Edition
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Bordoloi and Bock CURSORS. Bordoloi and Bock CURSOR MANIPULATION To process an SQL statement, ORACLE needs to create an area of memory known as the context.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
Oracle10g Developer: PL/SQL Programming1 Objectives Manipulating data with cursors Managing errors with exception handlers Addressing exception-handling.
Concepts of Database Management, Fifth Edition Chapter 4: The Relational Model 3: Advanced Topics.
Chapter 4 The Relational Model 3: Advanced Topics Concepts of Database Management Seventh Edition.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.
Concepts of Database Management Seventh Edition
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
Concepts of Database Management Seventh Edition
A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.
CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 PL/SQL.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to Insert, Update, Delete, and View Data.
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
Dynamic SQL. 2 home back first prev next last What Will I Learn? Recall the stages through which all SQL statements pass Describe the reasons for using.
Oracle10g Developer: PL/SQL Programming1 Objectives SQL queries within PL/SQL Host or bind variables The %TYPE attribute Include queries and control structures.
Introduction to Explicit Cursors. 2 home back first prev next last What Will I Learn? Distinguish between an implicit and an explicit cursor Describe.
INSERT BOOK COVER 1Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Getting Started with VBA for Microsoft Office 2010 by.
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
Chapter 8 Embedded SQL.
Oracle11g: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
A Guide to SQL, Sixth Edition 1 Chapter 5 Updating Data.
SQL Triggers, Functions & Stored Procedures Programming Operations.
Programming with Microsoft Visual Basic 2012 Chapter 14: Access Databases and SQL.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
3 A Guide to MySQL.
Microsoft Visual Basic 2010: Reloaded Fourth Edition
Database Systems: Design, Implementation, and Management Tenth Edition
UNIT - V STORED PROCEDURE.
Using SQL*Plus.
PL/SQL Scripting in Oracle:
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
Database Processing: David M. Kroenke’s Chapter Seven:
A Guide to SQL, Eighth Edition
Chapter 7 Using SQL in Applications
Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Tenth Edition
Handling Data in PL/SQL Blocks
Presentation transcript:

A Guide to SQL, Seventh Edition

Objectives Embed SQL commands in PL/SQL programs Retrieve single rows using embedded SQL Update a table using embedded INSERT, UPDATE, and DELETE commands Use cursors to retrieve multiple rows in embedded SQL A Guide to SQL, Seventh Edition

Objectives Update a database using cursors Manage errors in programs containing embedded SQL commands Use SQL in a language that does not support embedded SQL commands A Guide to SQL, Seventh Edition

Introduction A procedural language requires a step-by-step process to accomplish tasks You can embed, or include, SQL commands in the programs using procedural languages Useful when needed tasks are beyond the capabilities of SQL A Guide to SQL, Seventh Edition

Using Prompt Variables Sources of input for SQL commands Onscreen form Passed as arguments from procedure Prompt variables Prompt variables are when user is prompted to enter a value when the program is run Precede the variable with an “&” A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

PL/SQL Programs Embed SQL commands in PL/SQL programs Create and save the programs as script files Run the script files to run the programs A Guide to SQL, Seventh Edition

Retrieving a Single Row and Column To place the results of a command in a variable, use the INTO clause SELECT LAST_NAME INTO I_LAST_NAME FROM REP WHERE REP_NUM = ‘&I_REP_NUM;’ A Guide to SQL, Seventh Edition

Retrieving a Single Row and Column When executed, user will be prompted for a value for I_REP_NUM That value will be used to retrieve the last name of the sales rep whose number equals this value The results will be placed in the variable I_LAST_NAME This variable can be used in another program A Guide to SQL, Seventh Edition

Retrieving a Single Row and Column Procedure uses one argument and produces a single line of text containing this argument output To see the output, execute the command SET SERVEROUTPUT ON Include this command in the PL/SQL program to ensure it will be executed A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

Retrieving a Single Row and Column Procedural code are commands that specify exactly what program is to do Procedural code located between BEGIN and END commands Each variable declaration and command as well as the word END are followed by semicolons A Guide to SQL, Seventh Edition

Retrieving a Single Row and Column The slash (/) at the end of the program appears on its own line When the script is run, the slash causes commands in the program to be executed immediately With the slash, commands are only loaded into memory Typing the slash runs the program A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

Using the %TYPE Attribute The %TYPE attribute ensures the variable has the same type as a particular column in a table Do not enter a data type  it is automatically assigned from the corresponding column I_REP_NUM REP.REP_NUM%TYPE A Guide to SQL, Seventh Edition

Retrieving a Single Row from a Join Use embedded SQL commands to join tables A Guide to SQL, Seventh Edition

Inserting a Row into a Table When updating a database from the PL/SQL program, use appropriate SQL commands A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

Changing a Single Row in a Table Use the SQL command to insert rows in a database A Guide to SQL, Seventh Edition

Deleting Rows from a Table SQL commands are used to delete rows from a table A Guide to SQL, Seventh Edition

Multiple-Row Select PL/SQL can process only one record at a time A cursor is a pointer to a row in the collection of rows retrieved by a SQL command A cursor advances one row at a time to provide sequential one-record-at-a-time access to retrieved rows A Guide to SQL, Seventh Edition

Using Cursors The first step is to declare the cursor and describe the associated query in the declaration section CURSOR CUSTGROUP IS SELECT CUSTOMER_NUM, CUSTOMER_NAME FROM CUSTOMER WHERE REP_NUM = ‘&I_REP_NUM’; Three commands are needed OPEN, FETCH, CLOSE A Guide to SQL, Seventh Edition

Opening a Cursor OPEN command Opens cursor Causes query to be executed Makes results available to the program Prior to opening, there are no rows available to be fetched OPEN CUSTGROUP A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

Fetching Rows from a Cursor FETCH command Advances cursor to next row in set of retrieved rows Places contents of row in indicated variables FETCH CUSTGROUP I_CUSTOMER_NUM, I_CUSTOMER_NAME; Execution of fetch command produces only a single row A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

Closing a Cursor CLOSE command Closes a cursor Deactivates it Data retrieved by execution of the query is no longer available A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

More Complex Cursors Any SLQ query is legitimate in a cursor definition More complicated retrieval requirements result in greater benefits A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

Advantages of Cursors Simplified coding in the program Progams with embedded SQL utilizes the optimizer Programmer doesn’t worry about the best way to retrieve data Program doesn’t have to change even if the underlying structure does Cursor definition only changes; not procedural code A Guide to SQL, Seventh Edition

Updating Cursors Update the rows encountered in processing cursors with FOR UPDATE OF A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

Error Handling Handle errors with the EXCEPTION command A Guide to SQL, Seventh Edition

Using SQL in Microsoft Access Programs In Access, programs are written in Visual Basic Does not allow inclusion of SQL commands in the code If the SQL command is store in string variable, use the DoCmd.RunSQL command A Guide to SQL, Seventh Edition

Deleting Rows DELETE FROM REP WHERE REP_NUM = ’20’ Place the SQL command in the procedure, including arguments Example DELETE FROM REP WHERE REP_NUM = ’20’ A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

Running the Code Normally run by calling it from another procedure or by associating it with an event Can be run by using the Immediate window Normally for testing A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

Updating Rows Similar to the procedure to delete a sales rep, except Need the UPDATE command Two arguments rather than one Two portions of the construction of the SQL command that involve variables A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

Inserting Rows Process is similar Create the appropriate INSERT command in the strSQL variable Multiple arguments Once for each value inserted A Guide to SQL, Seventh Edition

Finding Rows SELECT commands handled differently than in PL/SQL No cursors Handle results of query just as you would use a loop to process through the records on the table A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

Summary How to embed SQL commands in PL Created programs Retrieved single rows Inserted new rows Changed and deleted existing rows Cursors to update database Error Handling Using SQL command in Access A Guide to SQL, Seventh Edition