Les 04

  • Uploaded by: alaasamak
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Les 04 as PDF for free.

More details

  • Words: 1,017
  • Pages: 26
4

Interacting with the Oracle Server

Copyright © 2006, Oracle. All rights reserved.

Objectives After completing this lesson, you should be able to do the following: • Determine which SQL statements can be directly included in a PL/SQL executable block • Manipulate data with DML statements in PL/SQL • Use transaction control statements in PL/SQL • Make use of the INTO clause to hold the values returned by a SQL statement • Differentiate between implicit cursors and explicit cursors • Use SQL cursor attributes

4-2

Copyright © 2006, Oracle. All rights reserved.

SQL Statements in PL/SQL • • •

4-3

Retrieve a row from the database by using the SELECT command. Make changes to rows in the database by using DML commands. Control a transaction with the COMMIT, ROLLBACK, or SAVEPOINT command.

Copyright © 2006, Oracle. All rights reserved.

4-4

Copyright © 2006, Oracle. All rights reserved.

SELECT Statements in PL/SQL Retrieve data from the database with a SELECT statement. Syntax: SELECT INTO

select_list {variable_name[, variable_name]... | record_name} FROM table [WHERE condition];

4-5

Copyright © 2006, Oracle. All rights reserved.

4-6

Copyright © 2006, Oracle. All rights reserved.

SELECT Statements in PL/SQL • •

The INTO clause is required. Queries must return only one row.

Example SET SERVEROUTPUT ON DECLARE fname VARCHAR2(25); BEGIN SELECT first_name INTO fname FROM employees WHERE employee_id=200; DBMS_OUTPUT.PUT_LINE(' First Name is : '||fname); END; /

4-7

Copyright © 2006, Oracle. All rights reserved.

4-8

Copyright © 2006, Oracle. All rights reserved.

Retrieving Data in PL/SQL Retrieve the hire_date and the salary for the specified employee. Example DECLARE emp_hiredate employees.hire_date%TYPE; emp_salary employees.salary%TYPE; BEGIN SELECT hire_date, salary INTO emp_hiredate, emp_salary FROM employees WHERE employee_id = 100; END; /

4-9

Copyright © 2006, Oracle. All rights reserved.

Retrieving Data in PL/SQL Return the sum of the salaries for all the employees in the specified department. Example SET SERVEROUTPUT ON DECLARE sum_sal NUMBER(10,2); deptno NUMBER NOT NULL := 60; BEGIN SELECT SUM(salary) -- group function INTO sum_sal FROM employees WHERE department_id = deptno; DBMS_OUTPUT.PUT_LINE ('The sum of salary is ' || sum_sal); END; / 4-10

Copyright © 2006, Oracle. All rights reserved.

Naming Conventions

DECLARE hire_date sysdate employee_id BEGIN SELECT INTO FROM WHERE END; /

4-11

employees.hire_date%TYPE; hire_date%TYPE; employees.employee_id%TYPE := 176; hire_date, sysdate hire_date, sysdate employees employee_id = employee_id;

Copyright © 2006, Oracle. All rights reserved.

Naming Conventions • • • •



4-12

Use a naming convention to avoid ambiguity in the WHERE clause. Avoid using database column names as identifiers. Syntax errors can arise because PL/SQL checks the database first for a column in the table. The names of local variables and formal parameters take precedence over the names of database tables. The names of database table columns take precedence over the names of local variables.

Copyright © 2006, Oracle. All rights reserved.

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

UPDATE

4-13

MERGE

Copyright © 2006, Oracle. All rights reserved.

Inserting Data Add new employee information to the EMPLOYEES table. Example BEGIN INSERT INTO employees (employee_id, first_name, last_name, email, hire_date, job_id, salary) VALUES(employees_seq.NEXTVAL, 'Ruth', 'Cores', 'RCORES',sysdate, 'AD_ASST', 4000); END; /

4-14

Copyright © 2006, Oracle. All rights reserved.

Updating Data Increase the salary of all employees who are stock clerks. Example DECLARE sal_increase employees.salary%TYPE := 800; BEGIN UPDATE employees SET salary = salary + sal_increase WHERE job_id = 'ST_CLERK'; END; /

4-15

Copyright © 2006, Oracle. All rights reserved.

Deleting Data Delete rows that belong to department 10 from the employees table. Example DECLARE deptno employees.department_id%TYPE := 10; BEGIN DELETE FROM employees WHERE department_id = deptno; END; /

4-16

Copyright © 2006, Oracle. All rights reserved.

Merging Rows Insert or update rows in the copy_emp table to match the employees table. DECLARE empno employees.employee_id%TYPE := 100; BEGIN MERGE INTO copy_emp c USING employees e ON (e.employee_id = c.empno) WHEN MATCHED THEN UPDATE SET c.first_name = e.first_name, c.last_name = e.last_name, c.email = e.email, . . . WHEN NOT MATCHED THEN INSERT VALUES(e.employee_id, e.first_name, e.last_name, . . .,e.department_id); END; / 4-17

Copyright © 2006, Oracle. All rights reserved.

4-18

Copyright © 2006, Oracle. All rights reserved.

SQL Cursor • •

A cursor is a pointer to the private memory area allocated by the Oracle server. There are two types of cursors: – Implicit: Created and managed internally by the Oracle server to process SQL statements – Explicit: Explicitly declared by the programmer

4-19

Copyright © 2006, Oracle. All rights reserved.

4-20

Copyright © 2006, Oracle. All rights reserved.

SQL Cursor Attributes for Implicit Cursors Using SQL cursor attributes, you can test the outcome of your SQL statements. SQL%FOUND

Boolean attribute that evaluates to TRUE if the most recent SQL statement returned at least one row

SQL%NOTFOUND

Boolean attribute that evaluates to TRUE if the most recent SQL statement did not return even one row

SQL%ROWCOUNT

An integer value that represents the number of rows affected by the most recent SQL statement

4-21

Copyright © 2006, Oracle. All rights reserved.

SQL Cursor Attributes for Implicit Cursors Delete rows that have the specified employee ID from the employees table. Print the number of rows deleted. Example VARIABLE rows_deleted VARCHAR2(30) DECLARE empno employees.employee_id%TYPE := 176; BEGIN DELETE FROM employees WHERE employee_id = empno; :rows_deleted := (SQL%ROWCOUNT || ' row deleted.'); END; / PRINT rows_deleted 4-22

Copyright © 2006, Oracle. All rights reserved.

Summary In this lesson, you should have learned how to: • Embed DML statements, transaction control statements, and DDL statements in PL/SQL • Use the INTO clause, which is mandatory for all SELECT statements in PL/SQL • Differentiate between implicit cursors and explicit cursors • Use SQL cursor attributes to determine the outcome of SQL statements

4-23

Copyright © 2006, Oracle. All rights reserved.

Practice 4: Overview This practice covers the following topics: • Selecting data from a table • Inserting data into a table • Updating data in a table • Deleting a record from a table

4-24

Copyright © 2006, Oracle. All rights reserved.

4-25

Copyright © 2006, Oracle. All rights reserved.

4-26

Copyright © 2006, Oracle. All rights reserved.

Related Documents

Les 04
May 2020 6
Les 04
May 2020 9
Werkblad Les 04
November 2019 20
Les
May 2020 44
Les
June 2020 42

More Documents from ""

Intro
May 2020 5
Les 09
May 2020 10
Les 08
May 2020 6
Les 04
May 2020 6