IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida

Slides:



Advertisements
Similar presentations
Chapter 9: Advanced SQL and PL/SQL Topics Guide to Oracle 10g.
Advertisements

Module 9: Implementing Stored Procedures. Introduction to Stored Procedures Creating Executing Modifying Dropping Using Parameters in Stored Procedures.
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
Concepts of Database Management Sixth Edition
A Guide to SQL, Seventh Edition. Objectives Embed SQL commands in PL/SQL programs Retrieve single rows using embedded SQL Update a table using embedded.
A Guide to MySQL 3. 2 Objectives Start MySQL and learn how to use the MySQL Reference Manual Create a database Change (activate) a database Create tables.
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
Introduction to PL/SQL Lecture 0 – Self Study Akhtar Ali.
8/6/ ITE 370: SQL Stored Procedures. 8/6/ Stored Procedures A stored procedure is A stored procedure is a collection of SQL statements saved.
1 Nassau Community CollegeProf. Vincent Costa Acknowledgements: Introduction to Database Management, All Rights ReservedIntroduction to Database Management.
Module 2: Using Transact-SQL Querying Tools. Overview SQL Query Analyzer Using the Object Browser Tool in SQL Query Analyzer Using Templates in SQL Query.
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
ASP.NET Programming with C# and SQL Server First Edition
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
Introduction to Databases Chapter 7: Data Access and Manipulation.
IMS 6217: Data Warehousing / Business Intelligence Part 3 1 Dr. Lawrence West, Management Dept., University of Central Florida Analysis.
Chapter 4 The Relational Model 3: Advanced Topics Concepts of Database Management Seventh Edition.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Tom Castiglia Hershey Technologies
Stored Procedures, Transactions, and Error-Handling
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Module 8: Implementing Stored Procedures. Introducing Stored Procedures Creating, Modifying, Dropping, and Executing Stored Procedures Using Parameters.
Module 1: Introduction to Transact-SQL
T-SQL Transact-SQL is microsoft implementation of SQL. It contains additional programming constracts T-SQL enables you to write programs that contain SQL.
Introduction to MySQL Lab no. 10 Advance Database Management System.
CSC 2720 Building Web Applications Database and SQL.
IMS 4212: Intro to SQL 1 Dr. Lawrence West, Management Dept., University of Central Florida Introduction to SQL—Topics Introduction to.
Copyright © Curt Hill Stored Procedures In Transact-SQL.
Stored Procedure. Objective At the end of the session you will be able to know :  What are Stored Procedures?  Create a Stored Procedure  Execute a.
Stored Procedures Week 9. Test Details Stored Procedures SQL can call code written in iSeries High Level Languages –Called stored procedures SQL has.
Christopher M. Pascucci.NET Programming: Databases & ADO.NET.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Module 8: Implementing Stored Procedures. Overview Implementing Stored Procedures Creating Parameterized Stored Procedures Working With Execution Plans.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 8 Advanced SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida Additional Data Manipulation Statements INSERT.
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Chapter 9: Advanced SQL and PL/SQL Guide to Oracle 10g.
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.
Understanding Databases Lesson 6. Objective Domain Matrix Skills/ConceptsMTA Exam Objectives Understanding Relational Database Concepts Understand relational.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
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: Data Modeling—Business Rules 1 Dr. Lawrence West, Management Dept., University of Central Florida Business Rules—Topics Intro.
IMS 4212: Intro to Multi-Table SELECT Statements 1 Dr. Lawrence West, MIS Dept., University of Central Florida Multi-Table SELECT Statements—Topics.
IMS 4212: Constraints & Triggers 1 Dr. Lawrence West, Management Dept., University of Central Florida Stored Procedures in SQL Server.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
IMS 3253: Validation and Errors 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Validation and Error Handling Validation.
Create Stored Procedures and Functions Database Management Fundamentals LESSON 2.4.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
1. Advanced SQL Functions Procedural Constructs Triggers.
1 c6212 Advanced Database and Client Server MS SQL Server 2000 Stored Procedures and Parameters What ? Why ? How ?
3 A Guide to MySQL.
Chapter 5 Introduction to SQL.
Presented by: Teererai Marange
A Guide to SQL, Seventh Edition
Task oriented processing
Database Systems: Design, Implementation, and Management Tenth Edition
Database Application Development
PL/SQL Scripting in Oracle:
ORACLE SQL Developer & SQLPLUS Statements
CIS16 Application Programming with Visual Basic
Contents Preface I Introduction Lesson Objectives I-2
Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Tenth Edition
Chapter 11 Managing Databases with SQL Server 2000
CS4540 Special Topics in Web Development SQL and MS SQL
Database Application Development
Presentation transcript:

IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida Stored Procedures in SQL Server Architecture Overview Designing and Creating SP SP Variables and Return Values Odds ‘n’ Ends

IMS 4212: Application Architecture and Intro to Stored Procedures 2 Dr. Lawrence West, Management Dept., University of Central Florida Architecture Elements A full-fledged systems production and operation environment will consist of a multitude of elements –Databases Tables, relationships, indices Stored procedures –Database connectivity System connectivity (DSN, ADO.Net) Application connectivity –Applications

IMS 4212: Application Architecture and Intro to Stored Procedures 3 Dr. Lawrence West, Management Dept., University of Central Florida Architecture Elements (cont.) Next few weeks will be dealing with these elements It will be important that you understand individual topics in the context of this framework

IMS 4212: Application Architecture and Intro to Stored Procedures 4 Dr. Lawrence West, Management Dept., University of Central Florida Stored Procedures Stored Procedures (SP) are procedural instructions stored within the database SP can be called accessed by other SP or by external applications Simplest SP execute a single SQL statement SP can be incredibly complex SP can accept variable values SP can return results –Individual discrete values –Entire recordsets (query results)

IMS 4212: Application Architecture and Intro to Stored Procedures 5 Dr. Lawrence West, Management Dept., University of Central Florida Script to Create a Simple Stored Procedure if exists (select name from sysobjects where name = 'up_Organization_Update' and type = 'P') drop procedure up_Organization_Update GO CREATE PROCEDURE varchar(25) AS UPDATE Organization SET OrgName AcctName WireAcct WHERE OrgID GO More on this SP later

IMS 4212: Application Architecture and Intro to Stored Procedures 6 Dr. Lawrence West, Management Dept., University of Central Florida SP Advantages Centralized –Any application can access the SP because it is stored with the database –Maintenance takes place in one location Fast!! –DB compiles SP and develops an ‘execution plan’ the first time the SP is run –Subsequent runs are as fast as they can be Secure –SP logic is hidden from anyone who does not have permissions to view the object Really Important

IMS 4212: Application Architecture and Intro to Stored Procedures 7 Dr. Lawrence West, Management Dept., University of Central Florida Designing SP Most tables will need INSERT INTO and UPDATE SP Identify other SP needed for your application’s business logic –DELETE –Specialized SELECT queries Retrieve an individual record by some criteria Retrieve a collection of records by a criteria Retrieve all records in a table Perform multi-table join queries Perform aggregate queries

IMS 4212: Application Architecture and Intro to Stored Procedures 8 Dr. Lawrence West, Management Dept., University of Central Florida Designing SP (cont.) Naming Stored Procedures –All begin with “up” for User Procedure –Rest of name should give purpose of query –Single table procedures should be “up_tablename_purpose” –Examples up_customers_insert up_customers_selectbyCustID up_monthlysalesdetail_bymonth SP appear alphabetically in Enterprise Manager

IMS 4212: Application Architecture and Intro to Stored Procedures 9 Dr. Lawrence West, Management Dept., University of Central Florida Creating SP SP are created in Enterprise Manager Executing the SP creation command in the Query Analyzer creates the SP as an object in the DB SP can be modified in the Enterprise Manager or modified in the Query Analyzer and recreated Be sure to save your SP files from Query Analyzer so they can be modified and rerun if necessary Demonstration

IMS 4212: Application Architecture and Intro to Stored Procedures 10 Dr. Lawrence West, Management Dept., University of Central Florida Creating SP (cont.) CREATE PROCEDURE procedure_name [parameter list] AS –Creates procedure –Parameter names must start with –Parameters are typed with SQL Server data types –Parameters should match field types CREATE PROCEDURE varchar(25) AS

IMS 4212: Application Architecture and Intro to Stored Procedures 11 Dr. Lawrence West, Management Dept., University of Central Florida Creating SP (cont.) Body of procedure executes logic Parameters are used like variables in the SQL statements Note that there are no delimiters (single quotes for text or #-signs for dates) around these values UPDATE Organization SET OrgName AcctName WireAcct WHERE OrgID GO

IMS 4212: Application Architecture and Intro to Stored Procedures 12 Dr. Lawrence West, Management Dept., University of Central Florida SP Variables and Return Values When SP create a recordset with an SQL SELECT statement that recordset is available to the calling procedure or application (more later) SP may return a value with the syntax is not an input parameter it must be created with the DECLARE statement datatype Use SET to assign a value to a variable and are common intrinsic values that are returned

IMS 4212: Application Architecture and Intro to Stored Procedures 13 Dr. Lawrence West, Management Dept., University of Central Florida SP Variables & Return Values (cont.) CREATE PROCEDURE varchar(25) AS bigint --Perform the insert INSERT INTO Organization ( OrgName, AcctName, --Load the PK into the return parameter = RETURN GO Parameters Declare internal variable gives identity attribute value of most recently added record Returning the variable value

IMS 4212: Application Architecture and Intro to Stored Procedures 14 Dr. Lawrence West, Management Dept., University of Central Florida SP Variables & Return Values (cont.) Notes: –RETURN can only return a variable won’t work –Pay careful attention to data types When a parameter variable or internal variable interacts directly with a table field the field and the variable must be of the same data type Ensure that varchar variables and fields are the same length –We will see how to read returned recordset values next time

IMS 4212: Application Architecture and Intro to Stored Procedures 15 Dr. Lawrence West, Management Dept., University of Central Florida Testing SP Test SP in Enterprise Mgr. with the Exec statement Exec SP_Name [parameter list] [Parameter List] is a list of values, one for each parameter in the SP, in the specified order. Must have delimiters if applicable Enter in E.M.: –EXEC up_Organization_Insert ‘Test Org’, ‘Test Acct’, ‘12345’ –SELECT * FROM Organization CREATE PROCEDURE varchar(25) AS bigint --Perform the insert INSERT INTO Organization ( OrgName, AcctName, --Load the PK into the return parameter = RETURN GO

IMS 4212: Application Architecture and Intro to Stored Procedures 16 Dr. Lawrence West, Management Dept., University of Central Florida Testing SP (cont.) SP may also be tested with named parameters from an Enterprise Manager query window EXEC = = = = = 0

IMS 4212: Application Architecture and Intro to Stored Procedures 17 Dr. Lawrence West, Management Dept., University of Central Florida Default Parameter Values CREATE PROCEDURE smallint = real = 0 AS --Decrement QOH in Products table UPDATE Products SET UnitsInStock = UnitsInStock WHERE ProductID --Insert into [Order Details] INSERT INTO [Order @Discount) Execution need not provide values for all parameters (but may) EXEC = = = 14

IMS 4212: Application Architecture and Intro to Stored Procedures 18 Dr. Lawrence West, Management Dept., University of Central Florida Output Parameters Use Output param to return values ALTER PROCEDURE smallint = real = smallint Output AS --Decrement quantity on hand in Products table UPDATE Products SET UnitsInStock = UnitsInStock WHERE ProductID -- Return QOH After Update = ( SELECT UnitsInStock FROM Products WHERE ProductID Illustrating calling the procedure to the left from EM smallint EXEC = = = OUTPUT May also declare parameters in.VB code to be of ParameterDirection.Output

IMS 4212: Application Architecture and Intro to Stored Procedures 19 Dr. Lawrence West, Management Dept., University of Central Florida Flow Control with If [Else] ALTER PROCEDURE smallint = real = smallint Output AS bit --Decrement quantity on hand in Products table UPDATE Products SET UnitsInStock = UnitsInStock WHERE ProductID -- Return QOH After Update = ( SELECT UnitsInStock FROM Products WHERE ProductID = (SELECT Count(*) FROM [Order Details] WHERE OrderID AND ProductID = 0 BEGIN --Perform the insert into [Order Details] table INSERT INTO [Order @Discount) END ELSE BEGIN -- Update existing record UPDATE [Order Details] SET Quantity = Quantity WHERE OrderID AND ProductID END

IMS 4212: Application Architecture and Intro to Stored Procedures 20 Dr. Lawrence West, Management Dept., University of Central Florida User Defined Functions CREATE FUNCTION [dbo].[udf_Orders_LastCustOrderDate ] nchar(5) ) RETURNS datetime AS BEGIN -- Declare the return datetime variable datetime -- Select the last date into the return variable = ( SELECT Max(OrderDate) FROM Orders WHERE CustomerID -- Return the result of the function END Using the Function in a SP –Must use the dbo.function_name format –May have multiple parameters just like a VB function SELECT CustomerID, CompanyName, dbo.udf_Orders_LastCustOrderDate(Cu stomerID) AS [Last Order] FROM Customers WHERE dbo.udf_Orders_LastCustOrderDate(Cu stomerID) < '1/1/2007'

IMS 4212: Application Architecture and Intro to Stored Procedures 21 Dr. Lawrence West, Management Dept., University of Central Florida More on SP SP can actually be incredibly rich procedural code using T-SQL (transact SQL) –Conditional execution –Looping execution –Branching execution –Calling other SP (reusable logic modules) Oracle and other DB have similar capabilities Most common SP execute discrete DB activities based around SELECT, INSERT INTO, UPDATE, and DELETE statements

IMS 4212: Application Architecture and Intro to Stored Procedures 22 Dr. Lawrence West, Management Dept., University of Central Florida More on SP (cont.) It is absolutely imperative that you practice these steps –We are now moving into programming technologies –You must be comfortable with each step –You will be combining many elements in your applications (see figure on slide #3) Next: –Using VB to connect to SQL Server using SP –Working with recordsets created by a SP SELECT statement