Manipulating data within PL/SQL Please use speaker notes for additional information!

Slides:



Advertisements
Similar presentations
PL/SQL User Defined Types Record and Table Please use speaker notes for additional information!
Advertisements

Reports Using SQL Script Please check speaker notes for additional information!
Group functions using SQL Additional information in speaker notes!
Relational example using donor, donation and drive tables For additional information see the speaker notes!
PL/SQL - Using IF statements Please use speaker notes for additional information!
More on IF statements Use speaker notes for additional information!
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.
Chapter 5 Data Manipulation and Transaction Control Oracle 10g: SQL
Database Updates Made Easy In WebFocus Using SQL And HTML Painter Sept 2011 Lender Processing Services 1.
Table maintenance revisited (again) Please use speaker notes for additional information!
SQL Use of Functions Character functions Please use speaker notes for additional information!
Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.
Cursors in PL/SQL Includes cursor example and continuation of first cursor example Please use speaker notes for additional information!
Introduction to Tables/Arrays Please use the speaker notes for additional information. Tables/Arrays.
More on variables with Oracle’s SQL*Plus As always, speaker notes will contain additional information!
Totals on the Screen Please use speaker notes for additional information!
Interacting With The Oracle Server. Objectives After completing this lesson, you should be able to do the following: Write a successful SELECT statement.
Relationships and Advanced Query Concepts Using Multiple Tables Please use speaker notes for additional information!
XML and DTD Please you speaker notes for additional information!
Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2014.
SQL Basics. 5/27/2016Chapter 32 of 19 Naming SQL commands are NOT case sensitive SQL commands are NOT case sensitive But user identifier names ARE case.
Examples dealing with cursors and tables Please use speaker notes for additional information!
Stored Procedures Week 9. Test Details Stored Procedures SQL can call code written in iSeries High Level Languages –Called stored procedures SQL has.
More on views Please refer to speaker notes for additional information!
SQL and Conditions Speaker notes will provide additional information!
Explanation of SAMPLEIF (if88in1.cbl or if88in1.html) Please use speaker notes for additional information!
Exceptions in PL/SQL Please use speaker notes for additional information!
1 PL\SQL Dev Templates. 2 TEMPLATE DEFINITION Whenever you create a new program unit, its initial contents are based upon a template which contains pre-defined.
Indexes in Oracle An Introduction Please check speaker notes for additional information!
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Introduction to Oracle - SQL Additional information is available in speaker notes!
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
G. Green 1.  Options include:  Script Files  already covered  APIs  last course topic  Database-Stored Code  our focus 2.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
More on Primary and Foreign Keys Please see speaker notes for additional information!
Managing Database With Oracle Replacement for Ch10 COP 4708.
Oracle PL/SQL Loops Please use speaker notes for additional information!
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
Declaring PL/SQL Variables
Introduction to PL/SQL As usual, use speaker notes for additional information!
PRACTICE OVERVIEW PL/SQL Part Your stored procedure, GET_BUDGET, has a logic problem and must be modified. The script that contains the procedure.
10 1 Chapter 10 - A Transaction Management Database Systems: Design, Implementation, and Management, Rob and Coronel.
IT420: Database Management and Organization Triggers and Stored Procedures 24 February 2006 Adina Crăiniceanu
PHP with MYSQL Please use speaker notes for additional information!
Ch 7. Working with relational data. Transactions Group of statements executed as a group. If all statements execute successfully, changes are committed.
More about maintaining a table Use speaker notes for additional information!
Lab 2 Writing PL/SQL Blocks CISB514 Advanced Database Systems.
A Guide to MySQL 6. 2 Objectives Create a new table from an existing table Change data using the UPDATE command Add new data using the INSERT command.
Introduction to PL/SQL N. Dimililer. About PL/SQL –PL/SQL is an extension to SQL with design features of programming languages. –Data manipulation and.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
Chapter 2 Anonymous Block
SQL and SQL*Plus Interaction
Introduction to Triggers
More on Procedures (Internal/Local procedures)
Handling Exceptions.
Introduction to Procedures
Using SQL*Plus.
Interacting with the Oracle Server
MySQL - Creating donorof database offline
Please see speaker notes for additional information!
Introduction to Functions
Please use speaker notes for additional information!
Handling Exceptions.
Introduction to Views and Reports
Database Management Systems 2
Handling Exceptions.
More and Still More on Procedures and Functions
Updating Databases With Open SQL
SQL NOT NULL Constraint
Updating Databases With Open SQL
Presentation transcript:

Manipulating data within PL/SQL Please use speaker notes for additional information!

SQL> DESC testplsql; Name Null? Type IDNO NUMBER(3) NAME VARCHAR2(20) AMT1 NUMBER(6,2) A_CODE CHAR(2) AMT2 NUMBER(4) Manipulating data SQL> SELECT * FROM testplsql; IDNO NAME AMT1 A_ AMT John Smith MA Ann Wilson RI Susan French MA James Rogers RI 123 These records were inserted as I created the notes (please see the notes for additional information). I am going to go in and run test_insert which adds records to the table using testplsql_seq. Note that the last record in this table had an idno of 114 which was created by the testplsql_seq. I have been playing with testplsql_seq and now when I go in and use it, you will see that there is a gap.

SQL> edit test_insert Manipulating data DECLARE v_idno testplsql.idno%TYPE; BEGIN SELECT testplsql_seq.NEXTVAL INTO v_idno FROM dual; INSERT INTO testplsql VALUES(v_idno, 'Stephen York', 36.87, 'NY', 122); END; / Note that the idno is coming in from the sequence, the rest of the data has been hard coded into the PL/SQL block. test_insert PL/SQL procedure successfully completed. SQL> SELECT * FROM testplsql; IDNO NAME AMT1 A_ AMT John Smith MA Ann Wilson RI Susan French MA James Rogers RI Stephen York NY 122

Manipulating data SET VERIFY OFF ACCEPT in_name PROMPT 'Enter name: '; ACCEPT in_amt1 PROMPT 'Enter amount one: '; ACCEPT in_a_code PROMPT 'Enter the state code: '; ACCEPT in_amt2 PROMPT 'Enter amount two: '; DECLARE v_idno testplsql.idno%TYPE; v_name testplsql.name%TYPE :='&in_name'; v_amt1 testplsql.amt1%TYPE :=&in_amt1; v_a_code testplsql.a_code%TYPE :='&in_a_code'; v_amt2 testplsql.amt2%TYPE :=&in_amt2; BEGIN SELECT testplsql_seq.NEXTVAL INTO v_idno FROM dual; INSERT INTO testplsql VALUES(v_idno, v_name, v_amt1, v_a_code, v_amt2); END; / SET VERIFY ON SQL> edit test_insert1

test_insert1 Enter name: Jennifer Ames Enter amount one: Enter the state code: TX Enter amount two: 456 PL/SQL procedure successfully completed. Manipulating data The ACCEPT statements shown on the previous slide and below put out these prompts and took in the user input data. ACCEPT in_name PROMPT 'Enter name: '; ACCEPT in_amt1 PROMPT 'Enter amount one: '; ACCEPT in_a_code PROMPT 'Enter the state code: '; ACCEPT in_amt2 PROMPT 'Enter amount two: '; SQL> SELECT * FROM testplsql; IDNO NAME AMT1 A_ AMT John Smith MA Ann Wilson RI Susan French MA James Rogers RI Stephen York NY Jennifer Ames TX 456 testplsql_seq.NEXTVAL was used to put in the idno from the testplsql_seq sequence.

SET VERIFY OFF ACCEPT in_idno PROMPT 'Enter the idno of the record to change amt1 in: '; DECLARE v_idno_in testplsql.idno%TYPE :=&in_idno; v_idno testplsql.idno%TYPE; v_amt1 testplsql.amt1%TYPE; v_new_amt1 testplsql.amt1%TYPE; BEGIN SELECT idno, amt1 INTO v_idno, v_amt1 FROM testplsql WHERE idno = v_idno_in; v_new_amt1 := v_amt1 * 1.1; UPDATE testplsql SET amt1 = v_new_amt1 WHERE idno = v_idno_in; END; / SET VERIFY ON SQL> edit test_update1 Manipulating data

SQL> SELECT * FROM testplsql; IDNO NAME AMT1 A_ AMT John Smith MA Ann Wilson RI Susan French MA James Rogers RI Stephen York NY Jennifer Ames TX 456 SQL> edit test_update1 test_update1 Enter the idno of the record to change amt1 in: 131 PL/SQL procedure successfully completed. SQL> SELECT * FROM testplsql; IDNO NAME AMT1 A_ AMT John Smith MA Ann Wilson RI Susan French MA James Rogers RI Stephen York NY Jennifer Ames TX 456 The code in test_update1 selects a row/record, increases amt1 by 10% and then updates the record.

SET VERIFY OFF ACCEPT in_idno PROMPT 'Enter the idno of the record to delete: '; DECLARE v_idno_in testplsql.idno%TYPE :=&in_idno; v_idno testplsql.idno%TYPE; BEGIN COMMIT; DELETE FROM testplsql WHERE idno = v_idno_in; END; / SET VERIFY ON Manipulating data SQL> edit test_delete1 As I did in the notes, I am going to delete a record and then rollback to restore the record. The delete is done using the test_delete1 block of code.

test_delete1 Enter the idno of the record to delete: 131 PL/SQL procedure successfully completed. SQL> SELECT * FROM testplsql; IDNO NAME AMT1 A_ AMT John Smith MA Ann Wilson RI Susan French MA James Rogers RI Jennifer Ames TX 456 SQL> ROLLBACK; Rollback complete. SQL> SELECT * FROM testplsql; IDNO NAME AMT1 A_ AMT John Smith MA Ann Wilson RI Susan French MA James Rogers RI Stephen York NY Jennifer Ames TX 456 Manipulating data The prompt is the result of the ACCEPT in the code. Note that the row/record 131 has been deleted. After the rollback, the record is restored.