Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter Name By Adrienne Watt

Similar presentations


Presentation on theme: "Chapter Name By Adrienne Watt"— Presentation transcript:

1 Chapter Name By Adrienne Watt
September 98 Stored Procedures By Adrienne Watt

2 Benefits of Stored Procedures
Stored procedures are vital to any database scheme. Database developers, as well as database administrators, often write their own stored procedures to run commonly performed administrative tasks or to apply a complex business rule. These types of procedures can contain control-of- flow structures, data modifications or data retrieval statements, cursors, and error-handling statements.

3 Benefits of Stored Procedures
Stored procedures can be Simple – one Select statement Complex – multiple Select statements using control-of- flow statements. Benefits: Speed – optimized Share application logic – small sp can be incorporated with other sp Security – give Execute rights only – no table access

4 Benefits of Stored Procedures
Utilizing stored procedures has many benefits, including: Faster execution due to precompiled code. Encapsulated business rules. Increased security. Greater modularity.

5 Create Stored Procedures
You can create stored procedures using the CREATE PROCEDURE statement. Before creating a stored procedure, consider that: CREATE PROCEDURE statements cannot be combined with other SQL statements in a single batch. You can create a stored procedure in the current database. Objects that you reference must exist when you execute the stored procedure.  

6 Create Stored Procedures
Creates a stored procedure (a precompiled collection of SQL statements) that can take and/or return user- supplied parameters. CREATE PROCedure [owner.]procedure_name [(parameter1 [, parameter2]...[parameter255])] AS sql_statements

7 Creating Stored procedure
Start and login to SQL Server Management Studio. Select the pubs database. Click on New query button. In the Query window, type the following statement CREATE PROCEDURE spshwAuthors AS SELECT au_lname, au_fname FROM AUTHORS

8 Create Stored Procedures

9 Create Stored Procedures
This stored procedure returns all authors (first and last names supplied), their titles, and their publishers from a four-table join. This stored procedure does not use any parameters. Can be a view. CREATE PROCEDURE au_info_all AS SELECT au_lname, au_fname, title, pub_name FROM authors a INNER JOIN titleauthor ta ON a.au_id = ta.au_id INNER JOIN titles t ON t.title_id = ta.title_id INNER JOIN publishers p ON t.pub_id = p.pub_id GO

10 Create Stored Procedures
This stored procedure returns only the specified authors (first and last names supplied), their titles, and their publishers from a four-table join. This stored procedure accepts exact matches for the parameters passed. CREATE PROC au_info varchar(20) ) AS SELECT au_lname, au_fname, title, pub_name FROM authors, titles, publishers, titleauthor WHERE au_fname AND au_lname AND authors.au_id = titleauthor.au_id AND titles.title_id = titleauthor.title_id AND titles.pub_id = publishers.pub_id

11 Stored Procedures The au_info stored procedure can be executed in these ways: EXECUTE au_info 'Dull', 'Ann' -- Or EXECUTE = = 'Ann' EXECUTE = = 'Dull'

12 Stored Procedure with If statements
CREATE PROCEDURE SimpleProc AS int varchar(255) = count(*) FROM authors WHERE city = 'Oakland' > 0 = + ' authors live in Oakland' Else = 'Oakland is culturally deprived'

13 If..Else & Begin..End CREATE PROCEDURE Proc2 AS varchar(255) IF (SELECT COUNT(title_id) FROM titles WHERE price BETWEEN 10 AND 20) > 0 BEGIN = 'There are several books that are a good value between $10 and $20. These books are: ' SELECT title FROM titles WHERE price BETWEEN 10 AND 20 END ELSE = 'There are no books between $10 and $20. You might consider the following books that are under $10.' WHERE price < 10

14 If..Else & Begin..End CREATE PROC money AS varchar(255) IF (SELECT COUNT(title_id) FROM titles WHERE type AND price > 0 BEGIN = 'There are several books that are a good value between the prices you chose. These books are: ‘ SELECT title FROM titles WHERE type AND price END ELSE SET NOCOUNT ON = 'There are no books between the prices you chose. You might consider the following books.' SELECT title FROM titles WHERE type AND price

15 Output CREATE PROCEDURE smallint OUTPUT AS = GO smallint EXEC newsum 2, 4, 6, OUTPUT SELECT ‘The sum of these numbers is:


Download ppt "Chapter Name By Adrienne Watt"

Similar presentations


Ads by Google