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)

最新文章

  1. java反射复制属性值
  2. uvm - driver
  3. python 多线程网络编程 ( 二 )
  4. DataConvertJson
  5. PHPSTORM支持dwt文件设置方法
  6. javaweb学习总结(十八)——JSP属性范围
  7. groot 引入外部模板
  8. 从Hadoop框架与MapReduce模式中谈海量数据处理(含淘宝技术架构) (转)
  9. 文顶顶 iOS开发UI篇—UITabBarController简单介绍 iOS开发UI篇—UITabBarController简单介绍
  10. 【RabbitMQ系列】队列、绑定、交换器
  11. SceneKit做一个旋转的地球效果
  12. Leetcode_114_Flatten Binary Tree to Linked List
  13. Centos7下搭建LAMP环境,安装wordpress(不会生产博客,只是一名博客搬运工)(菜鸟)
  14. java导出excel模板数据
  15. 【模板】Splay
  16. POJ.1704.Georgia and Bob(博弈论 Nim)
  17. localStorage 不方便存储数组时的替代方法
  18. Qt UI界面改了,但UI界面不更新
  19. Can only modify an image if it contains a bitmap
  20. 布隆过滤器 zz

热门文章

  1. Android下的联网下载的操作
  2. Logstash+ElasticSearch+Kibana处理nginx访问日志(转)
  3. nginx静态资源缓存与压缩
  4. git从ssh到提交到github
  5. 一些新东西学习 - Texture3D,Texture2DArray
  6. android 通过命令行启动Apk
  7. django搭建一个小型的服务器运维网站-拿来即用的bootstrap模板
  8. MYSQL双机热备份的配置实施(问题总结)
  9. [Windows Azure] Create and use a reporting service in Windows Azure SQL Reporting
  10. [Windows Azure] .NET Multi-Tier Application Using Storage Tables, Queues, and Blobs - 1 of 5