新聞中心
https://blog.csdn.net/qq_23833037/article/details/53170789
我們提供的服務(wù)有:成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、余杭ssl等。為1000+企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢(xún)和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的余杭網(wǎng)站制作公司
https://www.cnblogs.com/youring2/p/4916400.html
用戶(hù)定義自定義函數(shù)像內(nèi)置函數(shù)一樣返回標(biāo)量值,也可以將結(jié)果集用表格變量返回。
sql函數(shù)必須有返回值。
ps: 函數(shù)看成一個(gè)處理某些數(shù)據(jù)的功能,因有返回值,則在代碼使用中,需要一個(gè)處理過(guò)的數(shù)據(jù)。
可直接調(diào)用函數(shù)處理數(shù)據(jù),返回?cái)?shù)據(jù)給代碼使用。
標(biāo)量函數(shù):返回一個(gè)標(biāo)量值。
表格值函數(shù){內(nèi)聯(lián)表格值函數(shù)、多表格值函數(shù)}:返回行集(即返回多個(gè)值)
標(biāo)量函數(shù)和表格值函數(shù)的區(qū)別在于 返回是標(biāo)量值(單個(gè)數(shù)字或者單個(gè)數(shù)據(jù)),還是表格值(多個(gè)數(shù)據(jù))
1、標(biāo)量函數(shù)
create funetion 函數(shù)名(參數(shù)) return 返回值數(shù)據(jù)類(lèi)型 [with {Encryption | Schemabinding }] [as] begin SQL語(yǔ)句(必須有return 變量或值) End --Schemabinding :將函數(shù)綁定到它引用的對(duì)象上(注:函數(shù)一旦綁定,則不能刪除、修改,除非刪除綁定)
測(cè)試數(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 值類(lèi)型 -- 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ù):
(用戶(hù)自定義函數(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
實(shí)例2:
下面的這個(gè)函數(shù)根據(jù)生日返回年齡:
create function dbo.calcAge(@birthday datetime) --函數(shù)名和參數(shù) returns int --返回值類(lèi)型 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語(yǔ)句)
例子:
create function tabcmess(@code nvarchar(50)) returns table as return(select * from number where name = @code)
調(diào)用和結(jié)果:
b、多句表格值函數(shù)
多表格值函數(shù)的定義:包含多條SQL語(yǔ)句,必須或者至少有一條給表格變量賦值?。?!
表格變量格式:
returns @變量名(dt) table( 列定義 | 約束定義 )
對(duì)表格變量中可以執(zhí)行 select, insert, update, delete,
但select into 和 insert 語(yǔ)句的結(jié)果集是從存儲(chǔ)過(guò)程插入。
格式:
create function 函數(shù)名(參數(shù))
return @dt table(列的定義)
[with{Encryption | Schemabinding}]
as
begin
SQL語(yǔ)句
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é)果:
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
本文題目:SQL自定義函數(shù)function
網(wǎng)頁(yè)網(wǎng)址:http://fisionsoft.com.cn/article/ijpess.html