SQL Server creation functions

Source: Internet
Author: User
Tags types of functions

In the process of using the database, we often need to calculate some of the data first, and then query it out, so we need to create a function to accomplish this task.

The function you create typically has two return types, one that returns a collection (a data table), and one that returns a string directly.

The following is an example of creating two types of functions

1. Collection

Create a function Fn_selectworktime

Create FUNCTIONFn_selectworktime (@plantID int    , @CheckTime datetime --Attendance Time,@deptID int --Department,@wShop int -- Section,@roleID int --Post,@wGroup int --Team,@uID int --Employees)RETURNS @retValue Table(atworkdatetime, Offworkdatetime) asBEGIN    Declare @onTime datetime    , @offTime datetime        --......    Set @onTime = '2016-07-01'    Set @offTime = '2016-07-01 17:00'    Insert  into @retValue(Atwork, offwork)Values(@onTime,@offTime)    ReturnEND--calling the created function in a stored procedureSelect *  fromDbo.fn_selectworktime (@plantid,@time1,@deptID,@wShop,@roleID,@wGroup,@uid5)

2. String

Create a function Fn_calstdatt

CREATE FUNCTIONFn_calstdatt (@CheckTime datetime --Attendance Time,@deptID int --Department,@wShop int -- Section,@roleID int --Post,@wGroup int --Team,@uID int --Employees,@which Char(1)--I: Working hours, O: Off hours)RETURNS varchar( -) asBEGIN    Declare @retValue datetime    --......    Set @retValue = '2016-07-01'    Return Convert(varchar( -),@retValue, -)END--calling the created function in a stored procedureSelectDbo.fn_calstdatt (@CheckTime,@deptID,@wShop,@roleID,@wGroup,@uID,@which)

SQL Server creation functions

Related Article

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.