Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida"— Presentation transcript:

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

2 IMS 4212: Application Architecture and Intro to Stored Procedures 2 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu 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, ODBC, ADO.Net) Application connectivity –Applications

3 IMS 4212: Application Architecture and Intro to Stored Procedures 3 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu 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

4 IMS 4212: Application Architecture and Intro to Stored Procedures 4 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu Stored Procedures Stored Procedures (SP) are procedural instructions stored within the database SP can be called 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) –Output parameters (multiple discrete values)

5 IMS 4212: Application Architecture and Intro to Stored Procedures 5 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu Script to Create a Simple Stored Procedure CREATE PROCEDURE up_Organization_Update @OrgID bigint, @OrgName varchar(50), @AcctName varchar(50), @WireAcct varchar(25) AS UPDATE Organization SET OrgName = @OrgName, AcctName = @AcctName, WireAcct = @WireAcct WHERE OrgID = @OrgID GO More on this SP later

6 IMS 4212: Application Architecture and Intro to Stored Procedures 6 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu 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

7 IMS 4212: Application Architecture and Intro to Stored Procedures 7 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu 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 All business logic data access will take place in SP SP can contain any data manipulation queries

8 IMS 4212: Application Architecture and Intro to Stored Procedures 8 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu 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

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

10 IMS 4212: Application Architecture and Intro to Stored Procedures 10 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu 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 up_Organization_Update @OrgID bigint, @OrgName varchar(50), @AcctName varchar(50), @WireAcct varchar(25) AS

11 IMS 4212: Application Architecture and Intro to Stored Procedures 11 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu 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 = @OrgName, AcctName = @AcctName, WireAcct = @WireAcct WHERE OrgID = @OrgID GO

12 IMS 4212: Application Architecture and Intro to Stored Procedures 12 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu 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 RETURN(@varname) syntax If @varname is not an input parameter it must be created with the DECLARE statement DECLARE @varname datatype Use SET to assign a value to a variable @@ERROR and @@IDENTITY are common intrinsic values that are returned

13 IMS 4212: Application Architecture and Intro to Stored Procedures 13 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu SP Variables & Return Values (cont.) CREATE PROCEDURE up_Shippers_Insert @CompanyName nvarchar(40), @Phone nvarchar(24) AS DECLARE @ShipperID int --Perform the insert INSERT INTO Shippers ( CompanyName, Phone) VALUES (@CompanyName, @Phone) --Load the PK into the return parameter SET @ShipperID = @@Identity RETURN (@ShipperID) GO Parameters Declare internal variable @@Identity gives identity attribute value of most recently added record Returning the variable value

14 IMS 4212: Application Architecture and Intro to Stored Procedures 14 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu SP Variables & Return Values (cont.) Notes: –RETURN can only return a variable in SQL Server 2000 and earlier RETURN(@@Identity) works SQL 2005 & later –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 a compatible data type Ensure that varchar variables and fields are the same length –We will see how to read returned recordset values next time

15 IMS 4212: Application Architecture and Intro to Stored Procedures 15 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu 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

16 IMS 4212: Application Architecture and Intro to Stored Procedures 16 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu Exercises Create SP to: –Return a list of CategoryID and CategoryName values sorted by name –Receive a CategoryID and return all product details for products in that category –Receive a country name and return all product details for products from that country –Add a new Order Details record Hint: Be sure that you use valid OrderID and ProductID values

17 IMS 4212: Application Architecture and Intro to Stored Procedures 17 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu Alter Procedure You may also update a stored procedure using the ALTER PROCEDURE procedure_name syntax –Right click procedure name in Query Analyzer Object Browser –Select Modify from menu

18 IMS 4212: Application Architecture and Intro to Stored Procedures 18 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu Alter Procedure (cont.) SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO ALTER PROCEDURE up_Region_Insert @RegionID int, @RegionDescription nchar(50) AS INSERT INTO Region (RegionID, RegionDescription) VALUES (@RegionID, @RegionDescription) GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO Added by editor

19 IMS 4212: Application Architecture and Intro to Stored Procedures 19 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu Commenting SPs /* up_Shippers_Add * * Receives input parameters for the two non-identity attributes of * the shippers table and adds a new shippers record * Retrieves the value of the newly created record PK and * returns it as the return value of the SP */ Comment purpose of SP at top Use /*… */ to create a block comment -- at beginning of a line creates a comment on the line

20 IMS 4212: Application Architecture and Intro to Stored Procedures 20 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu Default Parameter Values A value must be provided for every parameter –But a parameter need not be provided for every field A default value can be provided in the SP which will be used if the calling application does not provide one All of the following are legal: CREATE PROCEDURE up_ProcedureName @CompanyName nvarchar(40), @OrderDate = GetDate(), @EmployeeID = 0, @ShippedDate = NULL AS Value must be provided Will use current date if no val Will use zero if no value Need not receive a value and will enter NULL if none provided

21 IMS 4212: Application Architecture and Intro to Stored Procedures 21 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu Testing SP From Query Analyzer DELETE Shippers WHERE CompanyName = 'Test Co' --Testing by adding values in list in parameter order EXEC up_Shippers_Add 'Test Co', '555-1212' GO --Testing by providing named parameter values EXEC up_Shippers_Add @CompanyName = 'Test Co', @Phone = 'Explicit' GO --Testing by adding named parameter values reversed EXEC up_Shippers_Add @Phone = 'Reverse', @CompanyName = 'Test Co' --Testing by providing no phone number with default provided EXEC up_Shippers_Add 'Test Co' SELECT * FROM Shippers

22 IMS 4212: Application Architecture and Intro to Stored Procedures 22 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu What’s To Come The Stored Procedures we will build here can easily be invoked from our Visual Basic programs Syntax is a little complex but is always the same for the same kind of SP Learn VB syntax (mimic the example code) and the applications are straightforward to implement


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

Similar presentations


Ads by Google