SQL custom function function

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


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


User-defined custom functions return scalar values ​​like built-in functions , and can also return result sets as tabular variables .
The sql function must have a return value.

ps: The function is regarded as a function of processing some data. Because of the return value, a processed data is required in the code use.
You can directly call the function to process the data and return the data for the code to use.

Scalar function: Returns a scalar value.
table-valued function {inline table-valued function, multi-table-valued function}: returns a set of rows (ie, returns multiple values)

The difference between a scalar function and a table-valued function is whether the return is a scalar value (single number or single data) or a table value (multiple data)

1. Scalar function

create funetion function name (parameter) 
return return value data type 
[with {Encryption | Schemabinding }] 
[as] 
begin 
SQL statement (must have return variable or value) 
End

--Schemabinding : Bind the function to the object it refers to (Note: Once the function is bound, it cannot be deleted or modified unless the binding is deleted)

Test Data:


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

Insert data:

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

example:

alter function SumRes(@sco nvarchar(20)) -- function name and parameters
returns real -- returns the real value type
-- 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 -- return value
end

Quoting a custom function: 

( The return value of the user-defined function can be placed in a local variable and assigned with 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

image.png


Example 2:

The following function returns age based on birthday:

create function dbo.calcAge(@birthday datetime) -- function name and parameters
returns int -- return value type
as
begin
    declare @now datetime
    declare @age int
    set @now=getdate()

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

    return @age -- return value
end
print dbo.calcAge('2000-1-1')

Execute this script to create a function. After the creation is successful, let's call it to see the effect: Output: 15


2. Table-valued functions

a. Inline table-valued function
format:
create function function name (parameter)
returns table
[with{ Encryption | Schemabinding }]
as
return (a SQL statement)

example:

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

call and result:

image.png

b. Multi-sentence table-valued functions

Definition of multi-table-valued function: Contains multiple SQL statements, and at least one of them must assign values ​​to table variables! ! !

Table variable format:
returns @variable name(dt) table(column definition | constraint definition)

Select, insert, update, delete can be performed on table variables,
but the result set of select into and insert statements are inserted from stored procedures.

Format:
create function function name (parameter)
return @dt table (column definition)
[with{Encryption | Schemabinding}]
as
begin
SQL statement
end

example:

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

call and result:

image.png


3.  Modify the custom function

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. Delete custom function

drop function tabcmess_mul


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324588193&siteId=291194637