当前位置:网站首页>Databases - create databases, tables, functions, etc.
Databases - create databases, tables, functions, etc.
2022-07-30 15:41:00 【temperamenttalkj】
数据库 - 创建数据库、表、函数等
1、创建自定义函数
- 自定义函数分为:Scalar-valued functions or table-valued functions.
- 标量值函数:如果 RETURNS 子句指定一种标量数据类型,则函数为标量值函数.
- 表值函数:如果 RETURNS 子句指定 TABLE,Then the function is a table-valued function.
- There are two types of table-valued functions:内嵌表值函数(行内函数)or multi-statement functions
- 如果 RETURNS 子句指定的 TABLE List without columns,Then the function is an inline table-valued function.
- 如果 RETURNS 子句指定的 TABLE Types have columns and their data types,Then the function is a multi-statement table-valued function
1.1 函数语法:
Create function 函数名(参数)
Returns 返回值数据类型
[with {Encryption | Schemabinding }]
[as]
begin
SQL语句(必须有return 变量或值)
End
1.2 Scalar-valued function instance:
CREATE FUNCTION Foo(@ret int ) --传入了一个int类型的参数
RETURNS int --Note that what is returned here is a data type
AS
BEGIN
declare @n int
set @n = 3
return @n* @ret
END
1.3 Inline table-valued function syntax:
create function 函数名(参数)
returns table
[with {Encryption | Schemabinding }]
as
return(一条SQL语句)
eg
create function GetUser(@name varchar(10))
returns table
as
return select * from userInfo where userName=@name
调用:
select * from getuser('admin')
1.4 Multi-statement table-valued function syntax:
--Multi-sentence table-valued functions
create function 函数名(参数)
returns table variable nametable (Table variable definitions)
[with {Encryption | Schemabinding }]
as
begin
SQL语句
end
--A multi-sentence table-valued function contains multiple entriesSQL语句,At least one of the table variables is filled with data values
eg
create function GetInfo(@name varchar(20))
returns @cTable table(UserName varchar(10),UserPwd varchar(10))
as
begin
insert into @cTable
select userName,userPass from userinfo where username=@name
return --The last statement in a function must be a return statement.
end
--调用
select * from GetInfo('admin')
------------
UserName UserPwd
admin amin
边栏推荐
猜你喜欢
随机推荐
golang modules初始化项目
【重磅来袭】教你如何在RGBD三维重建中获取高质量模型纹理
存储器映射、位带操作
【云原生】灰度发布、蓝绿发布、滚动发布、灰度发布解释
哨兵
组态 - 笔记
tiup list
Applicable scenarios of TiDB tools
Kubernetes应用管理深度剖析
CS内网横向移动 模拟渗透实操 超详细
Extremely Knowing v2 Analysis
TiUP 命令概览
How do luxury giants such as GUCCI and LV deploy the metaverse, should other brands keep up?
在树莓派上驱动CSI摄像头
CMake库搜索函数居然不搜索LD_LIBRARY_PATH
被捧上天的Scrum敏捷管理为何不受大厂欢迎了?
【云原生】服务行业案例-不可预测的并发场景解决方案
(科普文)什么是碎片化NFT(Fractional NFT)
HTTP缓存小结
Example of video switching playback (video switching example) code









