Chapter 8 Embedded SQL.

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
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
Chapter 4B: More Advanced PL/SQL Programming
Introduction to Structured Query Language (SQL)
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
SQL – Part II Yong Choi School of Business CSU, Bakersfield.
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
Fundamentals, Design, and Implementation, 9/e Chapter 7 Using SQL in Applications.
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.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Introduction to Structured Query Language (SQL)
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.
Concepts of Database Management Seventh Edition
A Guide to SQL, Eighth Edition Chapter Two Database Design Fundamentals.
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.
SQL – Part II Yong Choi School of Business CSU, Bakersfield.
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.
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.
1 A Guide to MySQL 2 Database Design Fundamentals.
Concepts of Database Management Seventh Edition
Chapter 6 Database Administration
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
1 A Guide to SQL Chapter 2. 2 Introduction Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation.
Chapter 4 Multiple-Table Queries
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.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Chapter Thirteen Working with Access Databases and LINQ Programming with Microsoft Visual Basic th Edition.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
Oracle10g Developer: PL/SQL Programming1 Objectives SQL queries within PL/SQL Host or bind variables The %TYPE attribute Include queries and control structures.
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
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.
implicit and an explicit cursor
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
A Guide to SQL, Sixth Edition 1 Chapter 5 Updating Data.
A Guide to SQL, Sixth Edition 1 Chapter 7 Reports.
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.
ASP.NET Programming with C# and SQL Server First Edition
Microsoft Visual Basic 2010: Reloaded Fourth Edition
A Guide to SQL, Eighth Edition
A Guide to SQL, Seventh Edition
Database Processing: David M. Kroenke’s Chapter Seven:
Chapter 7 Using SQL in Applications
Chapter 8 Advanced SQL.
Handling Data in PL/SQL Blocks
Presentation transcript:

Chapter 8 Embedded SQL

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

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

Introduction SQL is a non-procedural language Non-procedural language: many tasks can be accomplished using a single, relatively simple command Procedural language: computer must be given the step-by-step process for accomplishing tasks

Introduction PL/SQL procedural language developed by Oracle an extension of SQL This chapter uses PL/SQL to illustrate how to embed SQL commands in another language To embed SQL commands means to include SQL commands directly within programs written in another language

Using Prompt Variables There are many sources of input for SQL commands In this text, input comes from prompt variables Prompt variables: variables for which the user is prompted to enter a value To designate a prompt variable, precede the name with an ampersand (&)

Using Prompt Variables Problem: List the last name of the sales rep whose number is contained in the prompt variable I_REP_NUM. Solution: SQL> SELECT LAST_NAME 2 FROM REP 3 WHERE REP_NUM = '&I_REP_NUM';

PL/SQL Programs SQL commands can be embedded in PL/SQL programs The programs can be created and saved as script files To run the programs, run the script files

Retrieving a Single Row and Column Problem: Write a PL/SQL program to obtain the last name of the sales rep whose number is contained in the prompt variable I_REP_NUM, place it in the variable I_LAST_NAME, and then display the contents of I_LAST_NAME.

Retrieving a Single Row and Column To place the results of a command in a variable, use the INTO clause To see the contents of the line (the output), use: SET SERVEROUTPUT ON

Retrieving a Single Row and Column Solution: SET SERVEROUTPUT ON DECLARE I_REP_NUM CHAR(2); I_LAST_NAME CHAR(15); BEGIN SELECT LAST_NAME INTO I_LAST_NAME FROM REP WHERE REP_NUM = '&I_REP_NUM'; DBMS_OUTPUT.PUT_LINE(I_LAST_NAME); END; /

Using the %Type Attribute %TYPE attribute: ensures that a variable has the same type as a particular column To assign the variable I_REP_NUM the same type as the REP_NUM column in the REP table, use the following:  I_REP_NUM REP.REP_NUM%TYPE

Retrieving a Single Row from a Join An embedded SQL command can be used to join tables Problem: Obtain the name of the customer whose customer number is stored in the prompt variable I_CUSTOMER_NUM, and the last and first names of the sales rep who represents this customer.

Retrieving a Single Row from a Join SET SERVEROUTPUT ON DECLARE I_LAST_NAME REP.LAST_NAME%TYPE; I_FIRST_NAME REP.FIRST_NAME%TYPE; I_CUSTOMER_NUM CUSTOMER.CUSTOMER_NUM%TYPE; I_CUSTOMER_NAME CUSTOMER.CUSTOMER_NAME%TYPE; BEGIN SELECT LAST_NAME, FIRST_NAME, CUSTOMER_NAME INTO I_LAST_NAME, I_FIRST_NAME, I_CUSTOMER_NAME FROM REP, CUSTOMER WHERE REP.REP_NUM = CUSTOMER.REP_NUM AND CUSTOMER_NUM = '&I_CUSTOMER_NUM'; DBMS_OUTPUT.PUT_LINE(I_CUSTOMER_NAME); DBMS_OUTPUT.PUT_LINE(I_LAST_NAME); DBMS_OUTPUT.PUT_LINE(I_FIRST_NAME) END; /

Inserting a Row into a Table To update databases from within PL/SQL programs, appropriate SQL commands are used SQL INSERT: add a row to a table in the database Problem: Add a row to the REP table. Use prompt variables to obtain values for the fields.

Inserting a Row into a Table The procedural code part of the solution program is: BEGIN INSERT INTO REP (REP_NUM, LAST_NAME, FIRST_NAME, STREET, CITY, STATE, ZIP, COMMISSION, RATE) VALUES ('&I_REP_NUM', '&I_LAST_NAME', '&I_FIRST_NAME', '&I_STREET', '&I_CITY', '&I_STATE', '&I_ZIP', &I_COMMISSION, &I_RATE); END;

Changing a Single Row in a Table SQL commands can also be used to update the rows in a database Problem: Change the last name of the sales rep whose number is stored in I_REP_NUM to the value currently stored in I_LAST_NAME.

Changing a Single Row in a Table Solution: SET SERVEROUTPUT ON DECLARE I_REP_NUM REP.REP_NUM%TYPE; I_LAST_NAME REP.LAST_NAME%TYPE;   BEGIN UPDATE REP SET LAST_NAME = '&I_LAST_NAME‘ WHERE REP_NUM = '&I_REP_NUM'; END; /

Deleting Rows from a Table SQL commands can be used to delete rows Problem: Delete the sales rep whose number currently is stored in I_REP_NUM from the REP table.

Deleting Rows from a Table Solution: SET SERVEROUTPUT ON   DECLARE I_REP_NUM REP.REP_NUM%TYPE;   BEGIN DELETE FROM REP WHERE REP_NUM = '&I_REP_NUM';   END; /

Deleting Rows from Multiple Tables SQL commands can be used to delete more than one row from a table Problem: Delete the order whose number is stored in I_ORDER_NUM from the ORDERS table, and then delete each order line for the order whose order number is currently stored in the variable from the ORDER_LINE table.

Deleting Rows from Multiple Tables Solution: DECLARE I_ORDER_NUM ORDERS.ORDER_NUM%TYPE; W_ORDER_NUM CHAR(5); BEGIN I_ORDER_NUM := &W_ORDER_NUM; DELETE FROM ORDER_LINE WHERE ORDER_NUM = I_ORDER_NUM; DELETE FROM ORDERS WHERE ORDER_NUM = I_ORDER_NUM; END; /

Multiple-Row Select A SELECT command that retrieves multiple rows poses a problem since PL/SQL can process only one record at a time This problem can be solved by using a cursor

Using Cursors Cursor: a pointer to a row in the collection of rows retrieved by a SQL command The cursor advances one row at a time to provide sequential, one-record-at-a-time access to the retrieved rows By using a cursor, PL/SQL can process the set of retrieved rows as though they were records in a sequential file

Using Cursors Using a cursor in the procedural portion of the program involves three commands: OPEN: opens the cursor and causes the query to be executed FETCH: advances the cursor to the next row in the set of rows retrieved by the query and places the contents of the row in the indicated variables CLOSE: closes a cursor and deactivates it

Opening a Cursor Prior to opening the cursor, there are no rows available to be fetched The OPEN command to open the CUSTGROUP cursor is: OPEN CUSTGROUP;

Fetching Rows from a Cursor FETCH: fetch (get) the next row from a cursor The FETCH command is written as follows: FETCH CUSTGROUP INTO I_CUSTOMER_NUM, I_CUSTOMER_NAME;

Closing a Cursor The CLOSE command is written as follows: CLOSE CUSTGROUP;

Advantages of Cursors The three main advantages of using cursors: The coding in the program is greatly simplified In a program using embedded SQL, a special component of the DBMS called the optimizer determines the best way to access the data If the database structure changes in such a way that the necessary information is still obtainable using a different query, the only change required in the program is the cursor definition

Updating Cursors Data can be updated through a cursor by using the FOR UPDATE OF clause A WHERE clause can be used to restrict the scope of the update

Error Handling Programs must be able to handle exceptional conditions that can arise when accessing the database For example, a user might enter a value that would cause an error to occur Errors can be handled by using the EXCEPTION command

Using SQL in Microsoft Access Programs In Microsoft Access, programs are written in Visual Basic Visual Basic does not allow the inclusion of SQL commands directly in the code If the SQL command is stored in a string variable, the DoCmd.RunSQL command can be used to run the command

Using SQL in Microsoft Access Programs The procedure in which you place the SQL command can include arguments Arguments are values that provide information to the procedure Example: In a procedure to delete a sales rep, the argument would be the sales rep number

Deleting Rows To delete the sales rep whose number is 20, the command is: DELETE FROM REP WHERE REP_NUM = '20'; Usually, the specific sales rep number to delete is not known in advance It can be passed as an argument to the procedure containing the DELETE command A procedure is entered in the Microsoft Visual Basic window

Microsoft Visual Basic window Deleting Rows Microsoft Visual Basic window

Running the Code Normally, you run a function by: Calling it from another procedure Associating it with some event Functions can also be run directly by using the Immediate window

Running the Code The Immediate window

Running the Code To run a function procedure in the Immediate window: Type: A question mark The name of the procedure A set of parentheses Place the values for any arguments in the parentheses After typing the command, press the Enter key

Updating Rows A procedure can update a table by using an UPDATE command Such a procedure would be similar to the one used to delete a sales rep

Inserting Rows The process for inserting rows is similar The appropriate INSERT command is created in the strSQL variable There will be multiple arguments in the procedure—one for each value to be inserted

Finding Rows A SELECT command that returns several rows poses serious problems for record-at-a-time languages like PL/SQL and Visual Basic For such SELECT commands, the results of a query are handled in the same way as a loop is used to process through the records in a table

Summary To embed a SQL command in a PL/SQL program, type it in the procedural code Variables in PL/SQL programs are declared after the word DECLARE To assign variables the same type as a column, use the %TYPE attribute You can request user input in SQL commands by using prompt variables If a SELECT command is to retrieve more than one row, define a cursor to select one row at a time

Summary Data in the tables on which a cursor is based can be updated with FOR UPDATE OF To see whether an error has occurred, use the EXCEPTION command To use SQL commands in Access, create the command in a string variable To run the command stored in the string variable, use DoCmd.RunSQL To process a collection of rows retrieved by a SELECT in Access, use a recordset

SQL Project Eight Completed Good Luck H. Zamanzadeh