当前位置:网站首页>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
边栏推荐
猜你喜欢

(科普文)什么是碎片化NFT(Fractional NFT)

Go to Tencent for an interview and let people turn left directly: I don't know idempotency!

GeoServer

70行代码撸一个桌面自动翻译神器

JVM performance tuning

Lock wait timeout exceeded solution

阿里CTO程立:阿里巴巴的开源历程、理念和实践

Distributed pre-course: MySQL implements distributed locks

极验深知v2分析

关于mariadb/mysql的user表:密码正确但登录失败,可能与mysql的空用户有关
随机推荐
TensorFlow custom training function
延时消息队列
针对 MySQL/InnoDB 刷盘调优
【喂到嘴边了的模块】准备徒手撸GUI?用Arm-2D三分钟就够了
JVM performance tuning
TiUP 术语及核心概念
TiDB tool download
tiup clean
Fink异步IO的实战(关联维表)
HTTP缓存小结
Back waves are coming!Ali produced the "second generation" container technical manual and brain map, which is too fragrant
Flask introductory learning tutorial
TensorFlow自定义训练函数
tiup help
How to split microservices?
GeoServer
【云原生】阿里云ARMS业务实时监控
Placement Rules 使用文档
Excel uses Visual Basic Editor to modify macros
如何写一份高可读性的软件工程设计文档