Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

09 qmds2005 session13

485 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

09 qmds2005 session13

  1. 1. Querying and Managing Data Using SQL Server 2005Objectives In this session, you will learn to: Implement stored procedures Implement functions Ver. 1.0 Session 13 Slide 1 of 19
  2. 2. Querying and Managing Data Using SQL Server 2005Creating Stored Procedures 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… Ver. 1.0 Session 13 Slide 2 of 19
  3. 3. Querying and Managing Data Using SQL Server 2005Creating Stored Procedures (Contd.) 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… Ver. 1.0 Session 13 Slide 3 of 19
  4. 4. Querying and Managing Data Using SQL Server 2005Just a minute Which command will you use to modify the procedure? Answer: ALTER PROCEDURE Ver. 1.0 Session 13 Slide 4 of 19
  5. 5. Querying and Managing Data Using SQL Server 2005Just a minute Which system-defined table stores the names of all the stored procedure? Answer: sysobjects Ver. 1.0 Session 13 Slide 5 of 19
  6. 6. Querying and Managing Data Using SQL Server 2005Creating Parameterized Stored Procedures 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… Ver. 1.0 Session 13 Slide 6 of 19
  7. 7. Querying and Managing Data Using SQL Server 2005Returning Values from Stored Procedures 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… Ver. 1.0 Session 13 Slide 7 of 19
  8. 8. Querying and Managing Data Using SQL Server 2005Calling a Procedure from Another Procedure 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… Ver. 1.0 Session 13 Slide 8 of 19
  9. 9. Querying and Managing Data Using SQL Server 2005Demo: 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. Ver. 1.0 Session 13 Slide 9 of 19
  10. 10. Querying and Managing Data Using SQL Server 2005Demo: Creating Stored Procedures (Contd.) 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. Ver. 1.0 Session 13 Slide 10 of 19
  11. 11. Querying and Managing Data Using SQL Server 2005Creating UDFs 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. Ver. 1.0 Session 13 Slide 11 of 19
  12. 12. Querying and Managing Data Using SQL Server 2005Creating UDFs (Contd.) 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… Ver. 1.0 Session 13 Slide 12 of 19
  13. 13. Querying and Managing Data Using SQL Server 2005Creating UDFs (Contd.) 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… Ver. 1.0 Session 13 Slide 13 of 19
  14. 14. Querying and Managing Data Using SQL Server 2005Just a minute Which type of function returns a single value? Answer: Scalar functions Ver. 1.0 Session 13 Slide 14 of 19
  15. 15. Querying and Managing Data Using SQL Server 2005Demo: Creating Functions Problem Statement: As a database developer at AdventureWorks, Inc., you need to create a function that accepts the employee ID of an employee and returns the following details: • Employee ID • Name of the employee • Title of the employee • Number of other employees working under the employee How will you create the function? Ver. 1.0 Session 13 Slide 15 of 19
  16. 16. Querying and Managing Data Using SQL Server 2005Demo: Creating Functions (Contd.) Solution: To solve the preceding problem, you need to perform the following tasks: 1. Create a function. 2. Execute the function to verify the result. Ver. 1.0 Session 13 Slide 16 of 19
  17. 17. Querying and Managing Data Using SQL Server 2005Summary 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 statements and programming logic. A stored procedure provides better performance, security, and accuracy, and reduces the network congestion. 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. Ver. 1.0 Session 13 Slide 17 of 19
  18. 18. Querying and Managing Data Using SQL Server 2005Summary (Contd.) A stored procedure allows you to declare parameters, variables, and use T-SQL statements and programming logic. A stored procedure provides better performance, security, and accuracy, and reduces the network congestion. A stored procedure accepts data through input parameters. A stored procedure returns data through the output parameters or return statements. A stored procedure can be executed by using the EXECUTE statement. A stored procedure can be altered by 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. Ver. 1.0 Session 13 Slide 18 of 19
  19. 19. Querying and Managing Data Using SQL Server 2005Summary (Contd.) UDFs are of two 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 Session 13 Slide 19 of 19

×