intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Creating User-Defined Functions

Chia sẻ: Tuan Nghia | Ngày: | Loại File: PDF | Số trang:7

106
lượt xem
14
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Creating User-Defined Functions You can create your own user-defined functions in SQL Server. For example, you might want to create your own function to computer

Chủ đề:
Lưu

Nội dung Text: Creating User-Defined Functions

  1. Creating User-Defined Functions You can create your own user-defined functions in SQL Server. For example, you might want to create your own function to compute the discounted price given the original price and factor to multiply that price by. You create a function using the CREATE FUNCTION statement. There are three types of userdefined functions: • Scalar functions Scalar functions return a single value. The returned value can be of any data type except text, ntext, image, cursor, table, timestamp, and user- defined data types. • Inline table-valued functions Inline table-valued functions return an object of the table type. You can think of a table as a regular database table, except it is stored in memory. An inline table-valued function can return the results retrieved by only a single SELECT statement. • Multistatement table-valued functions Multistatement table-valued functions return an object of the table type. Unlike an inline table-valued function, a multistatement table-valued function can contain multiple T-SQL statements. You'll see examples of these three types of functions in the following sections. Using Scalar Functions Scalar functions return a single value. Listing 4.2 shows the DiscountPrice.sql script that creates the DiscountPrice() function, which returns the original price of an item multiplied by a discount factor. These values are passed as parameters to the DiscountPrice() function. You can load this file into Query Analyzer and run it. Listing 4.2: DISCOUNTPRICE.SQL /* DiscountPrice.sql creates a scalar function to return the new price of an item given the original price and a discount factor */ CREATE FUNCTION DiscountPrice(@OriginalPrice money, @Discount float) RETURNS money AS BEGIN RETURN @OriginalPrice * @Discount END
  2. The parameters to the function are placed in brackets after the name of the function in the CREATE FUNCTION statement. Warning Make sure you select the Northwind database from the drop-down list box on the Query Analyzer toolbar before running the script. That way, the function is created in the Northwind database. You can also create functions using Enterprise Manager. You do this by clicking the right mouse button on the User Defined Functions node in the Databases folder and selecting New User Defined Function. You can then cut and paste the contents of DiscountPrice.sql into the Enterprise Manager properties dialog box, as shown in Figure 4.4. Figure 4.4: Using Enterprise Manager to define a function You can view and modify a function by double-clicking the function name in Enterprise Manager. You can also delete a function using Enterprise Manager. The Object Browser of Query Analyzer allows you to view, modify, and delete functions as well. Tip You can also delete a function using the DROP FUNCTION statement, and you can modify a function using the ALTER FUNCTION statement. Once you've created the function, you can call it. When calling a scalar function, you use the following syntax: owner.functionName Where owner is the database user who owns the function, and functionName is the name of the function.
  3. Let's say you created the DiscountPrice() function using the dbo user, then you call that function using dbo.DiscountPrice(). The following example returns 3.0000, which is 10 * 0.3: SELECT dbo.DiscountPrice(10, 0.3); As with any other function, you can pass a column to DiscountPrice(). The following example returns 5.4000 and 18.0000; 5.4000 is 18.0000 * 0.3: SELECT dbo.DiscountPrice(UnitPrice, 0.3), UnitPrice FROM Products WHERE ProductID = 1; You can of course also pass variables as parameters to a function. As before, this example returns 5.4000 and 18.0000: DECLARE @MyDiscountFactor float SET @MyDiscountFactor = 0.3 SELECT dbo.DiscountPrice(UnitPrice, @MyDiscountFactor), UnitPrice FROM Products WHERE ProductID = 1; Using Inline Table-Valued Functions An inline table-valued function returns an object of the table type, which is populated using a single SELECT statement. Unlike a scalar function, an inline table-valued function doesn't contain a body of statements placed within BEGIN and END statements. Instead, only a single SELECT statement is placed within the function. For example, Listing 4.3 shows the ProductsToBeReordered.sql script that creates the ProductsToBeReordered() function. This function returns a table containing the rows from the Products table with a UnitsInStock column value less than or equal to the reorder level parameter passed to the function. Listing 4.3: PRODUCTSTOBEREORDERED.SQL /* ProductsToBeReordered.sql creates an inline table-valued function to return the rows from the Products table whose UnitsInStock column is less than or equal to the reorder level passed as a parameter to the function */ CREATE FUNCTION ProductsToBeReordered(@ReorderLevel int)
  4. RETURNS table AS RETURN ( SELECT * FROM Products WHERE UnitsInStock
  5. Figure 4.5: Using an inline table-valued function Using Multistatement Table-Valued Functions Multistatement table-valued functions return an object of the table type. Unlike an inline table-valued function, a multistatement table-valued function can contain multiple T-SQL statements, and allow you to build complex functions. For example, Listing 4.4 shows the ProductsToBeReordered2.sql script that creates the ProductsToBeReordered2() function. This function returns a table containing the ProductID, ProductName, and UnitsInStock columns from the Products table with a UnitsInStock column value less than or equal to the reorder level parameter. In addition, a new column named Reorder is added to the table, which contains the word Yes or No, depending on whether the product must be reordered. Listing 4.4: PRODUCTSTOBEREORDERED2.SQL /* ProductsToBeReordered2.sql creates an inline table-valued function that returns the rows from the Products table whose UnitsInStock column is less than or equal to the reorder level passed as a parameter to the function */ CREATE FUNCTION ProductsToBeReordered2(@ReorderLevel int)
  6. RETURNS @MyProducts table ( ProductID int, ProductName nvarchar(40), UnitsInStock smallint, Reorder nvarchar(3) ) AS BEGIN -- retrieve rows from the Products table and -- insert them into the MyProducts table, -- setting the Reorder column to 'No' INSERT INTO @MyProducts SELECT ProductID, ProductName, UnitsInStock, 'No' FROM Products; -- update the MyProducts table, setting the -- Reorder column to 'Yes' when the UnitsInStock -- column is less than or equal to @ReorderLevel UPDATE @MyProducts SET Reorder = 'Yes' WHERE UnitsInStock
  7. Figure 4.6: Using a multistatement table-valued function In the next section, you'll learn how to use stored procedures.
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2