Slide 1 of 19Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 In this session,...

19
Slide 1 of 19 Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 In this session, you will learn to: Implement stored procedures Implement functions Objectives

Transcript of Slide 1 of 19Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 In this session,...

Page 1: Slide 1 of 19Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 In this session, you will learn to: Implement stored procedures Implement.

Slide 1 of 19Session 13Ver. 1.0

Querying and Managing Data Using SQL Server 2005

In this session, you will learn to:Implement stored procedures

Implement functions

Objectives

Page 2: Slide 1 of 19Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 In this session, you will learn to: Implement stored procedures Implement.

Slide 2 of 19Session 13Ver. 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

Page 3: Slide 1 of 19Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 In this session, you will learn to: Implement stored procedures Implement.

Slide 3 of 19Session 13Ver. 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 statementSyntax:

DROP PROCEDURE proc_name

Let’s see how…

Creating Stored Procedures (Contd.)

Page 4: Slide 1 of 19Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 In this session, you will learn to: Implement stored procedures Implement.

Slide 4 of 19Session 13Ver. 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

Page 5: Slide 1 of 19Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 In this session, you will learn to: Implement stored procedures Implement.

Slide 5 of 19Session 13Ver. 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

Page 6: Slide 1 of 19Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 In this session, you will learn to: Implement stored procedures Implement.

Slide 6 of 19Session 13Ver. 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

Page 7: Slide 1 of 19Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 In this session, you will learn to: Implement stored procedures Implement.

Slide 7 of 19Session 13Ver. 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

Page 8: Slide 1 of 19Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 In this session, you will learn to: Implement stored procedures Implement.

Slide 8 of 19Session 13Ver. 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

Page 9: Slide 1 of 19Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 In this session, you will learn to: Implement stored procedures Implement.

Slide 9 of 19Session 13Ver. 1.0

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.

Page 10: Slide 1 of 19Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 In this session, you will learn to: Implement stored procedures Implement.

Slide 10 of 19Session 13Ver. 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.)

Page 11: Slide 1 of 19Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 In this session, you will learn to: Implement stored procedures Implement.

Slide 11 of 19Session 13Ver. 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

Page 12: Slide 1 of 19Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 In this session, you will learn to: Implement stored procedures Implement.

Slide 12 of 19Session 13Ver. 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_expressionEND[ ; ]

Let’s see how…

Creating UDFs (Contd.)

Page 13: Slide 1 of 19Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 In this session, you will learn to: Implement stored procedures Implement.

Slide 13 of 19Session 13Ver. 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.)

Page 14: Slide 1 of 19Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 In this session, you will learn to: Implement stored procedures Implement.

Slide 14 of 19Session 13Ver. 1.0

Querying and Managing Data Using SQL Server 2005 Just a minute

Which type of function returns a single value?

Answer:Scalar functions

Page 15: Slide 1 of 19Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 In this session, you will learn to: Implement stored procedures Implement.

Slide 15 of 19Session 13Ver. 1.0

Querying and Managing Data Using SQL Server 2005

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?

Demo: Creating Functions

Page 16: Slide 1 of 19Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 In this session, you will learn to: Implement stored procedures Implement.

Slide 16 of 19Session 13Ver. 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 function.

2. Execute the function to verify the result.

Demo: Creating Functions (Contd.)

Page 17: Slide 1 of 19Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 In this session, you will learn to: Implement stored procedures Implement.

Slide 17 of 19Session 13Ver. 1.0

Querying and Managing Data Using SQL Server 2005

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.

Summary

Page 18: Slide 1 of 19Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 In this session, you will learn to: Implement stored procedures Implement.

Slide 18 of 19Session 13Ver. 1.0

Querying and Managing Data Using SQL Server 2005

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.

Summary (Contd.)

Page 19: Slide 1 of 19Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 In this session, you will learn to: Implement stored procedures Implement.

Slide 19 of 19Session 13Ver. 1.0

Querying and Managing Data Using SQL Server 2005

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.

Summary (Contd.)