[MSSQL] 如何自己定义函数(返回多变量)
2008-10-08 19:20
260 查看
前些天在写存储过程的时候,需要用到这样一个功能: 14.2返回15 0.8 14.0返回14 0 思路是有带小数的取整+1,1-小数部分。由于在游标判断的时候有30个字段都需要判断。如果直接写在代码里面就有很多重复代码,造成冗余。 就想到用函数。没写过函数哟,就到网上看了一些例子: create function GetInt_decimal( 参数 ) returns varchar(20) 返回类型 as begin 处理代码 return 返回变量 end ---------------------------------------------------------------------------------------------------------- 很快就遇到一个问题,我是要返回多变量。 后来有个网友提醒用table 和拼字串的方法:(在这很感谢他) 方法一:table if object_id('GetInt_decimal') is not null drop function GetInt_decimal go create function GetInt_decimal( @p decimal(18,8) ) returns @table table(i int,d decimal(18,8)) as begin Declare @Int_Num as int,@decimal_Num as decimal(18,8) if @p-cast(@p as int)<>0 begin set @Int_Num=1+cast(@p as int)--整数 set @decimal_Num=@p-cast(@p as int)--小数 end else begin set @Int_Num=@p set @decimal_Num=0 end if @@error=0 insert into @table select @Int_Num,@decimal_Num return end 方法二:拼字串 if object_id('GetInt_decimal') is not null drop function GetInt_decimal go create function GetInt_decimal( @p decimal(18,8) ) returns varchar(20) as begin Declare @Int_Num as int,@decimal_Num as decimal(18,8),@Int_decimal varchar(20) if @p-cast(@p as int)<>0 begin set @Int_Num=1+cast(@p as int)--整数 set @decimal_Num=@p-cast(@p as int)--小数 end else begin set @Int_Num=@p set @decimal_Num=0 end if @@error=0 set @Int_decimal=convert(varchar,@Int_Num)+'|'+convert(varchar,@decimal_Num) return @Int_decimal end
相关文章推荐
- 从递归版归并排序算法看递归函数连续两次调用自己函数每步如何返回,看递归和栈的关系
- 在VC++MFC下如何定义全局变量和全局函数
- 在vue项目中 如何定义全局变量 全局函数
- ES6---箭头函数与function定义函数有什么区别?在箭头函数中,this指向,构造函数,变量提升是如何表现的?
- C语言如何跨文件调用函数定义中声明的变量
- VC++如何声明定义全局函数与全局变量
- 用变量a给出下面的定义:一个有10个指针的数组,该指针指向一个函数,该函数有一个整形参数并返回一个整型数
- 用类名做方法的返回值类型 在学习Java的初始阶段,很多同学使用基本数据类型定义变量和引用类型定义变量以及使用类名做方法的返回值类型常常不知所以。今天我以自己的解读方式和个人的理解作一个简单的解释,和
- C语言如何跨文件调用函数定义中声明的变量
- 在MFC下如何定义全局变量和全局函数
- 在MFC下如何定义全局变量和全局函数(转)
- //1、有一个字符串开头或结尾含有n个空格(” abcdefgdddd ”),欲去掉前后空格,返回一个新字符串。 //要求1:请自己定义一个接口(函数),并实现功能;70分 //要求2:编写
- 存储过程函数中如何定义表变量,删除表变量内容
- 如何将后台返回的值传给回调函数之外的变量
- 在MFC下如何定义全局变量和全局函数
- MFC下如何定义全局变量和全局函数
- 宏定义函数指针类型&函数返回左值类值 1。宏定义不分配内存,变量定义分配内存。 2。宏名和参数的括号间不能有空格 3。宏替换只作替换,不做计算,不做表达式求解 //下面是正确的标准的写法 typedef int(FUNC1)(int in); ty
- const、&在定义函数返回的作用及接收返回变量的类型
- 在MFC下如何定义全局变量和全局函数
- 在MFC下如何定义全局变量和全局函数