Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic...

26
MBUG 2013 Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013

Transcript of Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic...

Page 1: Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.

MBUG 2013

Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen FredericInstitution: IHLSeptember 16, 2013

Page 2: Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.

Please turn off your cell phone If you must leave the session early, please

do so discreetly Please avoid side conversation during the

session

Session Rules of Etiquette

Page 3: Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.

Intro to SQL Parts of an SQL statement Performing SQL queries

Session Overview

Page 4: Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.

Intro to PL/SQL Parts of PL/SQL file Using SQL in PL/SQL Writing PL/SQL reports

Session Overview Continued

Page 5: Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.

SQL

Page 6: Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.

Structured Query Language Used for managing data held in a relational

database management system Initially developed by IBM in the early 1970s

What is SQL?

Page 7: Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.

SELECT – data fields retrieved from tables FROM – table name(s) WHERE – conditions using operators to filter

what data is retrieved

Queries

Page 8: Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.

AND OR LIKE IN BETWEEN

Conditions

Page 9: Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.

Equal to: = Not equal to: != or <> Greater than: > Less than: < Greater than or equal to: >= Less than or equal to: <= IS or IS NOT

Operators

Page 10: Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.

Spool – saves query results in a file◦ spool filename

Everything following ‘spool’ is written to file When finished ‘spool off’

Writing to Files

Page 11: Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.

Describing Tables

Page 12: Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.

SELECT spriden_first_name, spriden_last_nameFROM spridenWHERE spriden_id = ‘902580337’AND spriden_change_ind is null;

Simple SQL Query

Page 13: Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.

Getting Address Information

Page 14: Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.

PL/SQL

Page 15: Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.

Procedural Language/Structured Query Language

Oracle’s procedural extention to SQL Can use conditional statements such as

if/then/else as well as loops Handles errors and exceptions

PL/SQL

Page 16: Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.

Declaration Execution Exception

Parts of PL/SQL Blocks

Page 17: Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.

Begins with ‘DECLARE’ Is optional Where SQL cursors are written for use in

execution section

Declaration Section

Page 18: Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.

Defines variables Variables can also be initialized Example:

◦ age number(2);◦ message varchar2(12) := ‘Hello World’;

Declare Section Continued

Page 19: Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.

Starts with ‘BEGIN’ and ends with ‘END’ Is mandatory Where if/then statements, loops, and

cursors are used

Execution Section

Page 20: Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.

Begins with ‘EXCEPTION’ Optional Handles errors so blocks terminate without

problems Useful when updating or deleting

information in tables

Exception Section

Page 21: Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.

Found in Declaration section Where SQL statements are written Variables can be passed to cursors for

dynamic queries

Cursors

Page 22: Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.

Loops OPEN, FETCH, CLOSE

◦ Pass variables in OPEN◦ FETCH into variable names◦ CLOSE when finished

Calling Cursors

Page 23: Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.

Spool on/off can be written into PL/SQL file Dbms_output

◦ .put(‘text’) – prints lines, appends to line◦ .put_line(‘text’) – prints line and then returns to

new line

Getting Output

Page 24: Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.

Save both SQL and PL/SQL files with .sql extension

To run: @filename

Saving and Running Files

Page 25: Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.

PL/SQL Example

Page 26: Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.

http://www.techonthenet.com/oracle/ http://www.w3schools.com/sql/

Additional Help