sql创建自定义函数

https://www.cnblogs.com/lideng/archive/2013/04/15/3022418.html

 

 

1.标量函数:返回标量数据类型

create function Foo(@ret int)
returns int
as
begin
declare @n int
set @n=3
return @n*@ret
end

select dbo.foo(2)

创建函数的时候不需要加dbo.,但在访问的时候,标量函数要加.dbo的,否则的话会报错“不是可以识别的 内置函数名称。”

2.定义一个内嵌表值函数语法

create function GetUser(@Id varchar(64))
returns table
as
return select * from Published where Id=@Id

select * from GetUser('1301390189239324672')

3.

create function sumUp(@number int)
returns int
as 
begin
    declare @sum int,@i int;
    set @sum = 0;
    set @i = 0;
    while @i <= @number
    begin
         set @sum=@sum+@i
         set @i=@i+1
     end
    return @sum
end

select dbo.sumUp(10)

 

posted @ 2021-02-03 14:10  饮水思源^0^  阅读(1507)  评论(0编辑  收藏  举报