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)

SQL自定义函数的更多相关文章

  1. SQL自定义函数split分隔字符串

    SQL自定义函数split分隔字符串 一.F_Split:分割字符串拆分为数据表 Create FUNCTION [dbo].[F_Split] ( @SplitString nvarchar(max ...

  2. MS SQL自定义函数IsPositiveInteger MS SQL自定义函数IsNumeric 水晶报表使用IEnumerable<T>数据源

    MS SQL自定义函数IsPositiveInteger   判断字符串是否为正整数,0开始的的数字不算. SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON ...

  3. sql自定义函数及C#中调用

    1.在C#中调用sql自定义函数 1.1 标量值函数 sql语句调用 select  dbo.GetClassIDWithName(1) string strSql = string.Format(& ...

  4. Spark SQL 自定义函数类型

    Spark SQL 自定义函数类型 一.spark读取数据 二.自定义函数结构 三.附上长长的各种pom 一.spark读取数据 前段时间一直在研究GeoMesa下的Spark JTS,Spark J ...

  5. sql 自定义函数--固定格式字符转时间类型

    遇到一个德国的客户,他们的时间格式是JJJJ-TT-DD HH:MM:SS,程序按照这个格式将时间插入数据库,但是在sql自带的转换函数convert.cast过程中报错,网上搜了下都说用conver ...

  6. PL/SQL自定义函数

    从SQL表达式中调用函数的限制 为了从SQL表达式中调用函数,一个用户定义函数必须: 是存储函数 只接受IN函数 只接收有受的SQL数据类型,而不接受PL/SQL数据类型 返回数据类型为有效的SQL数 ...

  7. MS SQL自定义函数判断是否正整数

    可以写一个函数: 主要是使用正则来判断.另外输入字符是空的话,使用"-"来替换. CREATE FUNCTION [dbo].[svf_NonNegativeInteger] ( ...

  8. MS SQL自定义函数IsPositiveInteger

    判断字符串是否为正整数,0开始的的数字不算. SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE FUNCTION [dbo].[svf_I ...

  9. MS SQL自定义函数IsNumeric

    判断字符串是否为纯数字,负数不算.如'00012','54585','1000' SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE FUN ...

随机推荐

  1. python md5 问题(TypeError: Unicode-objects must be encoded before hashing)

    import hashlib import sys def md5s(): m=hashlib.md5() strs=sys.argv[1] m.update(strs.encode("ut ...

  2. [转]gdb调试多进程和多线程命令

    1. 默认设置下,在调试多进程程序时GDB只会调试主进程.但是GDB(>V7.0)支持多进程的分别以及同时调试,换句话说,GDB可以同时调试多个程序.只需要设置follow-fork-mode( ...

  3. chrome 开发并使用油猴 Tampermonkey 插件

    背景:以前 test.user.js 的插件方式被 Chrome 封杀了.现在只能依赖油猴来编写自己的 js 插件. 官方网站:https://tampermonkey.net/ chrome商店:  ...

  4. SpringApplication初始化

    SpringApplication: private void initialize(Object[] sources) { if (sources != null && source ...

  5. Lua中and、or的一些特殊使用方法

    Lua中的逻辑运算符:与(and).或(or)和非(not),与其它语言的逻辑运算符功能一致,这里不做赘述.仅仅说一点,全部的逻辑运算符将false和nil视为假,其它不论什么东西视为真.0也视为真. ...

  6. 通过maven中properties标签定义spring版本号

    一 发现问题 在pom.xml中添加依赖时语法如下 <dependency>  <groupId>org.springframework</groupId>  &l ...

  7. 给Java开发人员的Play Framework(2.4)介绍 Part1:Play的优缺点以及适用场景

    1. 关于这篇系列 这篇系列不是Play框架的Hello World,由于这样的文章网上已经有非常多. 这篇系列会首先结合实际代码介绍Play的特点以及适用场景.然后会有几篇文章介绍Play与Spri ...

  8. 2-2-求并集A=A∪B-线性表-第2章-《数据结构》课本源码-严蔚敏吴伟民版

    课本源码部分 第2章  线性表 - 求并集A=A∪B ——<数据结构>-严蔚敏.吴伟民版        ★有疑问先阅读★ 源码使用说明  链接☛☛☛ <数据结构-C语言版>(严 ...

  9. CNN卷积可视化与反卷积

    1.<Visualizing and Understanding Convolutional Networks> 2.<Adaptive deconvolutional networ ...

  10. sql server2000导出表结构说明

    SELECT 表名 then d.name else '' end, 表说明 then isnull(f.value,'') else '' end, 字段序号=a.colorder, 字段名=a.n ...