当前位置:网站首页>数据库 - 创建数据库、表、函数等
数据库 - 创建数据库、表、函数等
2022-07-30 14:53:00 【temperamentalkj】
数据库 - 创建数据库、表、函数等
1、创建自定义函数
- 自定义函数分为:标量值函数或表值函数两种。
- 标量值函数:如果 RETURNS 子句指定一种标量数据类型,则函数为标量值函数。
- 表值函数:如果 RETURNS 子句指定 TABLE,则函数为表值函数。
- 表值函数又分为两种:内嵌表值函数(行内函数)或多语句函数
- 如果 RETURNS 子句指定的 TABLE 不附带列的列表,则该函数为内嵌表值函数。
- 如果 RETURNS 子句指定的 TABLE 类型带有列及其数据类型,则该函数是多语句表值函数
1.1 函数语法:
Create function 函数名(参数)
Returns 返回值数据类型
[with {Encryption | Schemabinding }]
[as]
begin
SQL语句(必须有return 变量或值)
End
1.2 标量值函数实例:
CREATE FUNCTION Foo(@ret int ) --传入了一个int类型的参数
RETURNS int --注意这里返回的是一个数据类型
AS
BEGIN
declare @n int
set @n = 3
return @n* @ret
END
1.3 内嵌表值函数语法:
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 多语句表值函数语法:
--多句表格值函数
create function 函数名(参数)
returns 表格变量名table (表格变量定义)
[with {Encryption | Schemabinding }]
as
begin
SQL语句
end
--多句表格值函数包含多条SQL语句,至少有一条在表格变量中填上数据值
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 --函数中最后一条语句必须是返回语句。
end
--调用
select * from GetInfo('admin')
------------
UserName UserPwd
admin amin
边栏推荐
猜你喜欢
随机推荐
元宇宙的前景及四大赛道
Smart Contract Security - Private Data Access
超T动力 盈运天下——中国重汽黄河/豪沃WP14T产品首发荣耀上市!
[Enlightenment by Opportunity-53]: "Sushu"-3- Self-cultivation and Self-cultivation
分布式前修课:MySQL实现分布式锁
Nature Microbiology综述:聚焦藻际--浮游植物和细菌互作的生态界面
5G-based Warehousing Informatization Solution 2022
postgresql的普通字符串和转义字符串
How is the B+ tree index page size determined?
【为宏正名】99%的人从第一天学习C语言就自废的武功
DDS Arbitrary Waveform Output Based on FPGA
JUC common thread pool source learning 02 ( ThreadPoolExecutor thread pool )
被捧上天的Scrum敏捷管理为何不受大厂欢迎了?
[Cloud native] Alibaba Cloud ARMS business real-time monitoring
难道Redis真的变慢了吗?
【云原生 • DevOps】influxDB、cAdvisor、Grafana 工具使用详解
Kubernetes应用管理深度剖析
Lock wait timeout exceeded解决方案
websocket flv 客户端解封包
Mysql database query is very slow. Besides the index, what else can be caused?








![[Cloud Native] Service Industry Case - Solutions for Unpredictable Concurrency Scenarios](/img/c7/3faa29dc374e2e16e59ac9b8ebf249.png)
