There are several sqlserver custom function in the
scalar-valued function (type of return value to determine a)
the scalar value one can think of the scalar mathematics (not only the magnitude direction) and a vector (direction while the size), Finally, I guess right, ha ha!
Table-valued function (returned as a table value corresponding to a parameterized view)
multi-statement table-valued function bound (scalar and inline table-valued function body, the return value is a list, and scalar functions, can use begin ... end)
hands:
first put forward a table
CREATE TABLE [dbo].[test](
[keyid] [int] IDENTITY(1,1) NOT NULL,
[sort] [varchar](10) NULL,
[qty] [int] NULL,
[aaa] [nvarchar](max) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
Discharge point data into
a, scalar-valued function
create function F_Get(@@CustomKeyID int) --@@CustomKeyID 参数
returns varchar(50) --返回的类型text, 这里是 returns
as
begin
declare @res varchar(50)=''; --申明返回,并初始化
select @res =[sort] from test where [keyid] =@@CustomKeyID --写好sql
return @res;
end
调用
SELECT [dbo].F_Get(5) as [名字]
---调用时候最好写上[dbo],通过数据库对象去调用,不然会报错,找到不这个函数
Results:
2, the value of the function table
create function F_GetTablea(@keyid int)
returns table --返回table
as
return ( select * from [dbo].[test] where [keyid]=@keyid ) --括弧里面返回一个数据表的查询
select * from dbo.F_GetTablea(5) --调用
Results:
3, multi-statement table-valued function
create function F_gettest(@cuseID int)
returns @cuestable table --字段需要和原表字段名称类型都相同,
(
[keyid] int,
[sort] varchar(10)
)
as
begin
if (@cuseID%2 =0)
begin
insert into @cuestable select [keyid] ,[sort] from [dbo].[test] where [keyid] =1
end
else
begin
insert into @cuestable select [keyid] ,[sort] from [dbo].[test] where [keyid] =6
end
return --这个不能删除,一种语法结构
end
The results
temporarily write to you, welcome advice!