Malinda Ramadhani

38
Using Stored procedure with mySQL and PHP Writing external scripts to perform complex data handling is a tedious affair. The best way to automate tasks straightaway into the server is by using Stored Procedures. It is very useful to make them as flexible as possible, as it facilitates easy identification of any errors and can be used for executing a variety of tasks as well. What are Stored Procedures? Stored procedures are set of SQL commands that are stored in the database data server. After the storing of the commands is done, the tasks can be performed or executed continuously, without being repeatedly sent to the server. This also helps in decreasing the traffic in the networks and also reduces the CPU load. There are many advantages of using stored procedures, which include: The functionality is application and platform related Functionality has to be developed only once, and all applications can call the same commands Task execution becomes easier and less complicated Network Traffic reduced to a greater extent Centralization of all commands made possible, which is helpful for various applications that repeatedly call the same set of complicated commands Runs on any kind of environment MySQL Stored Procedures For few years, Oracle and Microsoft SQL servers were having one upper hand over MySQL by having the facility to use the advantage of Stored Procedures. But this advantage has become a thing of the past now. With MySQL 5, you can use Stored Procedures the way you have been utilizing with other servers. The syntax for using Stored Procedures is as follows: Syntax for Stored Procedures

description

Compiled By Malinda Ramadhani

Transcript of Malinda Ramadhani

Page 1: Malinda Ramadhani

Using Stored procedure with mySQL and PHP

Writing external scripts to perform complex data handling is a tedious affair. The best way to automate tasks straightaway into the server is by using

Stored Procedures. It is very useful to make them as flexible as possible, as it facilitates easy identification of any errors and can be used for executing

a variety of tasks as well.

What are Stored Procedures?

Stored procedures are set of SQL commands that are stored in the database data server. After the storing of the commands is done, the tasks can be

performed or executed continuously, without being repeatedly sent to the server. This also helps in decreasing the traffic in the networks and also

reduces the CPU load.

There are many advantages of using stored procedures, which include:

The functionality is application and platform related Functionality has to be developed only once, and all applications can call the same commands Task execution becomes easier and less complicated Network Traffic reduced to a greater extent Centralization of all commands made possible, which is helpful for various applications that repeatedly call the same set of complicated

commands

Runs on any kind of environment

MySQL Stored Procedures

For few years, Oracle and Microsoft SQL servers were having one upper hand over MySQL by having the facility to use the advantage of Stored

Procedures. But this advantage has become a thing of the past now. With MySQL 5, you can use Stored Procedures the way you have been utilizing

with other servers.

The syntax for using Stored Procedures is as follows:

Syntax for Stored Procedures

CREATE    [DEFINER = { user | CURRENT_USER }]

Page 2: Malinda Ramadhani

    PROCEDURE sp_name ([proc_parameter[,...]])    [characteristic ...] routine_body CREATE    [DEFINER = { user | CURRENT_USER }]    FUNCTION sp_name ([func_parameter[,...]])    RETURNS type    [characteristic ...] routine_body    proc_parameter:    [ IN | OUT | INOUT ] param_name type    func_parameter:    param_name type type:    Any valid MySQL data type characteristic:    LANGUAGE SQL  | [NOT] DETERMINISTIC  | { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }  | SQL SECURITY { DEFINER | INVOKER }  | COMMENT 'string' routine_body:    Valid SQL procedure statement

Application

MySQL Stored Procedures can be applied in absolutely any place. Right from complex applications to simple procedures, these stored procedures can

be utilized in absolutely any place.

Few of the many places that MySQL Stored procedures can be used are:

When diverse client applications are structured using various languages in different platforms When security is of highest importance, like in financial institutions, the users and applications would have no direct access to the database

tables. This provides excellent secured environment.

When very few database servers service the client machines, thereby providing efficient performance

Though not as mature as Oracle, DB2 or the SQL Server, the MySQL Stored Procedures is definitely worth a try. If the structure of the database is the

same, the same stored procedures can be used for all.

A simple example for MySQL Stored Procedure

Page 3: Malinda Ramadhani

To calculate the area of a circle with given radius R, the following commands can be given

delimiter //

create function Area (R double) returns double

deterministic

begin

declare A double;

set A = R * R * pi();

return A;

end

//

delimiter ;

And to call it from php code to display the area of a circle with radius 22cm,

<?

$rs_area = mysql_query(“select Area(22)”);

$area = mysql_result($rs_area,0,0);

echo “The area of the circle with radius 22cm is ”.$area.” sq.cm”;

?>

Our Sites:  Tutorial Buzz  |  How To Tree  |  Recipe Voice  |  Golf Twist  |  DIY Click  |  Movie Lizard  |  Halloween Twist  

Page 5: Malinda Ramadhani

Simple Queries in SQL ServerA sample from Microsoft SQL Server 2005 A Beginner's Guideby Dusan Petkovic

This chapter and the next describe the most important Transact-SQL statement - SELECT. In this chapter you will learn how to use the SELECT statement to

perform simple queries. Every clause in this statement is described, and numerous examples using our sample database are given to demonstrate the practical use of each clause. The second part of this chapter introduces aggregate functions and the UNION operator.

In this chapter, you will learn about:

SELECT Statement: A Basic Form WHERE Clause Simple Subqueries GROUP BY Clause Aggregate Functions HAVING Clause ORDER BY Clause SELECT Statement and IDENTITY Property Set Operators CASE Expressions COMPUTE Clause Temporary Tables

Mail to a Friend

Receive updates via our RSS feed

Page 6: Malinda Ramadhani

Computed Columns

Simple SubqueriesAll previous examples in this chapter contain comparisons of column values with an expression or constant. Additionally, the Transact-SQL language offers the ability to compare column values with the result of another SELECT statement. Such SELECT statements, which are nested in the WHERE clause of another SELECT statement, are called subqueries. The first SELECT statement in a subquery is often called the outer query—in contrast to the inner query, which denotes the second SELECT statement. The inner query will always be evaluated first, and the outer query receives the values of the inner query.

Note

A subquery can also be nested in an INSERT, UPDATE, or a DELETE statement, which will be discussed in Chapter 7.

There are two types of subqueries:

Simple Correlated

In a simple subquery, the inner query is evaluated exactly once. A correlated subquery differs from a simple one in that its value depends upon a variable from the outer query. Therefore, the inner query of a correlated subquery is evaluated each time the system retrieves a new row from the outer query. The correlated subquery will be discussed in Chapter 6.

A simple subquery can be used with the following operators:

Comparison operators IN operator ANY or ALL operator EXISTS function

Subqueries and Comparison Operators

Page 7: Malinda Ramadhani

Example 5.29 shows the simple subquery that is used with the operator =.

EXAMPLE 5.29Get the first and last names of employees who work in the research department.

USE sampleSELECT emp_fname, emp_lname   FROM employee   WHERE dept_no =   (SELECT dept_no      FROM department      WHERE dept_name = ‘Research’)

The result is

emp_fname emp_lname

John Barrimore

Sybill Moser

SQL Server first evaluates the inner query. That query returns the number of the research department (d1). Thus, after the evaluation of the inner query, the subquery in Example 5.29 can be represented with the following equivalent query:

USE sampleSELECT emp_fname, emp_lname   FROM employee   WHERE dept_no = ‘d1’

A subquery can be used with other comparison operators, too. Example 5.30 shows the use of the operator <.

EXAMPLE 5.30

Page 8: Malinda Ramadhani

Get all project numbers of employees whose employee numbers are smaller than the number of the employee named Moser.

USE sampleSELECT DISTINCT project_no   FROM works_on   WHERE emp_no <   (SELECT emp_no      FROM employee      WHERE emp_lname = ‘Moser’)

The result is

project_no

p1

p2

p3

Any comparison operator can be used, provided the inner query returns exactly one row. This is obvious, because the comparison between particular column values of the outer query and a set of values (as a result of the inner query) is not possible.

Subqueries and IN OperatorThe IN operator allows the specification of a set of expressions (or constants) that are subsequently used for the query search. This operator can be applied to a subquery for the same reason—that is, when the result of an inner query contains a set of values.

Example 5.31 shows the use of the IN operator.

EXAMPLE 5.31

Page 9: Malinda Ramadhani

Get full details of all employees whose departments are located in Dallas.

USE sampleSELECT *   FROM employee   WHERE dept_no IN   (SELECT dept_no      FROM department      WHERE location = ‘Dallas’)

The result is

emp_no emp_fname emp_lname dept_no

25348 Matthew Smith d3

10102 Ann Jones d3

18316 John Barrimore d1

28559 Sybill Moser d1

Each inner query may contain further queries. This type of subquery is called a subquery with multiple levels of nesting. The maximum number of inner queries in a subquery depends on the amount of memory SQL Server has for each SELECT statement. In the case of subqueries with multiple levels of nesting, the system first evaluates the innermost query and returns the result to the query on the next nesting level, and so on. Finally, the outermost query evaluates the final outcome.

EXAMPLE 5.32Get the last names of all employees who work on the project Apollo.

USE sampleSELECT emp_lname   FROM employee   WHERE emp_no IN

Page 10: Malinda Ramadhani

   (SELECT emp_no      FROM works_on      WHERE project_no IN      (SELECT project_no         FROM project         WHERE project_name = ‘Apollo’))

The result is

emp_lname

Jones

James

Bertoni

Moser

The innermost query in Example 5.32 evaluates to the project_no value p1. The middle inner query compares this value with all values of the project_no column in the works_on table. The result of this intermediate query is the set of employee numbers: (10102, 29346, 9031, 28559). Finally, the outermost query displays the corresponding last names for the selected employee numbers.

ANY and ALL OperatorsThe operators ANY and ALL are always used in combination with one of the comparison operators. The general syntax of both operators is

column operator [ANY | ALL] query

where operator stands for a comparison operator.

Page 11: Malinda Ramadhani

Note

Do not use ANY and ALL operators! Every query using ANY or ALL can be better formulated with the EXISTS function (see Chapter 6). Additionally, the semantic meaning of the ANY operator can be easily confused with the semantic meaning of the ALL operator and vice versa.

The ANY operator evaluates to true if the result of an inner query contains at least one row that satisfies the comparison. Example 5.33 shows the use of the ANY operator.

EXAMPLE 5.33Get the employee numbers, project numbers, and job names for employees who have not spent the most time on one of the projects.

USE sampleSELECT DISTINCT emp_no, project_no, job   FROM works_on   WHERE enter_date > ANY   (SELECT enter_date      FROM works_on)

The result is

emp_no project_no job

2581 p3 Analyst

9031 p1 Manager

9031 p3 Clerk

10102 p3 Manager

18316 p2 NULL

25348 P2 Clerk

Page 12: Malinda Ramadhani

28559 p1 NULL

28559 p2 Clerk

29346 p1 Clerk

29346 p2 NULL

Each value of the enter_date column in Example 5.33 is compared with all values of this column. For all dates of this column, except the oldest one, the comparison is evaluated to true at least once. The row with the oldest date does not belong to the result because the comparison does not evaluate to true in any case. In other words, the expression “enter_date > ANY (SELECT enter_date FROM works_on)” is true if there are any (one or more) rows in the works_on table with a value of the enter_date column less than the value of enter_date for the current row. This will be true for all but the minimum (or earliest) value of enter_date in the table.

The keyword SOME is the synonym for ANY.

Example 5.34 shows the use of the ANY operator.

EXAMPLE 5.34Get the first and last names for all employees who work on project p1.

USE sampleSELECT emp_fname, emp_lname   FROM employee   WHERE emp_no = ANY      (SELECT emp_no         FROM works_on         WHERE project_no = ‘p1’)

The result is

emp_fname emp_lname

Page 13: Malinda Ramadhani

Ann Jones

James James

Elsa Bertoni

Sybill Moser

The ALL operator evaluates to true if the evaluation of the table column in the inner query returns all values of that column. Example 5.35 shows the use of the ALL operator.

EXAMPLE 5.35Get jobs of the employee with the smallest employee number.

USE sampleSELECT job   FROM works_on   WHERE emp_no <= ALL   (SELECT emp_no      FROM employee)

The result is

Job

Analyst

EXISTS FunctionThe EXISTS function checks the inner query of a subquery and evaluates toits result contains at least one row. The syntax of the EXISTS function is

[NOT] EXISTS (query)

Chapter 6 contains all examples regarding the EXISTS function.

Page 14: Malinda Ramadhani

Queries in the FROM Clause The previous versions of SQL Server only allowed you to place a query in the WHERE clause of the SELECT statement, as shown in earlier examples. Generally, it should be possible to write a query any place in a SELECT statement where a table can appear. (The result of a query is always a table or, in a special case, an expression.) SQL Server now allows you to write a query as part of the FROM clause. Example 5.36 shows the use of a query inside the FROM clause.

EXAMPLE 5.36Get the names of all employees with employee numbers greater than or equal to 10000.

USE sampleSELECT emp_fname, emp_lname   FROM (SELECT *      FROM employee      WHERE emp_no >= 10000 ) AS empno_10000

The result is

emp_fname emp_lname

Matthew Smith

Ann Jones

John Barrimore

James James

Sybill Moser

The name empno_10000 is an alias table name for the result of the SELECT statement in the FROM clause. (The alias for a table must be specified if a query is placed in the FROM clause of a SELECT statement.)

<< Previous Next >>

Page 15: Malinda Ramadhani

Copyright © 2006 by McGraw-Hill Osborne Media

LearnVisualStudio.NETOver 500 VB, C#, ASP.NET, SQL tutorial videos. Save 10% today!

Home  |  News  |  Source Code  |  Tutorials  |  Components  |  Tools  |  Books  |  Free Magazines  |  Jobs  |  Gear  |  Hosting  |  Links  

Copyright © 2000 - 2006 Code Beach  |   Email Us  |  Privacy Policy  

 

 

A function is a relatively small task that should be performed aside but can be accessed any time to give a result. In Transact-SQL, a function is considered an object. Based on this, you must create a function and execute it before using it. The function then becomes part of a database and it can be accessed.

Practical Learning: Introducing Functions1. Make sure you have a user account named pkatts (created in Lesson 1).

Make sure you created a login for the pkatts account (created in Lesson 3).Start the SQL Server Management Studio

2. To create a new database, right-click Databases and click New Database...

Page 16: Malinda Ramadhani

3. In the Name text box, type RealEstate1

4. Set the owner to ComputerName\pkatts (Windows XP or Vista) or DomainName\pkatts (Windows Server)

5. Set the Paths to C:\Microsoft SQL Server Database Development

6. Click OK

7. In the Object Explorer, Expand the Databases node followed by the RealEstate1 node

8. Under RealEstate1, right-click Security, position the mouse on New, and click Schema... 

Page 17: Malinda Ramadhani

9. Type the name as Payroll and press Enter

Page 18: Malinda Ramadhani
Page 19: Malinda Ramadhani

Function Creation Fundamentals

There are various ways you can start the creating of a function:

In the Object Explorer, expand the desired database. Expand the Programmatically node. Expand the Functions node. Right-click Scalar-Valued Function and click New Scalar-Valued Function... Sample code would be generated for you. You can then modify to customize it

Open an empty query window. Display the Templates Explorer window and expand the Function node. Drag Create Scalar-Valued Function and drop it in the query window

You can open a new empty query window and start typing your code in it

In Transact-SQL, the primary formula of creating a function is:

CREATE FUNCTION FunctionName()The Name of a Function

We mentioned already that, in SQL, a function is created as an object. As such, it must have a name. In our lessons, here are the rules we will use to name our functions:

A name will start with either an underscore or a letter. Examples are _n, act, or Second

After the first character as an underscore or a letter, the name will have combinations of underscores, letters, and digits. Examples are _n24, act_52_t

A name will not include special characters such as !, @, #, $, %, ^, &, or *

We will avoid using spaces in a name, with few exceptions 

If the name is a combination of words, each word will start in uppercase. Examples are DateHired, _RealSport, or DriversLicenseNumber

Returning a Value From a Function

For a function to be useful, it must produce a result. This is also said that the function returns a result or a value. When creating a function, you must specify the type of value that the function would return. To provide this information, after

Page 20: Malinda Ramadhani

the name of the function, type the RETURNS keyword followed by a definition for a data type. Here is a simple example:

CREATE FUNCTION Addition()RETURNS Decimal(6,3)

After specifying the type of value that the function would return, you can create a body for the function. The body of a function starts with the BEGIN and ends with the END keywords. Here is an example:

CREATE FUNCTION Addition()RETURNS Decimal(6,3)BEGIN

END

Optionally, you can type the AS keyword before the BEGIN keyword:

CREATE FUNCTION Addition()RETURNS Decimal(6,3)ASBEGIN

END

Between the BEGIN and END keywords, which is the section that represents the body of the function, you can define the assignment the function must perform. After performing this assignment, just before the END keyword, you must specify the value that the function returns. This is done by typing the RETURN keyword followed by an expression. A sample formula is:

CREATE FUNCTION Addition()RETURNS Decimal(6,3)ASBEGIN RETURN ExpressionEND

Here is an example

CREATE FUNCTION GetFullName()

Page 21: Malinda Ramadhani

RETURNS varchar(100)ASBEGIN

RETURN 'Doe, John'

ENDPractical Learning: Creating Functions1. In the Object Explorer, right-click RealEstate1 and click New Query...

2. To create a function, type the following statement: 

CREATE FUNCTION CalculateWeeklySalary()RETURNS Decimal(8, 2)ASBEGIN RETURN 880.44END;GO

3. To execute the statement, on the SQL Editor toolbar, click the Execute button

4. To save the file that contains the code of the function, on the Standard toolbar, click the Save button

5. Type Calculate as the name of the file

6. Click Save

7. In the Object Explorer, expand the RealEstate1 node, expand Programmability. Expand Functions. And expand Scalar-Valued Functions. Notice the presence of the CalculateWeeklySalary node

Function Calling

After a function has been created, you can use the value it returns. Using a function is also referred to as calling it. To call a function, you must qualify its name. To do this, type the name of the database in which it was created, followed by the period operator, followed by dbo, followed by the period operator, followed by the name of the function, and its parentheses. The formula to use is:

DatabaseName.dbo.FunctionName()

Page 22: Malinda Ramadhani

Because a function returns a value, you can use that value as you see fit. For example, you can use either PRINT or SELECT to display the function's value in a query window. Here is an example that calls the above Addition() function:

PRINT Exercise.dbo.GetFullName();

As an alternative, to call a function, in the Object Explorer, right-click its name, position the mouse on Script Function As, SELECT To, and click New Query Editor Window.

Practical Learning: Calling a Function1. In the Object Explorer, right-click RealEstate1 and click New Query

2. To execute the function we just created, execute the following statement: 

PRINT RealEstate1.dbo.CalculateWeeklySalary();GO

Page 23: Malinda Ramadhani

3. To specify a column name for the returned value of a function, change the function as follows and execute it: 

SELECT RealEstate1.dbo.CalculateWeeklySalary() AS [Weekly Salary];GO

Page 24: Malinda Ramadhani

4. To save the current window, on the toolbar, click the Save button

Function Maintenance  Introduction

Because a function in Transact-SQL is treated as an object, it may need maintenance. Some of the actions you would take include renaming, modifying, or deleting a function.

Renaming a Function

If you create a function and execute it, it is stored in the Scalar-Valued Functions node with the name you gave it. If you

Page 25: Malinda Ramadhani

want, you can change that name but keep the functionality of the function.

To rename a function, in the Object Explorer, right-click it and click Rename. Type the desired new name and press Enter.

Deleting a Function

If you create a function and decide that you don't need it any more, you can delete it.

To delete a function in the Object Explorer, locate the function in the Functions section, right-click it and click Delete. The Delete Object dialog box would come up. If you still want to delete the function, click OK; otherwise, click Cancel.

To programmatically delete a function:

In a query window, type DROP FUNCTION followed by the name of the function and execute the statement

In the Object Explorer, right-click the name of the function, position the mouse on Script Function As, DROP To, and click New Query Editor Window

Open a new query window associated with the database that contains the function. Display the Templates Explorer and expand the Function node. Drag the Drop Function node and drop it in the empty query window

Practical Learning: Deleting a Function1. In the Object Explorer, under the Scalar-Valued Functions node, right-click dbo.CalculateWeeklySalary and click

Delete

2. In the Delete Object dialog box, click OK

Modifying a Function

As mentioned already, in the body of the function, you define what the function is supposed to take care of. As a minimum, a function can return a simple number, typed on the right side of the RETURN keyword. Here is an example:

CREATE FUNCTION Addition()RETURNS intBEGIN RETURN 1END

Page 26: Malinda Ramadhani

You can also declare new variables in the body of the function to help in carrying the assignment. A variable declared in the body of a function is referred to as a local variable. Once such a variable has been declared, it can be used like any other variable. Here is an example:

CREATE FUNCTION Addition()RETURNS intBEGIN DECLARE @Number1 int SET @Number1 = 588 RETURN @Number1 + 1450

ENDPractical Learning: Declaring Local Variables

1. In the Calculate query window, change the code as follows (notice the addition of the schema): 

CREATE FUNCTION Payroll.CalculateWeeklySalary()RETURNS Decimal(8, 2)ASBEGIN

DECLARE@HourlySalary Decimal(8, 2),@WeeklyHours Real,@FullName varchar(100);SET @HourlySalary = 24.15;SET @WeeklyHours = 42.50;

RETURN @HourlySalary * @WeeklyHoursEND;GO

2. Press F5 to execute the statement 

Page 27: Malinda Ramadhani

3. To call the function, select and delete the code. Replace it with the following: 

SELECT RealEstate1.Payroll.CalculateWeeklySalary()

Page 28: Malinda Ramadhani

AS [Weekly Salary];GO

4. Execute the code by pressing F5

Function Arguments  Introduction

In order to carry its assignment, a function can be provided with some values. Put it another way, when you create a function, instead of, or in addition to, local variables, you may want the code that will call the function to provide the

Page 29: Malinda Ramadhani

values needed to perform the assignment. For example, imagine you want to create a function that would generate employees email addresses when a user has entered a first and last name. At the time you are creating the function, you cannot know or predict the names of employees, including those who have not even been hired yet. In this case, you can write the whole function but provide one or more placeholders for values that would be supplied when the function is called.

An external value that is provided to a function is called a parameter. A function can also take more than one parameter. Therefore, when you create a function, you also decide whether your function would take one or more parameters and what those parameters, if any, would be.

A Parameterized Function

We have already seen that a function's name is also followed by parentheses. If the function doesn't use an external value, its parentheses can be left empty. If a function will use an external value, when you create the function, you must specify a name and the type of value of the parameters. The name of the parameter is created with the @ sign, like a variable as we saw in the previous lesson. Here is an example:

CREATE FUNCTION Addition(@Number1 Decimal(6,2))

When a function takes a parameter, in the body of the function, you can use the parameter as if you knew its value, as long as you respect the type of that value. Here is an example:

CREATE FUNCTION Addition(@Number1 Decimal(6,2))RETURNS Decimal(6,2)BEGIN RETURN @Number1 + 1450

ENDCalling a Parameterized Function

When you call a function that takes one parameter, you must supply a value for that argument. To do this, type the value of the parameter in the parentheses of the function. Here is an example:

Page 30: Malinda Ramadhani

A Function With Various Arguments

Instead of only one parameter, you can also create a function that takes more than one parameter. In this case, separate the arguments in the parentheses of the function with a comma. Here is an example:

CREATE FUNCTION Addition(@Number1 Decimal(6,2), @Number2 Decimal(6,2))

Once again, in the body of the function, you can use the parameters as if you already knew their value. You can also declare local variables and involve them with parameters as you see fit. Here is an example:

CREATE FUNCTION Addition(@Number1 Decimal(6,2), @Number2 Decimal(6,2))

Page 31: Malinda Ramadhani

RETURNS Decimal(6,2)BEGIN DECLARE @Result Decimal(6,2) SET @Result = @Number1 + @Number2 RETURN @ResultEND;GO

When calling a function that takes more than one parameter, in the parentheses of the function, provide a value for each parameter, in the exact order they appear in the parentheses of the function. Here is an example:

PRINT Variables1.dbo.Addition(1450, 228);

You can also pass the names of already declared and initialized variables. Here is an example that calls the above function:

DECLARE @Nbr1 Decimal(6,2), @Nbr2 Decimal(6,2)SET @Nbr1 = 4268.55SET @Nbr2 =26.83SELECT @Nbr1 As First, @Nbr2 As Second, Variables1.dbo.Addition(@Nbr1, @Nbr2) AS Result

This would produce:

Page 32: Malinda Ramadhani

Practical Learning: Creating Functions With Arguments1. In the Object Explorer, under the Scalar-Valued Functions node, right-click Payroll.CalculateWeeklySalary and click

Delete

2. In the Delete Object dialog box, click OK

3. To add arguments, change the code of the Calculate() function as follows:

Page 33: Malinda Ramadhani

 

CREATE FUNCTION Payroll.CalculateWeeklySalary(@WeeklyHours Decimal(6,2), @HourlySalary SmallMoney)RETURNS Decimal(8, 2)AS BEGIN DECLARE @Weekly SmallMoney SELECT @Weekly = @WeeklyHours * @HourlySalary

RETURN @WeeklyEND;GO

4. Press F5 to create the function

5. Delete the code in the window and replace it with the following: 

DECLARE @Hours Decimal(5,2), @Hourly SmallMoneySELECT @Hours = 42.50SELECT @Hourly = 18.62SELECT 'Hermine Singh' As [Employee Name], @Hours As [Weekly Hours], @Hourly As [Hourly Salary], RealEstate1.Payroll.CalculateWeeklySalary(@Hours, @Hourly) AS [Weekly Salary];GO

6. Press F5 to execute the statement 

Page 34: Malinda Ramadhani

7. Close the query window without saving the file

8. In the Object Explorer, under the Databases node, right-click RealEstate1 and click Delete

9. In the dialog box, click OK