SQL Getting Started Classic (ix) custom functions

Source: Internet
Author: User

UDFs are similar to stored procedures, where a user-defined function is an ordered set of T-SQL statements that are pre-optimized and compiled, and can be used as a unit to test calls. The main difference between UDFs and stored procedures is the way the results are returned, and in order to support more return values, UDFs are more restrictive than stored procedures.

UDF basic syntax: CREATE FUNCTION <function name> (< @parameter name> <data Type>[default VALUE][......N)

RETURNS < return type >

As Begin/* code */END

The simplest thing to try is a UDF:

CREATE FUNCTIONFun_getdatediff (@startDate datetime,@endDate datetime)returns int as BEGIN   return  DateDiff(Month,@startDate,@endDate);ENDGOSelectProductID,sum(LineTotal) fromPurchasing.PurchaseOrderDetailwhereDbo.fun_getdatediff (DueDate,getdate())=142 /**/  Group  byProductID--142 of the data before the query
View Code

To modify the UDF: change the create directly to alter. UDFs are basically the same as stored procedures.

This chapter is relatively simple. Will not write too much, next to talk about business and locks. The feeling behind the writing is getting harder. A bit of an impulse to write.

SQL Getting Started Classic (ix) custom functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.