SQL自定义函数
2024-08-28 12:39:14
1,自定义函数--返回单一值
CREATE FUNCTION [dbo].[Round2]
(
-- Add the parameters for the function here
@p1 sql_variant,
-- decimal numbers
@scale int
)
RETURNS sql_variant
AS
BEGIN
-- Declare the return variable here
DECLARE @Result sql_variant,@interval sql_variant -- Add the T-SQL statements to compute the return value here
if @scale >=0
set @interval = 1.0/POWER(10,@scale) * 0.5
else
set @interval = POWER(10,@scale) * 0.5 if @p1 > @interval
set @Result = round(cast(@p1 as float) - cast( @interval as float),@scale)
else
set @Result = 0 -- Return the result of the function
RETURN @Result
END
调用自定义函数
-- 注意,前缀dbo好像不能省略,不清楚原因
select dbo.round2(123.456,2)
删除自定义函数
drop function round2
2,自定义函数--返回一个表结构
2.1 返回的表结构自己定义
CREATE FUNCTION f1
(
-- Add the parameters for the function here
@p1 int,
@p2 char
)
RETURNS
@Table_Var TABLE
(
-- Add the column definitions for the TABLE variable here
c1 int,
c2 int
)
AS
BEGIN
-- Fill the table variable with the rows for your result set
insert into @Table_Var values(1,2)
insert into @Table_Var values(1,2)
RETURN
END
GO
调用
select * from f1(1,1)
2.2 返回的表结构不明确定义,由select语句决定
CREATE FUNCTION f2
(
-- Add the parameters for the function here
@p1 int,
@p2 char
)
RETURNS TABLE
AS
RETURN
(
-- Add the SELECT statement with parameter references here
SELECT top 10 * from dbo.DQuestionData
)
GO
调用
select * from f2(1,1)
最新文章
- java反射复制属性值
- uvm - driver
- python 多线程网络编程 ( 二 )
- DataConvertJson
- PHPSTORM支持dwt文件设置方法
- javaweb学习总结(十八)——JSP属性范围
- groot 引入外部模板
- 从Hadoop框架与MapReduce模式中谈海量数据处理(含淘宝技术架构) (转)
- 文顶顶 iOS开发UI篇—UITabBarController简单介绍 iOS开发UI篇—UITabBarController简单介绍
- 【RabbitMQ系列】队列、绑定、交换器
- SceneKit做一个旋转的地球效果
- Leetcode_114_Flatten Binary Tree to Linked List
- Centos7下搭建LAMP环境,安装wordpress(不会生产博客,只是一名博客搬运工)(菜鸟)
- java导出excel模板数据
- 【模板】Splay
- POJ.1704.Georgia and Bob(博弈论 Nim)
- localStorage 不方便存储数组时的替代方法
- Qt UI界面改了,但UI界面不更新
- Can only modify an image if it contains a bitmap
- 布隆过滤器 zz
热门文章
- Android下的联网下载的操作
- Logstash+ElasticSearch+Kibana处理nginx访问日志(转)
- nginx静态资源缓存与压缩
- git从ssh到提交到github
- 一些新东西学习 - Texture3D,Texture2DArray
- android 通过命令行启动Apk
- django搭建一个小型的服务器运维网站-拿来即用的bootstrap模板
- MYSQL双机热备份的配置实施(问题总结)
- [Windows Azure] Create and use a reporting service in Windows Azure SQL Reporting
- [Windows Azure] .NET Multi-Tier Application Using Storage Tables, Queues, and Blobs - 1 of 5