溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SQL自定義函數(shù)function

發(fā)布時間:2020-08-11 19:01:16 來源:網(wǎng)絡(luò) 閱讀:5276 作者:chenhao_asd 欄目:關(guān)系型數(shù)據(jù)庫

https://blog.csdn.net/qq_23833037/article/details/53170789


https://www.cnblogs.com/youring2/p/4916400.html


用戶定義自定義函數(shù)像內(nèi)置函數(shù)一樣返回標量值,也可以將結(jié)果集用表格變量返回。
sql函數(shù)必須有返回值。

ps: 函數(shù)看成一個處理某些數(shù)據(jù)的功能,因有返回值,則在代碼使用中,需要一個處理過的數(shù)據(jù)。
可直接調(diào)用函數(shù)處理數(shù)據(jù),返回數(shù)據(jù)給代碼使用。

標量函數(shù):返回一個標量值。
表格值函數(shù){內(nèi)聯(lián)表格值函數(shù)、多表格值函數(shù)}:返回行集(即返回多個值)

標量函數(shù)和表格值函數(shù)的區(qū)別在于 返回是標量值(單個數(shù)字或者單個數(shù)據(jù)),還是表格值(多個數(shù)據(jù))

1、標量函數(shù)

create funetion 函數(shù)名(參數(shù)) 
return 返回值數(shù)據(jù)類型 
[with {Encryption | Schemabinding }] 
[as] 
begin 
SQL語句(必須有return 變量或值) 
End

--Schemabinding :將函數(shù)綁定到它引用的對象上(注:函數(shù)一旦綁定,則不能刪除、修改,除非刪除綁定)

測試數(shù)據(jù):


USE [Scratch]
GO
/****** Object:  Table [dbo].[number]    Script Date: 04/19/2018 17:01:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[number](
 [number] [int] NULL,
 [name] [nchar](10) NULL
) ON [PRIMARY]
GO

插入數(shù)據(jù):

  INSERT INTO number(number,name)
  VALUES(123,'a'),
  (222,'b'),
  (333,'c'),
  (323,'d')

例子:

alter function SumRes(@sco nvarchar(20)) --函數(shù)名和參數(shù)
returns real --返回real 值類型
-- real=float(24)   
as
begin
declare @sum real 
declare @code varchar(11)
set @code = @sco + '%'
select @sum = sum(number) from number where name like @code
return @sum --返回值
end

引用自定義函數(shù): 

用戶自定義函數(shù)返回值可放在局部變量中,用set select exec 賦值)

declare @sum1 real,@sum2 real,@sum3 real
set @sum1 = dbo.SumRes('b')
select @sum2 = dbo.SumRes('b')
exec @sum3 = dbo.sumRes'b'
select @sum1 ,@sum2 ,@sum3

SQL自定義函數(shù)function


實例2:

下面的這個函數(shù)根據(jù)生日返回年齡:

create function dbo.calcAge(@birthday datetime)    --函數(shù)名和參數(shù)
returns int    --返回值類型
as
begin
    declare @now datetime
    declare @age int
    set @now=getdate()

    set @age=YEAR(@now)-YEAR(@birthday)

    return @age    --返回值
end
print dbo.calcAge('2000-1-1')

執(zhí)行這段腳本創(chuàng)建函數(shù),創(chuàng)建成功之后,我們調(diào)用一下看看效果: 輸出:15


2、表格值函數(shù)

a、內(nèi)聯(lián)表格值函數(shù)
格式:
create function 函數(shù)名(參數(shù))
returns table
[with{ Encryption | Schemabinding }]
as
return(一條SQL語句)

例子:

create function tabcmess(@code nvarchar(50))
returns table
as
return(select * from number where name = @code)

調(diào)用和結(jié)果:

SQL自定義函數(shù)function

b、多句表格值函數(shù)

多表格值函數(shù)的定義:包含多條SQL語句,必須或者至少有一條給表格變量賦值?。?!

表格變量格式:
returns @變量名(dt) table( 列定義 | 約束定義 )

對表格變量中可以執(zhí)行 select, insert, update, delete,
但select into 和 insert 語句的結(jié)果集是從存儲過程插入。

格式:
create function 函數(shù)名(參數(shù))
return  @dt  table(列的定義)
[with{Encryption | Schemabinding}]
as
begin
SQL語句
end

例子:

create function tabcmess_mul(@code nvarchar(50))
returns @dt table(number int,name nchar(10))
as
begin
insert into @dt select number,name from number where name = @code
return
end

調(diào)用和結(jié)果:

SQL自定義函數(shù)function


3. 修改自定義函數(shù)

alter function tabcmess_mul(@code nvarchar(50))
returns @dt table(number int,name nchar(10))
as
begin
insert into @dt select number,name from number where name = @code
return
end

4. 刪除自定義函數(shù)

drop function tabcmess_mul


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI