What’s So Great About PL/SQL

Slides:



Advertisements
Similar presentations
Subquery. Subquery or Inner query or Nested query A subquery is a query within another query. The outer query is called as main query and The inner query.
Advertisements

MUCH ADO ABOUT NOTHING Walter Schenk SoluTech Consulting Services Inc.
Beginning SQL Tutorial Author Jay Mussan-Levy. What is SQL?  Structured Query Language  Communicate with databases  Used to created and edit databases.
Advanced Package Concepts. 2 home back first prev next last What Will I Learn? Write packages that use the overloading feature Write packages that use.
Chapter 9: Advanced SQL and PL/SQL Topics Guide to Oracle 10g.
Introduction to Oracle9i: SQL1 Selected Single-Row Functions.
Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
30-Jun-15 SQL A Brief Introduction. SQL SQL is Structured Query Language Some people pronounce SQL as “sequel” Other people insist that only “ess-cue-ell”
2 Copyright © 2004, Oracle. All rights reserved. Creating Stored Functions.
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
Introduction to PL/SQL Lecture 0 – Self Study Akhtar Ali.
Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.
Introduction to PL/SQL. Procedural Language extension for SQL Oracle Proprietary 3GL Capabilities Integration of SQL Portable within Oracle data bases.
ORACLE ONLINE TRAINING Contact our Support Team : SOFTNSOL India: Skype id : softnsoltrainings id:
SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions.
ASP.NET Programming with C# and SQL Server First Edition
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.
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL.
Chapter 5 Selected Single-Row Functions. Chapter Objectives  Use the UPPER, LOWER, and INITCAP functions to change the case of field values and character.
Functions Oracle Labs 5 & 6. 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 2 SQL Functions Function arg n arg 2 arg 1. Input Resulting Value.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
Lecture 8 Creating Stored Functions. Objectives  After completing this lesson, you should be able to do the following:  What is Function?  Types of.
Single Row Functions Week 2. Objectives –Describe types of single row functions in SQL –Describe and use character, number, date, general and conversion.
Oracle 11g: SQL Chapter 10 Selected Single-Row Functions.
LECTURE 1 INTRODUCTION TO PL/SQL Tasneem Ghnaimat.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
Intro to SQL Management Studio. Please Be Sure!! Make sure that your access is read only. If it isn’t, you have the potential to change data within your.
Chapter 3 Selected Single-Row Functions and Advanced DML & DDL.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 8 Advanced SQL.
Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to Insert, Update, Delete, and View Data.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
Commercial RDBMSs Access and Oracle. Access DBMS Architchecture  Can be used as a standalone system on a single PC: -JET Engine -Microsoft Data Engine.
EXPRESSION Transformation. Introduction ►Transformations help to transform the source data according to the requirements of target system and it ensures.
IFS Intro to Data Management Chapter 5 Getting More Than Simple Columns.
Visual Programing SQL Overview Section 1.
Single Row Functions. Objectives –Use character, number, and date functions –Use conversion functions –Describe types of single row functions in SQL.
Chapter 9: Advanced SQL and PL/SQL Guide to Oracle 10g.
Dynamic SQL. 2 home back first prev next last What Will I Learn? Recall the stages through which all SQL statements pass Describe the reasons for using.
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
Creating Functions. V 12 NE - Oracle 2006 Overview of Stored Functions A function is a named PL/SQL block that returns a value A function can be stored.
Built-in SQL Functions. 2 Type of Functions Character Functions returning character values returning numeric values Numeric Functions Date Functions Conversion.
Oracle & SQL. Oracle Data Types Character Data Types: Char(2) Varchar (20) Clob: large character string as long as 4GB Bolb and bfile: large amount of.
4/2/16. Ltrim() is used to remove leading occurrences of characters. If we don’t specify a character, Oracle will remove leading spaces. For example Running.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Overview.
Gollis University Faculty of Computer Engineering Chapter Five: Retrieval, Functions Instructor: Mukhtar M Ali “Hakaale” BCS.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
 CONACT UC:  Magnific training   
Stored Procedures and Functions Pemrograman Basis Data MI2183.
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
13 Copyright © 2004, Oracle. All rights reserved. Migrating SQL Statements.
 Reviewing basic architecture concepts  Oracle 10g Architecture  Main features of 9i and 10g
Single Row Functions Part I Week 2. Objectives –Describe types of single row functions in SQL –Describe and use character, number and date SQL functions.
1 Peter Eisentraut Sun Microsystems PORTING APPLICATIONS FROM ORACLE TO POSTGRESQL 1.
Oracle SQL.
Relational Database Design
Oracle SQL.
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL
Database Systems: Design, Implementation, and Management Tenth Edition
JDBC.
Using Single-Row Functions to Customize Output
Creating Noninput Items
Index Note: A bolded number or letter refers to an entire lesson or appendix. A Adding Data Through a View ADD_MONTHS Function 03-22, 03-23,
Contents Preface I Introduction Lesson Objectives I-2
Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Tenth Edition
IST 318 Database Administration
Presentation transcript:

What’s So Great About PL/SQL By Paul Hunter Thanks to our Gold Sponsors

Overview Introductions: Topics: Paul Hunter paul.hunter@NightOwlSystems.com Topics: Translating to Oracle Procedures & Functions Package / Package Body PL/SQL Native Function Highlights

A Little About Me I’ve been a developer for 20+ years and a SQL developer for 12 of those years I’m a Certified Scrum Master NTSSUG - NorthTexas.SQLPass.org DFW Scrum - www.DFWScrum.com Dallas ASP.NET User Group  www.DallasASP.net Application development & design w/SQL & .NET I prefer Agile/SCRUM over waterfall methods Follow me on Twitter: @SqlNightOwl

Translating to Oracle Oracle & SQL Server are different, but the same. Many of the concepts map over very nicely while others need a little more work. SQL Server Oracle Instance Database Schema Page Block FileGroup TableSpace SQLCMD SQL*PLUS

More Translations rownum – this is similar to the top operator sysdate – returns the system date/time down to the second dual – This is not a function. This is an internal table containing a single row. There are many uses for this internal table. One common use is equivalent to the Numbers/Tally table in SQL server. Pick your favorite numbers/tally table method in these types of cases. sequence – delve into currval nextval desc[ribe] – this is similar to sp_help but only returns column name, null-ability and data type. Still, it’s handy. connect by – This is a pretty cool piece of functionality unique to Oracle. I’ve seen this used in recursive CTEs to help control the hierarchy. In these cases, it limits the result set to rows meeting the criteria of the connect by statement. Similar functionality can be achieved through use of Joins and the Where clause.

Working with Oracle I like using linked servers because you can then query Oracle using one of two methods: Direct Query: SELECT * FROM LSN..SCHEMA.TABLE_OR_VIEW OpenQuery: SELECT * FROM OPENQUERY(LSN, 'SELECT * FROM TABLE_OR_VIEW')

Procedures & Functions There are many similarities between a SQL Server stored procedure and an Oracle procedure; however there are some significant differences: You must declare the parameter direction (in/out/inout) The parameters must be enclosed in parentheses (ever if there are no parameters) You cannot return a recordset from an Oracle procedure. Insert, update & delete are the only supported actions; however you can return a single record thru output parameters or in RowType variable. PL/SQL uses unbound blocks

Sample Block Header CREATE OR REPLACE PROCEDURE procedure_name ( parameter_name direction datatype [:= default_value] [, parameterX direction datatype [:= default_value]] )   IS -- but you can also use AS Declaration Section DECLARE variable_name datatype [:= initial value]; BEGIN Execution Section select some_value into some_variable from some_table where column_name = parameter_name; Now go and do something interesting... EXCEPTION Exception Section when NO_DATA_FOUND then null; when other rollback work; END;

Miscellaneous Stuff %type / %rowtype – this is REALLY COOL. You can declare variables based on a specific column’s data type or the type of the entire row. If you’re using a linked server, you can execute the procedure by creating an anonymous PL/SQL block: declare @cmd varchar(50) set @cmd = 'begin procedure_name( parameter_value [, etc.] ); end;'; exec (@cmd) at LSN;

Package / Package Body This is similar in concept to an interface class and the implementation of the interface in another class. The Package specifies the declaration of the public method “signature” The Package Body contains the procedures and functions to implement the Package interface The Package Body may contain many more procedures and functions than are defined in the Package. There is a 1-to-1 mapping between a Package and Package Body

PL/SQL Functions Aggregates Strings Most of these are named and behave the same as their SQL Server counterpart. Strings || – concatenation in PL/SQL initcap() – no comparable function applies to the first letter of each word in a string. lpad() / rpad() – allow you to pad a string with another string. If no pad string is provided then a blank space is used.

Strings (continued) substr() – In SQL, this translates to substring. Easy enough right? There is one more difference between the two than just the name. The parameters are ordered differently in substr() than they are in substring(). Pay careful attention to your parameter sequence when converting this function from Oracle to SQL Server. instr() – This one is less obvious. I have used PatIndex() and CharIndex() for this one – depends on needed functionality. If you understand that instr is searching for a value within a string – it makes it a little easier to understand. Also knowing that PatIndex searches for “Patterns” and CharIndex() searches for a character is helpful. If you need to supply the optional parameter used by instr(), then you should use CharIndex. Though not entirely the same – similar functionality is available in SQL for the instr() function.

Dates add_months() – adds X number of months to the date provided. If the original date is the last day of the month then the date returned will always be the last day, otherwise the day will remain unchanged. Negative numbers can be moved to find prior dates. last_day() – returns the last day of the month for the date provided. months_between() – returns the number of months between the two dates. Returns an integer if the two dates are the same day of the month otherwise it’s a decimal value. next_day() – returns the next occurrence of a specific weekday

Miscellaneous Functions greatest() / least() – will take the max/min value in the list of values provided. Kind of an aggregate without having to us a group by. decode() – similar to inline if or case statement (max parameters 255). Sample: decode(some_column, null, ‘no where’, ‘TX’, ‘Texas’, ‘OK’, ‘Oklahoma’, ’who cares’) trunc() – converts a date or number to an “integer” format (removes time portion of dates or no decimal places). This is achieved through different means in SQL. Two common methods are cast(), convert() and/or floor(). nlv() – functionally equivalent to isnull()