Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to customize functions in sqlserver

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

In this issue, the editor will bring you about how to customize functions in sqlserver. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

"Custom function" is a common term, while "user-defined function" is a written term in SQL Server. SQL Server 2000 allows users to create custom functions that can have return values. Custom functions are divided into scalar-valued functions or table-valued functions. If the RETURNS clause specifies a scalar data type, the function is a scalar-valued function. You can use multiple Transact-SQL statements to define scalar-valued functions. If the RETURNS clause specifies TABLE, the function is a table-valued function. Table-valued functions can be divided into embedded table-valued functions (inline functions) or multi-statement functions if the TABLE specified in the RETURNS clause does not have a list of columns, then the function is an embedded table-valued function. If the TABLE type specified by the RETURNS clause has a column and its data type, the function is a multi-statement table-valued function.

Example of scalar-valued function

The code is as follows: CREATE FUNCTION dbo.Foo () RETURNS int AS BEGIN declare @ n int select @ nasty 3 return @ n END

Example of embedded table-valued function

Code is as follows: CREATE FUNCTION dbo.Foo () RETURNS TABLE AS return select id, title from msgs

Embedded table-valued functions have only one select statement.

Example of multi-statement table-valued function (part)

The code is as follows: CREATE FUNCTION fn_FindReports (@ InEmpId nchar (5)) RETURNS @ retFindReports TABLE (empid nchar (5) primary key, empname nvarchar (50) NOT NULL, mgrid nchar (5), title nvarchar (30).

Pay attention to the RETURNS section. The following statements are allowed in the body of a multi-statement function. Statements that are not listed in the following list cannot be used in the function body. Assignment statement. Control the flow of sentences. The DECLARE statement, which defines data variables and cursors that are local to the function. The SELECT statement, which contains a select list with expressions that assign values to local variables of the function. A cursor operation that references a local cursor declared, opened, closed, and released in a function. Only FETCH statements that assign values to local variables as INTO clauses are allowed; FETCH statements that return data to the client are not allowed. INSERT, UPDATE, and DELETE statements that modify the function's local table variables. The EXECUTE statement calls the extended stored procedure.

This is how to customize the function in sqlserver shared by Xiaobian. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report