Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 c6212 Advanced Database and Client Server MS SQL Server 2000 Stored Procedures and Parameters What ? Why ? How ?

Similar presentations


Presentation on theme: "1 c6212 Advanced Database and Client Server MS SQL Server 2000 Stored Procedures and Parameters What ? Why ? How ?"— Presentation transcript:

1 1 c6212 Advanced Database and Client Server MS SQL Server 2000 Stored Procedures and Parameters What ? Why ? How ?

2 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 3 A Stored Procedure is an Object stored in the Database table1 table2 table4 sp1 sp2 sp3 tr1 tr2tr3 view1view2

4 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 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 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 7 Enterprise Manager

8 8 How? -Templates T SQL frameworks that you can modify Found on Query Analyser top menu bar

9 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 10

11 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 12 System Stored Procedures sp_help sp_helpconstraint sp_rename sp_who rename & drop – no cascade

13 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 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 15 use of message window

16 16

17 17 Error Handling LEVEL STATE

18 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 19 Enhancing Stored Procedures Variables Input parameters Output Parameters Transactions Error Handling

20 20 Why use parameters? Simplify procedures Reuse procedures

21 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 22 Example 3 Parameters in Stored Procedures Create Procedure Pr_Insert_Three ( @ID int, @last varchar (30), @first varchar(30) ) AS INSERT INTO staff (staff_Id, last_name, first_name) VALUES (@ID, @last, @first); RETURN exec Pr_Insert_Three 333,Smith, Mary exec Pr_Insert_Three '444','Smith','Mary' exec Pr_Insert_Three @ID =555, @first=Fred, @last= Smith exec Pr_Insert_Three @ID =666, @last= 'Smith', @first='Fred', input parameters @implies variable rather than column

23 23 Input Parameters tables screen grid stored procedure input parameter values data values from tables results

24 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 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 26 Example 5 Output Parameter Create Procedure Pr_five ( @id int, @full_name varchar(30) output ) AS select @full_name = last_name + first_name from staff where staff_id = @ID RETURN

27 27 Using the output value declare @a varchar(30) exec pr_five 888, @a output print @a local variable SmithFred message window

28 28 Using a Scalar in Select declare @a varchar(30) exec pr_five 555, @a output select "Full Name " = @a declare @a varchar(30) exec pr_five 555, @a output select @a "Full Name " results in grid window

29 29

30 30 alter Procedure Pr_six ( @id int, @full_name varchar(30) output ) AS IF @id <0 BEGIN Print 'You must enter a positive value' return END select @full_name = last_name + first_name from staff where staff_id = @ID RETURN Must use BEGIN.... END Error Handling

31 31 create Procedure Pr_seven ( @id int, @full_name varchar(30) output ) AS IF @id <0 BEGIN RAISERROR ('You must enter a positive value',16,10) RETURN END select @full_name = last_name + first_name from staff where staff_id = @ID RETURN Message appears in the messages window RAISERROR

32 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 33 Web Page Set up

34 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 myCommand.Parameters.Add("@Id", SqlDbType.Int).Value = Inputparam.text myConnection.Open() DataGrid1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection) DataGrid1.DataBind() End Sub 1 2 3

35 35 Simple Stored Procedure

36 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


Download ppt "1 c6212 Advanced Database and Client Server MS SQL Server 2000 Stored Procedures and Parameters What ? Why ? How ?"

Similar presentations


Ads by Google