Previous | Table of Contents | Next |
You can return information from a procedure to a calling program, a client application, or a user in any of five ways. Its important to understand what those return methods are for, and how they are different.
Today youve learned all the syntax associated with writing and executing stored procedures. Here is a quick summary of the syntax:
create proc[edure] <procedure-name> [ ( @parameter-name datatype [= default-value] [output] [, ]) ] as <SQL statements> return [return-status] [exec[ute]] [return-status = ] procedure-name [@parameter-name =] parameter-value [output] [, ]
Tomorrow we will look at some detailed programming issues associated with stored procedures and then analyze a couple of sample procedures. Until then, here is some general advice about writing stored procedures.
Do......Dont
|
Q Where are stored procedures stored?
A A stored procedure is a database object, so it is stored in the database in which it is created. There are references to a stored procedure in several system tables, including sysobjects (one line per procedure), syscolumns (one line per parameter), sysdepends (one line per referenced table, view, or procedure), and syscomments (one line per block of 255 characters of source SQL).
Q Is a stored procedure faster than a batch?
A Tomorrow well look more closely at procedure performance issues. For now, Ill say, Yes, most of the time.
Q How do I get a list of all stored procedures in the system?
A On a database-by-database basis, you can execute this statement to retrieve a list of stored procedures:
select name from sysobjects where type = P
Q How do I get a list of parameters for a procedure?
A Use sp_help procedure-name.
Q Can I write a system stored procedure?
A Yes. You will learn how tomorrow.
Q What does group number refer to?
A If you try to create a procedure whose name already exists, you get this error message:
Msg 2729, Level 16, State 1 Object oops group number 1 already exists in the database. Choose another procedure name
Group numbering allows you to create sets of procedures with the same name, but different sequence numbers. For instance, to add a procedure oops to group 2, I would execute:
create proc oops;2 as
This creates a maintenance nightmare because (1) you cant run any stored procedures against the procedure, and (2) you have to drop all of the procedures belonging to a group at once.
Dont mess with it its ugly.
The Workshop provides quiz questions to help you solidify your understanding of the material covered, as well as exercises to provide you with experience in using what youve learned. Try to understand the quiz and exercise answers before continuing on to the next days lesson. Answers are provided in the appendix.
create proc p1 (@x int output) as select @x = 1 return declare @out_x int execute p1 @out_x select @out_x
create proc p2 as declare @x int select @x = 1 if @x = 1 return 1 else return 0 declare @retstat int execute p2 select @retstat return
select avg(sum(qty * price)) from sales s, titles t where stor_id = 7131 and t.title_id = s.title_id group by ord_num
Previous | Table of Contents | Next |