1 c6212 Advanced Database and Client Server MS SQL Server 2000 Stored Procedures and Parameters What ? Why ? How ?
2 What ? A named script made up of SQL & T-SQL statements Can call other procedures Can have input and / or output parameters Is stored in the database May be user defined or system provided Provides a good mechanism for error handling
3 A Stored Procedure is an Object stored in the Database table1 table2 table4 sp1 sp2 sp3 tr1 tr2tr3 view1view2
4 What ? The procedure's name is stored in the SysObjects Table The text is stored in the SysComments Table The execution plan is stored in the procedure cache May be external: Extended SP's – DLL's
5 Why ? A T-SQL script is compiled and optimised every time it is run. A Stored Procedure is compiled,optimised and stored. A Stored Procedure is much more efficient Can be reused. Provides a programming framework Provides security features
6 How ? -EM In Enterprise Manager: select your database, then right click on Stored Procedures and select New Stored Procedure CREATE PROCEDURE ex_1 AS SELECT * FROM Staff;
7 Enterprise Manager
8 How? -Templates T SQL frameworks that you can modify Found on Query Analyser top menu bar
9 How ? Use the Object Browser Icon on Query Analyser top menu bar Tools menu (show / hide) or F8 to toggle Right Click / Edit
10
11 How ? Directly –EXECUTE proc_name –EXEC ex_1 Use within another command –INSERT INTO tablename EXEC proc_name (refresh F5 / right click on database)
12 System Stored Procedures sp_help sp_helpconstraint sp_rename sp_who rename & drop – no cascade
13 Example 1 Create Procedure Pr_Insert_One AS INSERT INTO staff (staff_Id, last_name, first_name) VALUES (1, 'Anderson', 'Neo'); RETURN Exec Pr_Insert_One
14 Example 2 Create Procedure Pr_Insert_Two AS INSERT INTO staff (staff_Id, last_name, first_name) VALUES (2, 'Smith', 'Mr'); RETURN EXEC Pr_Insert_Two
15 use of message window
16
17 Error Handling LEVEL STATE
18 Using Stored Procedures from a Web site Create the procedure in your SQL Server database. Create a web page to –connect to the server –execute the stored procedure Enhance the web page to send values to the database (input parameters) Enhance the web page to receive & display results (output parameters) from the database
19 Enhancing Stored Procedures Variables Input parameters Output Parameters Transactions Error Handling
20 Why use parameters? Simplify procedures Reuse procedures
21 Example 1 Example 2 Create Procedure Pr_Insert_One AS INSERT INTO staff (staff_Id, last_name, first_name) VALUES (1, 'Anderson', 'Neo'); RETURN Exec Pr_Insert_One Create Procedure Pr_Insert_Two AS INSERT INTO staff (staff_Id, last_name, first_name) VALUES (2, 'Smith', 'Mr'); RETURN EXEC Pr_Insert_Two
22 Example 3 Parameters in Stored Procedures Create Procedure Pr_Insert_Three varchar varchar(30) ) AS INSERT INTO staff (staff_Id, last_name, RETURN exec Pr_Insert_Three 333,Smith, Mary exec Pr_Insert_Three '444','Smith','Mary' exec =555, Smith exec input variable rather than column
23 Input Parameters tables screen grid stored procedure input parameter values data values from tables results
24 Input & Output Parameters tables stored procedure 1 input parameter values data values from tables output parameters may become input parameters stored procedure 2
25 Input & Output Parameters tables stored procedure 1 input parameter values data values from tables output parameters may become input parameters stored procedure 2
26 Example 5 Output Parameter Create Procedure Pr_five varchar(30) output ) AS = last_name + first_name from staff where staff_id RETURN
27 Using the output value varchar(30) exec pr_five output local variable SmithFred message window
28 Using a Scalar in Select varchar(30) exec pr_five output select "Full Name " varchar(30) exec pr_five output "Full Name " results in grid window
29
30 alter Procedure Pr_six varchar(30) output ) AS <0 BEGIN Print 'You must enter a positive value' return END = last_name + first_name from staff where staff_id RETURN Must use BEGIN.... END Error Handling
31 create Procedure Pr_seven varchar(30) output ) AS <0 BEGIN RAISERROR ('You must enter a positive value',16,10) RETURN END = last_name + first_name from staff where staff_id RETURN Message appears in the messages window RAISERROR
32 Using Parameters from a Web Page Web PageWeb Server DBMS VB Script including SP name +parameter values SP name +parameter values return values
33 Web Page Set up
34 Sub Page_Load(Sender As Object, E As EventArgs) ' TODO: Update the ConnectionString for your application Dim ConnectionString As String = "server=Galba;database=Northwind;uid=******;pwd=******;" ' TODO: Updatd the name of the Stored Procedure for your application Dim CommandText As String = "CustOrdersDetail" Dim myConnection As New SqlConnection(ConnectionString) Dim myCommand As New SqlCommand(CommandText, myConnection) Dim workParam As New SqlParameter() myCommand.CommandType = CommandType.StoredProcedure ' TODO: Set the input parameter, if necessary, for your application SqlDbType.Int).Value = Inputparam.text myConnection.Open() DataGrid1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection) DataGrid1.DataBind() End Sub 1 2 3
35 Simple Stored Procedure
36 Stored Procedures What ? –named pieces of SQL & T-SQL code Why ? –compiled & stored in the database –reusable How ? –EM –QA –Web page Why use parameters? Simplify procedures Reuse procedures