Instructor Inputs - Session 11 pot

14 174 0
Instructor Inputs - Session 11 pot

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Instructor Inputs Session 11 ¤NIIT Instructor Inputs 11.3 This session includes section two and three of Chapter 7 of the Student Guide. Slide 1 Slide 1 of 19Session 11 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 In this session, you will learn to: Implement stored procedures Implement functions Objectives Start the session by sharing the objectives with the students. In this session, the students will learn about usage of stored procedures and functions. They will learn how to create and manage these database objects. Session Overview 11.4 Instructor Inputs ¤NIIT Slide 2 Slide 2 of 19Session 11 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 Stored procedures: Are created using the CREATE PROCEDURE statement Are executed using the EXECUTE PROCEDURE statement Syntax: CREATE PROCEDURE proc_name AS BEGIN sql_statement1 sql_statement2 END Let’s see how… Creating Stored Procedures In this topic, you need to explain the concept of stored procedures to the students. Further, you will discuss the benefits of procedures as modularity, speed, security, reduced network congestion and consistency of usage across applications and users. Tell the students that since stored procedures have so many benefits, all operations and transactions from the client such as queries, updation, insertion, and deletion of rows are done using stored procedures. Even if the insert, update, delete, or query operation is very simple, a stored procedure must be created. This improves performance of the application. Hence, programmers simply execute the stored procedures, which are stored at the backend instead of sending SQL statements from the client. If the definition of the stored procedure needs to be modified, then use the ALTER PROCEDURE statement. You can use the examples given in the Student Guide to clarify the concept to the students. Further, you can execute the following statements to explain the concept: When designing an application, stored procedures can significantly reduce the network requirements. Use stored procedures for long, complicated, and frequently repeated queries. This reduces the traffic from the client to the server because only the stored procedure name and its associated parameters are passed across the network to the server, where it is executed. In addition, multi-step queries that perform additional filtering or processing based upon the response to initial queries, run much more efficiently as a stored procedure. By using a stored procedure, it is not necessary to pass the results of the initial query to the client in order that a second query can be passed to the server. ¤NIIT Instructor Inputs 11.5 The sp_depends stored procedure can be used to find out the dependencies of a stored procedure. The syntax of the sp_depends stored procedure is: sp_depends object_name Slide 3 Slide 3 of 19Session 11 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 Stored procedure: Is modified using the ALTER PROCEDURE statement Syntax: ALTER PROCEDURE proc_name Is deleted using the DROP PROCEDURE statement Syntax: DROP PROCEDURE proc_name Let’s see how… Creating Stored Procedures (Contd.) In this topic, you will tell the students how to alter and drop a stored procedure. You can use the examples given in the Student Guide to demonstrate the concept to the students. Slide 4 Slide 4 of 19Session 11 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 Just a minute Which command will you use to modify the procedure? Answer: ALTER PROCEDURE 11.6 Instructor Inputs ¤NIIT Reiterate the concepts taught in the preceding slides by asking the question. Slide 5 Slide 5 of 19Session 11 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 Just a minute Which system-defined table stores the names of all the stored procedure? Answer: sysobjects Reiterate the concepts taught in the preceding slides by asking the question. Slide 6 Slide 6 of 19Session 11 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 Parameterized stored procedures: Are used to pass values to the stored procedure during the run time Involve declaring variables and passing value to it, which is defined as input parameter Let’s see how… Creating Parameterized Stored Procedures In this topic, you need to explain about parameterized stored procedures. After explaining the two types of parameters, tell the students that a procedure with output parameters is typically executed from another calling procedure. ¤NIIT Instructor Inputs 11.7 Mention that the RETURN keyword is used when a single value needs to be returned to the calling program. Output parameters are used when multiple values of any data type have to be returned. Tell the students that apart from explicitly executing a stored procedure it can be executed automatically, for example, at the start-up of SQL Server. This can be done by using a system stored procedure called sp_procoption for only objects of master database, which are owned by dbo. The syntax for sp_procoption procedure is: sp_procoption @ProcName = 'procedure', @OptionName = 'option', @OptionValue = 'value' where, @ProcName defines the procedure for which to set the option. @OptionName defines the option to set for the procedure that can only be ‘startup’, which will set the procedure for autoexecution. @OptionValue defines whether to set the option as true or false. Slide 7 Slide 7 of 19Session 11 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 Stored procedure: Can also return values as output Uses the OUPUT keyword to specify the parameter as output parameter Syntax: CREATE PROCEDURE procedure_name [ {@parameter data_type} [OUTPUT] ] AS sql_statement [ n] Let’s see how… Returning Values from Stored Procedures Tell the students that you can return values from a stored procedure using the RETURN statement or using the output variables. Explain the usage of both the types of methods. 11.8 Instructor Inputs ¤NIIT Slide 8 Slide 8 of 19Session 11 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 Stored procedure: Can use the values returned by a procedure inside another procedure That calls or executes another procedure is known as the calling procedure That is called or executed by the calling procedure is known as the called procedure Let’s see how… Calling a Procedure from Another Procedure Explain the need to call a procedure from another. You can tell that this helps in reusing the code. Tell that whenever a procedure gets called from within another procedure you have to use the EXEC keyword. Slide 9 Slide 9 of 19Session 11 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 Demo: Creating Stored Procedures Problem Statement: You are a database developer of AdventureWorks, Inc. The Human Resource department needs to revise the payment details of the employees. You need to create a procedure that obtains the percentage value by which you need to increase the pay rate. In addition, you need to ensure that the pay is revised for only those employees whose pay rate was not revised in the last six months. At the end of this demo, students will be able to create and execute a stored procedure. ¤NIIT Instructor Inputs 11.9 Slide 10 Slide 10 of 19Session 11 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 Solution: To solve the preceding problem, you need to perform the following tasks: 1. Create a stored procedure. 2. Execute the stored procedure. 3. Verify the result. Demo: Creating Stored Procedures (Contd.) Slide 11 Slide 11 of 19Session 11 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 Scalar functions include the following components: Function name with optional schema/owner name Input parameter name and data type Options applicable to the input parameter Return parameter data type and optional name Options applicable to the return parameter One or more T-SQL statements Scalar functions can be created by using the CREATE FUNCTION statement. Creating UDFs In this slide, you need to explain the user-defined functions (UDFs). Explain the utility of the UDFs and how they are different from procedures. Also state that UDFs are of two types, scalar functions and table-valued functions. Explain the utility of both types of functions. 11.10 Instructor Inputs ¤NIIT Slide 12 Slide 12 of 19Session 11 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 Syntax: CREATE FUNCTION [ schema_name. ] function_name ( [ { @parameter_name [ AS ][ type_schema_name. ] parameter_data_type [ = default ] } [ , n ] ] ) RETURNS return_data_type [ WITH <function_option> [ , n ] ] [ AS ] BEGIN function_body RETURN scalar_expression END [ ; ] Let’s see how… Creating UDFs (Contd.) Explain the syntax of the CREATE FUNCTION statement and demonstrate an example given in the Student Guide. Slide 13 Slide 13 of 19Session 11 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 Table-valued functions: Returns a table as an output, which can be derived as a part of a SELECT statement Uses the table data type to store the set of rows Are of following two types: Inline table-valued function Multistatement table-valued function Let’s see how… Creating UDFs (Contd.) In this slide, you need to explain the students about the types of table-valued UDFs. Use the examples given in the Student Guide to demonstrate the concepts. Mention that the error handling strategy is similar in both the procedures and functions, as told in batches. [...]... various T-SQL statements that are stored under one name and are executed as a single unit A stored procedure can be created using the CREATE PROCEDURE statement Ver 1.0 Session 11 Slide 17 of 19 Summarize the session 11. 12 Instructor Inputs NIIT Slide 18 Querying and Managing Data Using SQL Server 2005 Summary (Contd.) A stored procedure allows you to declare parameters, variables, and use T-SQL statements... employee Title of the employee Number of other employees working under the employee How will you create the function? Ver 1.0 Session 11 Slide 15 of 19 You need to ensure that after this demo, the students are able to create and execute userdefined functions NIIT Instructor Inputs 11. 11 Slide 16 Querying and Managing Data Using SQL Server 2005 Demo: Creating Functions (Contd.) Solution: To solve the preceding... using the ALTER PROCEDURE statement A user-defined function is a database object that contains a set of T-SQL statements The user-defined functions can return either a single scalar value or a result set Session 11 Ver 1.0 Slide 18 of 19 Slide 19 Querying and Managing Data Using SQL Server 2005 Summary (Contd.) UDFs are of two types: scalar functions and table-valued functions A scalar function accepts... types: scalar functions and table-valued functions A scalar function accepts a single value and returns a single value A table-valued function returns a table as an output, which can be derived as a part of a SELECT statement Ver 1.0 NIIT Session 11 Slide 19 of 19 Instructor Inputs 11. 13 FAQs 1 What is use of WITH RECOMPILE option of the CREATE PROCEDURE statement? Ans: If data changes and indexes are... 1.0 Session 11 Slide 16 of 19 Slide 17 Querying and Managing Data Using SQL Server 2005 Summary In this session, you learned that: A stored procedure is a collection of various T-SQL statements that are stored under one name and are executed as a single unit A stored procedure can be created using the CREATE PROCEDURE statement A stored procedure allows you to declare parameters, variables, and use T-SQL... procedure is closed, no further execution of the global temporary stored procedure is allowed Only those connections that have already started executing the stored procedure are allowed to complete 11. 14 Instructor Inputs NIIT ...Slide 14 Querying and Managing Data Using SQL Server 2005 Just a minute Which type of function returns a single value? Answer: Scalar functions Session 11 Ver 1.0 Slide 14 of 19 Reiterate the learning by asking the given question Slide 15 Querying and Managing Data Using SQL Server 2005 Demo: Creating Functions Problem Statement: As a database developer... What are temporary stored procedures? Ans: A temporary stored procedure is a procedure that is stored in the tempdb database They are useful when you do not need support the reuse of execution plans for T-SQL statements They are denoted by the # and ## prefixes to the stored procedure names # denotes a local temporary stored procedure; ## denotes a global temporary stored procedure These procedures do . Instructor Inputs Session 11 ¤NIIT Instructor Inputs 11. 3 This session includes section two and three of Chapter 7 of the Student Guide. Slide 1 Slide 1 of 1 9Session 11 Ver. 1.0 Querying. learn how to create and manage these database objects. Session Overview 11. 4 Instructor Inputs ¤NIIT Slide 2 Slide 2 of 1 9Session 11 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 Stored. types, scalar functions and table-valued functions. Explain the utility of both types of functions. 11. 10 Instructor Inputs ¤NIIT Slide 12 Slide 12 of 1 9Session 11 Ver. 1.0 Querying and Managing

Ngày đăng: 31/07/2014, 15:20

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan