1 PL/SQL Part C Scope and Interacting with the Oracle Server.

Slides:



Advertisements
Similar presentations
BACS 485—Database Management Advanced SQL Overview Advanced DDL, DML, and DCL Commands.
Advertisements

Basic SQL Introduction Presented by: Madhuri Bhogadi.
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.
Manipulating Data Schedule: Timing Topic 60 minutes Lecture
Virtual training week 4 structured query language (SQL)
9-1 Copyright  Oracle Corporation, All rights reserved. Data Manipulation Language A DML statement is executed when you: – Add new rows to a table.
Subqueries 11. Objectives After completing this lesson, you should be able to do the following: Describe the types of problems that subqueries can solve.
Copyright  Oracle Corporation, All rights reserved. 9 Manipulating Data: INSERT, UPDATE, DELETE.
PL/SQLPL/SQL Declaring Variables Declaring Variables Declaring Variables Declaring Variables Writing Executable Statements Writing Executable Statements.
Chapter 5 Data Manipulation and Transaction Control Oracle 10g: SQL
1 PL/SQL programming Procedures and Cursors Lecture 1 Akhtar Ali.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Introduction to DBMS and SQL Introduction to DBMS and SQL GUIDED BY : MR. YOGESH SAROJ (PGT-CS) MR. YOGESH SAROJ (PGT-CS) Presented By : JAYA XII –COM.
EE Copyright س Oracle Corporation, All rights reserved. ® Review of PL/SQL.
4-1 Copyright  Oracle Corporation, All rights reserved. Data Manipulation Language (DML)
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
1 JumpStart PL/SQL Wang Hao. 2 Outline PL/SQL Programming SQL*Plus and SQL commands.
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 DB2 9 Fundamentals.
Interacting With The Oracle Server. Objectives After completing this lesson, you should be able to do the following: Write a successful SELECT statement.
Program with PL/SQL Lesson 5. Working with Composite Data Types.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
About PL/SQL –PL/SQL is an extension to SQL with design features of programming languages. –Data manipulation and query statements of SQL are included.
Program with PL/SQL. Interacting with the Oracle Server.
ACTION QUERIES (SQL COMMANDS ) STRUCTURED QUERY LANGUAGE.
1 Information Retrieval and Use (IRU) CE An Introduction To SQL Part 1.
SQL FUNDAMENTALS SQL ( Structured Query Language )
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
1 CursorsCursors. 2 SQL Cursor A cursor is a private SQL work area. A cursor is a private SQL work area. There are two types of cursors: There are two.
Session 2: SQL (A): Parts 1 and 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
PL/SQL Block Structure DECLARE - Optional Variables, cursors, user-defined exceptions BEGIN - Mandatory SQL Statements PL/SQL Statements EXCEPTIONS - Optional.
Manipulating Data in PL/SQL. 2 home back first prev next last What Will I Learn? Construct and execute PL/SQL statements that manipulate data with DML.
PL/SQL Declaring Variables PL/SQL Block Structure DECLARE (Optional) Variables, cursors, user-defined exceptions BEGIN (Mandatory) - SQL statements -
Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ Lecture by.
Introduction to PL/SQL. Objectives  After completing this lesson, you should be able to do the following:  Explain the need for PL/SQL  Explain the.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers.
Copyright  Oracle Corporation, All rights reserved. 18 Interacting with the Oracle Server.
Copyright  Oracle Corporation, All rights reserved. 16 Declaring Variables.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Copyright  Oracle Corporation, All rights reserved. 4 Introduction.
SQL: Part 1 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram. Not for commercial.
Using SQL in PL/SQL ITEC 224 Database Programming.
Manipulating Data. Objectives After completing this lesson, you should be able to do the following: Describe each DML statement Insert rows into a table.
9 Manipulating Data. 9-2 Objectives At the end of this lesson, you should be able to: Describe each DML statement Insert rows into a table Update rows.
1 Handling Exceptions Part F. 2 Handling Exceptions with PL/SQL What is an exception? Identifier in PL/SQL that is raised during execution What is an.
Introduction to Explicit Cursors. 2 home back first prev next last What Will I Learn? Distinguish between an implicit and an explicit cursor Describe.
Retrieving Data in PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Recognize the SQL statements that can.
Introduction to PL/SQL Francis Thottungal. The outline The basic PL/SQL code structure is : DECLARE -- optional, which declares and define variables,
Program with PL/SQL Lesson 3. Interacting with the Oracle Server.
CSCI N311: Oracle Database Programming 5-1 Chapter 15: Changing Data: insert, update, delete Insert Rollback Commit Update Delete Insert Statement –Allows.
PL/SQL Writing Executable Statements. PL/SQL Block Syntax and Guidelines Statement can be split across lines, but keywords must not be split Lexical units.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
PL/SQL Part B.
ITEC 224 Database Programming
CS322: Database Systems PL/ SQL PL/SQL by Ivan Bayross.
Interacting with the Oracle8 Server
Interacting with the Oracle Server
Interacting with the Oracle Server
ATS Application Programming: Java Programming
Subqueries Schedule: Timing Topic 25 minutes Lecture
Interacting with the Oracle Server
Database Management Systems 2
Interacting with the Oracle Server
كتابة الجمل التنفيذية في PL/SQL
مقدمة في قواعد البيانات
Chapter 8 Advanced SQL.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Subqueries Schedule: Timing Topic 25 minutes Lecture
Database Programming Using Oracle 11g
Presentation transcript:

1 PL/SQL Part C Scope and Interacting with the Oracle Server

Nested Blocks and Variable Scope Statements can be nested wherever an executable statement is allowed. Statements can be nested wherever an executable statement is allowed. A nested block becomes a statement. A nested block becomes a statement. An exception section can contain nested blocks. An exception section can contain nested blocks. The scope of an object is the region of the program that can refer to the object. The scope of an object is the region of the program that can refer to the object. Statements can be nested wherever an executable statement is allowed. Statements can be nested wherever an executable statement is allowed. A nested block becomes a statement. A nested block becomes a statement. An exception section can contain nested blocks. An exception section can contain nested blocks. The scope of an object is the region of the program that can refer to the object. The scope of an object is the region of the program that can refer to the object.

3 Nested Blocks and Variable Scope The body and the exception parts in the PL/SQL block could contain nested blocks. The body and the exception parts in the PL/SQL block could contain nested blocks. An identifier is visible in the regions in which you can reference the identifier: An identifier is visible in the regions in which you can reference the identifier: A block can look up to the enclosing block. A block can look up to the enclosing block. A block cannot look down to enclosed blocks. A block cannot look down to enclosed blocks. The body and the exception parts in the PL/SQL block could contain nested blocks. The body and the exception parts in the PL/SQL block could contain nested blocks. An identifier is visible in the regions in which you can reference the identifier: An identifier is visible in the regions in which you can reference the identifier: A block can look up to the enclosing block. A block can look up to the enclosing block. A block cannot look down to enclosed blocks. A block cannot look down to enclosed blocks.

4 Nested Blocks and Variable Scope... x BINARY_INTEGER; BEGIN... DECLARE y NUMBER; BEGIN... END;... END;... x BINARY_INTEGER; BEGIN... DECLARE y NUMBER; BEGIN... END;... END; Scope of x Scope of y Example

5 Nested Blocks Example SQL> DECLARE SQL> DECLARE v_m VARCHAR2(50) :='Oracle'; v_m VARCHAR2(50) :='Oracle'; BEGIN BEGIN DECLARE DECLARE v_z VARCHAR2(20):='Second'; v_z VARCHAR2(20):='Second'; BEGIN BEGIN v_m:=v_m || v_z; v_m:=v_m || v_z; END; END; v_m:=v_m || ‘ Exam'; v_m:=v_m || ‘ Exam'; DBMS_OUTPUT.PUT_LINE(v_m); DBMS_OUTPUT.PUT_LINE(v_m); END; END;

6 Nested Blocks Example SQL> DECLARE SQL> DECLARE v_m VARCHAR2(50) :='Oracle'; v_m VARCHAR2(50) :='Oracle'; BEGIN BEGIN DECLARE DECLARE v_z VARCHAR2(20):='Second'; v_z VARCHAR2(20):='Second'; BEGIN BEGIN v_m:=v_m || v_z; v_m:=v_m || v_z; END; END; v_m:=v_m || ‘ Exam'; v_m:=v_m || ‘ Exam'; DBMS_OUTPUT.PUT_LINE(v_m); DBMS_OUTPUT.PUT_LINE(v_m); END; END; / Output: Oracle Second Exam

7 Nested Blocks Example Qualify an identifier by using the block label prefix. The qualifier can be the label of an enclosing block > DECLARE birthdate DATE; birthdate DATE;BEGIN DECLARE DECLARE birthdate DATE; birthdate DATE; BEGIN BEGIN IF birthdate = outer.birthdate THEN... IF birthdate = outer.birthdate THEN...

8 Nested Blocks Example <<one>>Declare x integer := 5; z varchar2(20) := 'Oracle'; begin x := x+1; x := x+1; DBMS_OUTPUT.PUT_LINE('X ='|| x) ; DBMS_OUTPUT.PUT_LINE('X ='|| x) ; Declare Declare x integer := 30 ; x integer := 30 ; y number := 2.5; y number := 2.5; Begin Begin x := x+2; x := x+2; DBMS_OUTPUT.PUT_LINE('X ='|| x); DBMS_OUTPUT.PUT_LINE('X ='|| x); DBMS_OUTPUT.PUT_LINE('z ='|| z); DBMS_OUTPUT.PUT_LINE('z ='|| z); DBMS_OUTPUT.PUT_LINE('y ='|| y); DBMS_OUTPUT.PUT_LINE('y ='|| y); DBMS_OUTPUT.PUT_LINE('global oneis ' || one.x); DBMS_OUTPUT.PUT_LINE('global oneis ' || one.x); End ; End ; DBMS_OUTPUT.PUT_LINE('X ='|| x); DBMS_OUTPUT.PUT_LINE('X ='|| x);End;

9 The PL/SQL operators are generally the same as those of SQL(Arithmetic, logical,..) The PL/SQL operators are generally the same as those of SQL(Arithmetic, logical,..) ** is the exponential operator in PL/SQL ** is the exponential operator in PL/SQL dbms_output.put_line(2**3); --Output is 8 dbms_output.put_line(2**3); --Output is 8 Comparison involving null always yield null Comparison involving null always yield null Examples Examples V_count := v_count+1; V_count := v_count+1; V_equal :=(v_n1=v_n2); V_equal :=(v_n1=v_n2); V_valid :=(v_empno IS NOT NULL); V_valid :=(v_empno IS NOT NULL); The PL/SQL operators are generally the same as those of SQL(Arithmetic, logical,..) The PL/SQL operators are generally the same as those of SQL(Arithmetic, logical,..) ** is the exponential operator in PL/SQL ** is the exponential operator in PL/SQL dbms_output.put_line(2**3); --Output is 8 dbms_output.put_line(2**3); --Output is 8 Comparison involving null always yield null Comparison involving null always yield null Examples Examples V_count := v_count+1; V_count := v_count+1; V_equal :=(v_n1=v_n2); V_equal :=(v_n1=v_n2); V_valid :=(v_empno IS NOT NULL); V_valid :=(v_empno IS NOT NULL); Operators in PL/SQL

10 Programming Guidelines Use comments Use comments Use a case convention for the code Use a case convention for the code Write keywords in uppercase Write keywords in uppercase Write identifiers in lowercase Write identifiers in lowercase Use naming convention for identifiers Use naming convention for identifiers Variables: v_name Variables: v_name Constants: c_name Constants: c_name … Use Indentation Use Indentation Use comments Use comments Use a case convention for the code Use a case convention for the code Write keywords in uppercase Write keywords in uppercase Write identifiers in lowercase Write identifiers in lowercase Use naming convention for identifiers Use naming convention for identifiers Variables: v_name Variables: v_name Constants: c_name Constants: c_name … Use Indentation Use Indentation

11 Indenting Code For clarity, indent each level of code. For clarity, indent each level of code. Example Example For clarity, indent each level of code. For clarity, indent each level of code. Example Example BEGIN IF x=0 THEN y:=1; END IF; END; BEGIN IF x=0 THEN y:=1; END IF; END; DECLARE v_deptnoNUMBER(2); v_locationVARCHAR2(13); BEGIN SELECTdeptno, loc INTOv_deptno, v_location FROMdept WHEREdname = 'SALES';... END; DECLARE v_deptnoNUMBER(2); v_locationVARCHAR2(13); BEGIN SELECTdeptno, loc INTOv_deptno, v_location FROMdept WHEREdname = 'SALES';... END;

12 Determining Variable Scope Class Exercise Class Exercise... DECLARE V_SALNUMBER(7,2) := 60000; V_COMMNUMBER(7,2) := V_SAL *.20; V_MESSAGEVARCHAR2(255) := ' eligible for commission'; BEGIN... DECLARE V_SALNUMBER(7,2) := 50000; V_COMM NUMBER(7,2) := 0; V_TOTAL_COMPNUMBER(7,2) := V_SAL + V_COMM; BEGIN... V_MESSAGE := 'CLERK not'||V_MESSAGE; END; V_MESSAGE := 'SALESMAN'||V_MESSAGE; END;... DECLARE V_SALNUMBER(7,2) := 60000; V_COMMNUMBER(7,2) := V_SAL *.20; V_MESSAGEVARCHAR2(255) := ' eligible for commission'; BEGIN... DECLARE V_SALNUMBER(7,2) := 50000; V_COMM NUMBER(7,2) := 0; V_TOTAL_COMPNUMBER(7,2) := V_SAL + V_COMM; BEGIN... V_MESSAGE := 'CLERK not'||V_MESSAGE; END; V_MESSAGE := 'SALESMAN'||V_MESSAGE; END;

13 Interacting with the Oracle Server

Comparing SQL and PL/SQL Statement Types A PL/SQL block is not a transaction unit. Commits, savepoints, and rollbacks are independent of blocks, but you can issue these commands within a block. A PL/SQL block is not a transaction unit. Commits, savepoints, and rollbacks are independent of blocks, but you can issue these commands within a block. PL/SQL does not support data definition language (DDL), such as CREATE TABLE, ALTER TABLE, or DROP TABLE. PL/SQL does not support data definition language (DDL), such as CREATE TABLE, ALTER TABLE, or DROP TABLE. PL/SQL does not support data control language (DCL), such as GRANT or REVOKE. PL/SQL does not support data control language (DCL), such as GRANT or REVOKE. A PL/SQL block is not a transaction unit. Commits, savepoints, and rollbacks are independent of blocks, but you can issue these commands within a block. A PL/SQL block is not a transaction unit. Commits, savepoints, and rollbacks are independent of blocks, but you can issue these commands within a block. PL/SQL does not support data definition language (DDL), such as CREATE TABLE, ALTER TABLE, or DROP TABLE. PL/SQL does not support data definition language (DDL), such as CREATE TABLE, ALTER TABLE, or DROP TABLE. PL/SQL does not support data control language (DCL), such as GRANT or REVOKE. PL/SQL does not support data control language (DCL), such as GRANT or REVOKE.

SQL Statements in PL/SQL Extract a row of data from the database by using the SELECT command. Only a single set of values can be returned. Extract a row of data from the database by using the SELECT command. Only a single set of values can be returned. Make changes to rows in the database by using DML commands. Make changes to rows in the database by using DML commands. Control a transaction with the COMMIT, ROLLBACK, or SAVEPOINT command. Control a transaction with the COMMIT, ROLLBACK, or SAVEPOINT command. Determine DML outcome with implicit cursors. Determine DML outcome with implicit cursors. Extract a row of data from the database by using the SELECT command. Only a single set of values can be returned. Extract a row of data from the database by using the SELECT command. Only a single set of values can be returned. Make changes to rows in the database by using DML commands. Make changes to rows in the database by using DML commands. Control a transaction with the COMMIT, ROLLBACK, or SAVEPOINT command. Control a transaction with the COMMIT, ROLLBACK, or SAVEPOINT command. Determine DML outcome with implicit cursors. Determine DML outcome with implicit cursors.

16 SELECT Statements in PL/SQL Retrieve data from the database with SELECT. Retrieve data from the database with SELECT. Syntax Syntax Retrieve data from the database with SELECT. Retrieve data from the database with SELECT. Syntax Syntax SELECT select_list INTO {variable_name[, variable_name]... | record_name} FROM table WHERE condition;

17 SELECT Statements in PL/SQL The INTO clause is required. The INTO clause is required. You must give one variable for each item selected You must give one variable for each item selected Queries Must Return One and Only One Row Queries Must Return One and Only One Row More than one row or no row generates an error. More than one row or no row generates an error. The INTO clause is required. The INTO clause is required. You must give one variable for each item selected You must give one variable for each item selected Queries Must Return One and Only One Row Queries Must Return One and Only One Row More than one row or no row generates an error. More than one row or no row generates an error. DECLARE v_deptnoNUMBER(2); v_locVARCHAR2(15); BEGIN SELECTdeptno, loc INTOv_deptno, v_loc FROMdept WHEREdname = 'SALES';... END; DECLARE v_deptnoNUMBER(2); v_locVARCHAR2(15); BEGIN SELECTdeptno, loc INTOv_deptno, v_loc FROMdept WHEREdname = 'SALES';... END;

18 Retrieving Data in PL/SQL Terminate each SQL statement with a (;). Terminate each SQL statement with a (;). The INTO clause is required for the SELECT statement when it is embedded in PL/SQL. The INTO clause is required for the SELECT statement when it is embedded in PL/SQL. Retrieve the order date and the ship date for the specified order. Retrieve the order date and the ship date for the specified order. Terminate each SQL statement with a (;). Terminate each SQL statement with a (;). The INTO clause is required for the SELECT statement when it is embedded in PL/SQL. The INTO clause is required for the SELECT statement when it is embedded in PL/SQL. Retrieve the order date and the ship date for the specified order. Retrieve the order date and the ship date for the specified order. DECLARE v_orderdate ord.orderdate%TYPE; v_shipdate ord.shipdate%TYPE; BEGIN SELECT orderdate, shipdate INTO v_orderdate, v_shipdate FROM ord WHERE id = 620;... END; DECLARE v_orderdate ord.orderdate%TYPE; v_shipdate ord.shipdate%TYPE; BEGIN SELECT orderdate, shipdate INTO v_orderdate, v_shipdate FROM ord WHERE id = 620;... END;

19 Retrieving Data in PL/SQL Group functions cannot be used in PL/SQL syntax. They are used in SQL statements within a PL/SQL block. Group functions cannot be used in PL/SQL syntax. They are used in SQL statements within a PL/SQL block. Return the sum of the salaries for all employees in the specified department. Return the sum of the salaries for all employees in the specified department. Group functions cannot be used in PL/SQL syntax. They are used in SQL statements within a PL/SQL block. Group functions cannot be used in PL/SQL syntax. They are used in SQL statements within a PL/SQL block. Return the sum of the salaries for all employees in the specified department. Return the sum of the salaries for all employees in the specified department. DECLARE v_sum_sal emp.sal%TYPE; v_deptno NUMBER NOT NULL := 10; BEGIN SELECTSUM(sal) -- group function INTOv_sum_sal FROMemp WHEREdeptno = v_deptno; END;

20 INSERTUPDATEDELETE Manipulating Data Using PL/SQL Make changes to database tables by using DML commands: Make changes to database tables by using DML commands: INSERT INSERT UPDATE UPDATE DELETE DELETE Make changes to database tables by using DML commands: Make changes to database tables by using DML commands: INSERT INSERT UPDATE UPDATE DELETE DELETE

21 Inserting Data Add new employee information to the EMP table. Add new employee information to the EMP table. Example Example Add new employee information to the EMP table. Add new employee information to the EMP table. Example Example BEGIN INSERT INTO emp(empno, ename, job, deptno) VALUES(empno_sequence.NEXTVAL, 'HARDING', 'CLERK', 10); END; BEGIN INSERT INTO emp(empno, ename, job, deptno) VALUES(empno_sequence.NEXTVAL, 'HARDING', 'CLERK', 10); END;

22 Updating Data Increase the salary of all employees in the EMP table who are Analysts. Increase the salary of all employees in the EMP table who are Analysts. PL/SQL variable assignments always use := and SQL column assignments always use =. PL/SQL variable assignments always use := and SQL column assignments always use =. if column names and identifier names are identical in the WHERE clause, the Oracle Server looks to the database first for the name. if column names and identifier names are identical in the WHERE clause, the Oracle Server looks to the database first for the name. Increase the salary of all employees in the EMP table who are Analysts. Increase the salary of all employees in the EMP table who are Analysts. PL/SQL variable assignments always use := and SQL column assignments always use =. PL/SQL variable assignments always use := and SQL column assignments always use =. if column names and identifier names are identical in the WHERE clause, the Oracle Server looks to the database first for the name. if column names and identifier names are identical in the WHERE clause, the Oracle Server looks to the database first for the name. DECLARE v_sal_increase emp.sal%TYPE := 2000; BEGIN UPDATEemp SETsal = sal + v_sal_increase WHEREjob = 'ANALYST'; END;

23 Deleting Data Delete rows that belong to department 10 from the EMP table. Delete rows that belong to department 10 from the EMP table. Example Example Delete rows that belong to department 10 from the EMP table. Delete rows that belong to department 10 from the EMP table. Example Example DECLARE v_deptno emp.deptno%TYPE := 10; BEGIN DELETE FROM emp WHERE deptno = v_deptno; END; DECLARE v_deptno emp.deptno%TYPE := 10; BEGIN DELETE FROM emp WHERE deptno = v_deptno; END;

24 NotesNotes There is no possibility for ambiguity with identifiers and column names in the INSERT statement. Any identifier in the INSERT clause must be a database column name. There is no possibility for ambiguity with identifiers and column names in the INSERT statement. Any identifier in the INSERT clause must be a database column name. There may be ambiguity in the SET clause of the UPDATE statement because although the identifier on the left of the assignment operator is always a database column, the identifier on the right can be either a database column or a PL/SQL variable. There may be ambiguity in the SET clause of the UPDATE statement because although the identifier on the left of the assignment operator is always a database column, the identifier on the right can be either a database column or a PL/SQL variable. There is no possibility for ambiguity with identifiers and column names in the INSERT statement. Any identifier in the INSERT clause must be a database column name. There is no possibility for ambiguity with identifiers and column names in the INSERT statement. Any identifier in the INSERT clause must be a database column name. There may be ambiguity in the SET clause of the UPDATE statement because although the identifier on the left of the assignment operator is always a database column, the identifier on the right can be either a database column or a PL/SQL variable. There may be ambiguity in the SET clause of the UPDATE statement because although the identifier on the left of the assignment operator is always a database column, the identifier on the right can be either a database column or a PL/SQL variable.

Naming Conventions Use a naming convention to avoid ambiguity in the WHERE clause. Use a naming convention to avoid ambiguity in the WHERE clause. Database columns and identifiers should have distinct names. Database columns and identifiers should have distinct names. Syntax errors can arise because PL/SQL checks the database first for a column in the table. Syntax errors can arise because PL/SQL checks the database first for a column in the table. Use a naming convention to avoid ambiguity in the WHERE clause. Use a naming convention to avoid ambiguity in the WHERE clause. Database columns and identifiers should have distinct names. Database columns and identifiers should have distinct names. Syntax errors can arise because PL/SQL checks the database first for a column in the table. Syntax errors can arise because PL/SQL checks the database first for a column in the table.

26 Naming Conventions DECLARE orderdateord.orderdate%TYPE; shipdate ord.shipdate%TYPE; ordid ord.ordid%TYPE := 601; BEGIN SELECT orderdate, shipdate INTO orderdate, shipdate FROM ord WHERE ordid = ordid; END; SQL> / DECLARE * ERROR at line 1: ORA-01422: exact fetch returns more than requested number of rows ORA-06512: at line 6 DECLARE orderdateord.orderdate%TYPE; shipdate ord.shipdate%TYPE; ordid ord.ordid%TYPE := 601; BEGIN SELECT orderdate, shipdate INTO orderdate, shipdate FROM ord WHERE ordid = ordid; END; SQL> / DECLARE * ERROR at line 1: ORA-01422: exact fetch returns more than requested number of rows ORA-06512: at line 6

27 NotesNotes There is no possibility for ambiguity in the SELECT clause because any identifier in the SELECT clause must be a database column name. There is no possibility for ambiguity in the INTO clause because identifiers in the INTO clause must be PL/SQL variables. Only in the WHERE clause is there the possibility of confusion.

28 Example

29 Example

30 Example Example