1 SQL/PSM Procedures Stored in the Database General-Purpose Programming.

Slides:



Advertisements
Similar presentations
BD05/06 PL/SQL  Introduction  Structure of a block  Variables and types  Accessing the database  Control flow  Cursors  Exceptions  Procedures.
Advertisements

AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
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.
Stored Procedure Language Stored Procedure Overview Stored Procedure is a function in a shared library accessible to the database server can also write.
Fall 2001Arthur Keller – CS 18011–1 Schedule Oct. 30 (T) Embedded SQL. u Read Section 8.1. u Assignment 5 due. Not accepted late. u Project Part 4 due.
Winter 2002Arthur Keller – CS 1809–1 Schedule Today: Jan. 31 (TH) u Constraints. u Read Sections , Project Part 3 due. Feb. 5 (T) u Triggers,
1 SQL/PSM Procedures Stored in the Database General-Purpose Programming.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
1 PL/SQL Oracle’s Version of Triggers and PSM. 2 PL/SQL uOracle uses a variant of SQL/PSM which it calls PL/SQL. uPL/SQL not only allows you to create.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #2.
1 SQL Programming Embedded SQL Call-Level Interface Java Database Connectivity Persistent Stored Modules.
Phonegap Bridge – File System CIS 136 Building Mobile Apps 1.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
Introduction to Python
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.
TRAINING SESSIONS SQL Server Basics Design By.:.Net Prepared By:Dilip Namdeo Dated:23-Dec-09 Reference Site:
Stored Procedures, Transactions, and Error-Handling
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Module 1: Introduction to Transact-SQL
Winter 2006Keller Ullman Cushing8–1 Turning in Assignments Please turn in hard copy (use only in the direst of circumstances). I am not your secretary.
CSC 2720 Building Web Applications Database and SQL.
Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides
Winter 2006Keller, Ullman, Cushing9–1 Constraints Commercial relational systems allow much more “fine-tuning” of constraints than do the modeling languages.
SQL Data Definition Language (DDL) Using Microsoft SQL Server 1SDL Data Definition Language (DDL)
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
Copyright © Curt Hill Stored Procedures In Transact-SQL.
11 3 / 12 CHAPTER Databases MIS105 Lec15 Irfan Ahmed Ilyas.
Stored Procedures Week 9. Test Details Stored Procedures SQL can call code written in iSeries High Level Languages –Called stored procedures SQL has.
SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections (except and 6.6.6) Next u Constraints. u Read.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
Winter 2006 Keller, Ullman, Cushing 11–1 Embedded SQL Add to a conventional programming language (C in our examples) certain statements that represent.
Programmatic SQL Shaista Khan CS 157B. Topic Embedded SQL statements in high-level programming languages.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 8 Advanced SQL.
1 Real SQL Programming - 2 Persistent Stored Modules (PSM) PL/SQL Embedded SQL These slides are reused from Jeffrey Ullman’s class with the author’s permission.
Databases 1 9th lecture. Main topic: Oracle PL/SQL What is PL/SQL? ▫Procedural Langauge extension for standard SQL.. PL/SQL not only allows you to create.
SQL Server 2005 Implementation and Maintenance Chapter 3: Tables and Views.
Introduction to PL/SQL. Main topic: Oracle PL/SQL What is PL/SQL? ▫Procedural Langauge extension for standard SQL.. PL/SQL not only allows you to create.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
Slide 1 of 19Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 In this session, you will learn to: Implement stored procedures Implement.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Learningcomputer.com SQL Server 2008 –Views, Functions and Stored Procedures.
Stored Procedures / Session 4/ 1 of 41 Session 4 Module 7: Introducing stored procedures Module 8: More about stored procedures.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
1 Stored Procedure, Function and Trigger. 2Objectives 1. Database Programming 2. Stored Procedure 3. Function 4. Trigger.
1 Database Design: DBS CB, 2 nd Edition SQL in a Server Environment: Stored Procedure & Embedded SQL Ch. 9.3, 9.4.
1 Introduction to Database Systems, CS420 SQL Persistent Stored Modules (PSM) – Stored Procedure.
CS422 Principles of Database Systems Oracle PL/SQL Chengyu Sun California State University, Los Angeles.
In this session, you will learn to: Create and manage views Implement a full-text search Implement batches Objectives.
T-SQL Scripts Advanced Database Dr. AlaaEddin Almabhouh.
Introduction to Database Systems, CS420
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Module 2: Creating Data Types and Tables
CMSC-461 Database Management Systems
Foreign Keys Local and Global Constraints Triggers
Yanal Alahmad Java Workshop Yanal Alahmad
Introduction to Database Systems, CS420
CPSC-608 Database Systems
CPSC-608 Database Systems
Database Models Relational Model
CPSC-310 Database Systems
Persistent Stored Modules (PSM) PL/SQL Embedded SQL
CPSC-608 Database Systems
Modification to Views Via Triggers
CPSC-608 Database Systems
CMSC-461 Database Management Systems
Stored Procedure Language
Presentation transcript:

1 SQL/PSM Procedures Stored in the Database General-Purpose Programming

2 Stored Procedures uPSM, or “persistent, stored modules,” allows us to store procedures as database schema elements. uPSM = a mixture of conventional statements (if, while, etc.) and SQL. uLets us do things we cannot do in SQL alone.

3 Functionality Stored procedures can be used to u Return information to the caller u Modify data in databases u Implement business logic in data tier u Control access to data u Improve performance of the system u Reduce network traffic u Perform other actions and operations (such as process , execute operating usystem commands and processes, and manage other SQL server objects)

4 Basic PSM Form CREATE PROC[EDURE] procedure_name [ parameter data_type} [= default] [OUTPUT] ] [,... n] AS sql_statement [... n]

5 Example Create Procedure varchar(50) as Select * from Bar where name Bars(name, addr, license)

6 Execute the Stored Procedure [ [ EXEC [ UTE ] ] { = ] { procedure_name [ ;number ] } [ = ] { value [ OUTPUT ] | [ DEFAULT ] ] [,...n ] [ WITH RECOMPILE ] uExample - Execute - - prGetBar ‘Joe’

7 Receive Information from a Stored Procedure uResult set uOutput parameters uReturn value uGlobal cursor

8 Returning Result Sets uTo obtain a result set from a stored procedure, insert a Transact-SQL statement that returns a result set into the body of the stored procedure. The simplest way is by using a Select statement, but you could also call another stored procedure.

9 Input and Output Parameters uThe header of the stored procedure contains a list of parameters delimited with a comma (,) character. Each parameter is defined with an identifier and a data type. Parameter identifiers must begin with the at sign uIf the parameter with the keyword Output after the data type, it can return a value to the caller.

10 Returning Values with Output Parameters CREATE PROCEDURE smallint OUTPUT AS smallint EXECUTE mathtutor 5, OUTPUT SELECT 'The result is: The result is: 30 CREATE PROCEDURE smallint OUTPUT AS smallint EXECUTE mathtutor 5, OUTPUT SELECT 'The result is: The result is: 30 Creating Stored Procedure Procedure Executing Stored Procedure Procedure Results of Stored Procedure Procedure

11 Another Example Create Procedure money output as from Sells where bar and uExecute the stored procedure - money - Execute prGetBeerPrice Output - Sells(bar, beer, price)

12 Return Value uEach stored procedure can end with a Return statement. The statement can be followed by an integer value that can be read by the caller. If the return value is not explicitly set, the server will return the default value—zero (0). uBecause return values are limited to integer data types, they are most often used to signal an status or error code to the caller.

13 Example Create Procedure money output as from Sells where bar and Return uExecute the stored procedure - money - - =prGetBeerPrice Output - ErrorCode

14 Default Values Default values are defined at the end of a parameter definition, behind the data types. All that is needed is an assignment (=) and a value.

15 Example if exists (select * from sysobjects where id = object_id(' prGetBar ') and OBJECTPROPERTY(id, 'IsProcedure') = 1) drop procedure prGetBar GO Create Procedure varchar(50) = ‘Joe’ as Select * from Bar where name uExecute uExecute prGetBar

16 Renaming Stored Procedures sp_rename = ] 'object_name', = ] 'new_name' [, = ] 'object_type' ] uExample Sp_rename prGetBar getBar

17 Altering Stored Procedures Alter Procedure varchar(50) = ‘Joe’ as Select * from Bar where name

18 Deleting Stored Procedures uDROP PROCEDURE { procedure } [,...n ] uExample Drop Procedure getBar

19 T-SQL Identifiers The following are the rules for creating identifiers: u Identifiers in SQL Server 2000 may have between 1 and 128 characters. There are exceptions to this rule: certain objects are limited (for instance, temporary tables can have identifiers up to only 116 characters long). uThe first character of the identifier must be a letter, underscore ( _ ), at or number sign (#). The first letter must be defined in the Unicode 2.0 standard. Among other letters, Latin letters a–z and A–Z can be used as a first character. Some characters and #) have special meanings in T-SQL. They act as signals to SQL Server to treat their carriers differently. u Subsequent characters must be letters from the Unicode 2.0 standard, or decimal digits, or one of the special #, _, or $. u SQL Server reserved words should not be used as object identifiers. u Identifiers cannot contain spaces or other special characters except #, _, or $.

20 T_SQL Data Type(1) Data TypeDesc.Max Length charfixed size8000 characters varcharVariable size8000 characters textVariable size2^ ncharUnicode, fixed size4000 characters nvarcharUnicode, Variable size4000 characters ntextUnicode, Variable size2^30 - 1

21 T_SQL Data Type(2) Data TypeStorage SizeMinimumMaximum int4 bytes-2^312^31– 1 smallint2 bytes-2^152^15– 1 tinyint1 byte0255 bigint8 bytes-2^632^63– 1 bit1 bit01

22 T_SQL Data Type(3) Data TypeStorage Size binary<8000 bytes varbinary<8000 bytes image<2G datetime smalldatetime uniqueidentifierGUID,NEWID()

23 Variables uLocal variable - begin with prefix. uGlobal variable - begin with an prefix.

24 Declaring Variables u They must begin - varchar(50) u It is possible to define several variables in a single Declare statement. You just need to separate them with commas: - varchar(50), varchar(30), smalldatetime u You can also define variables based on user-defined data types: - phone

25 Assigning Values with the Select Statement SELECT FROM Beers WHERE manf = ’Anheuser-Busch’; manufacture;

26 Assigning Values with the Set Statement  Bud Anheuser-Busch’

27 Displaying the Values of Variables

28 Global Variables select Make, Model, EqTypeid into OldEquipment from Equipment where EqTypeid = 2 if = 0 Print "No rows were copied!"

29 Flow-Control Statements u Comments u Statement block u If…Else u While…Break u Break u Continue u GoTo u WaitFor u Begin…End

30 Comments uSingle-Line Comments -- - A complete line or part of the line canbe marked as a comment if the user places two hyphens (--) at the beginning. uMultiline Comments: /* … */

31 Statement Blocks: Begin…End Begin Insert Order(OrderDate, RequestedById, @LocId) = <> 0 begin RaiseError('Error occurred while inserting Order!', 16,1) Return end End

32 Conditional Execution: The If Statement uSyntax 1 If boolean_expression {Transact-SQL_statement | statement_block} [else {Transact-SQL_statement | statement_block}]

33 Example Create Procedure prInsertUpdateSells -- store values in Sells table. money ) As varchar(50) = beer From sells Where bar and Sells(bar, beer, price)

34 Example(Cont.) -- does such bn already exists in the database IS NOT NULL --insert a new record Insert sells (bar, beer, price ) Else --if it does not exist Begin -- update the beer’s price Update sells Set Where and End

35 Conditional Execution: The If Statement uSyntax 2 If [NOT] Exists(subquery) {Transact-SQL_statement | statement_block} [else {Transact-SQL_statement | statement_block}]

36 Example Create Procedure money As If not = beer From sells Where bar and --insert a new record Insert sells (bar, beer, price) Else --if it does not exist Begin -- update the beer’s price Update sells Set Where and End

37 Looping: The While Statement While Boolean_expression {sql_statement | statement_block} [Break] {sql_statement | statement_block} [Continue]

38 Example Create Procedure prCalcFactorial -- calculate factorial -- 1! = ! = 3 * 2 * 1 -- n! = n * (n-1)*... 5 * 4 * 3 * 2 * int OUTPUT As = 1 > 1 begin end return 0

39 System Stored Procedure uSP_Databases uSP_Tables uSP_Addlogin uSP_GrantAccess uSP_Password u SP_Attach_DB uSP_Detach_DB uSp_Rename uXP_cmdshell uXp_Fileexist uXp Fixeddrives

40 Cursors uA cursor is essentially a tuple-variable that ranges over all tuples in the result of some query. uDeclare a cursor c by: DECLARE c CURSOR FOR ;

41 Opening and Closing Cursors uTo use cursor c, we must issue the command: OPEN c; - The query of c is evaluated, and c is set to point to the first tuple of the result. uWhen finished with c, issue command: CLOSE c;

42 Fetching Tuples From a Cursor uTo get the next tuple from cursor c, issue command: FETCH FROM c INTO x1, x2,…,xn ; uThe x ’s are a list of variables, one for each component of the tuples referred to by c. uc is moved automatically to the next tuple.

43 Breaking Cursor Loops --- (1) uThe usual way to use a cursor is to create a loop with a FETCH statement, and do something with each tuple fetched. uA tricky point is how we get out of the loop when the cursor has no more tuples to deliver.

44 Breaking Cursor Loops --- (2) uEach SQL operation returns a status, which is a 5-digit number. - For example, = “Everything OK,” and = “Failed to find a tuple.” uIn PSM, we can get the value of the status in a variable called SQLSTATE.

45 Breaking Cursor Loops --- (3) uWe may declare a condition, which is a boolean variable that is true if and only if SQLSTATE has a particular value.  Example: We can declare condition NotFound to represent by: DECLARE NotFound CONDITION FOR SQLSTATE ’02000’;

46 Breaking Cursor Loops --- (4) uThe structure of a cursor loop is thus: cursorLoop: LOOP … FETCH c INTO … ; IF NotFound THEN LEAVE cursorLoop; END IF; … END LOOP;

47 Example: Cursor uLet’s write a procedure that examines Sells(bar, beer, price), and raises by $1 the price of all beers at Joe’s Bar that are under $3. - Yes, we could write this as a simple UPDATE, but the details are instructive anyway.

48 The Needed Declarations CREATE PROCEDURE JoeGouge( ) DECLARE theBeer CHAR(20); DECLARE thePrice REAL; DECLARE NotFound CONDITION FOR SQLSTATE ’02000’; DECLARE c CURSOR FOR (SELECT beer, price FROM Sells WHERE bar = ’Joe’’s Bar’); Used to hold beer-price pairs when fetching through cursor c Returns Joe’s menu

49 The Procedure Body BEGIN OPEN c; menuLoop: LOOP FETCH c INTO theBeer, thePrice; IF NotFound THEN LEAVE menuLoop END IF; IF thePrice < 3.00 THEN UPDATE Sells SET price = thePrice WHERE bar = ’Joe’’s Bar’ AND beer = theBeer; END IF; END LOOP; CLOSE c; END; Check if the recent FETCH failed to get a tuple If Joe charges less than $3 for the beer, raise it’s price at Joe’s Bar by $1.

50 uFunction alternative: CREATE FUNCTION ( ) RETURNS

51 example The example could be the export of a group of tables from a database to text files using bcp. Create Procedure prBcpOutTables --loop through tables and export them to text int = 0 As varchar(255) Cursor = Cursor FOR select name from sysobjects where xType = 'U'

52 -- get first table Fetch Next -- if we successfully read the current record While = 0) Begin -- assemble DOS command for exporting table = 'bcp "Asset..[' + ']" out C:\sql7\backup\' + '.txt -c -q -Sdejan -Usa -Pdejan' -- during test just display command <> 0 chvCommand -- in production execute DOS command and export table = 0 Execute NO_OUTPUT Fetch Next End Return 0