sql自定义函数及C#中调用
1、在C#中调用sql自定义函数
1.1 标量值函数
sql语句调用 select dbo.GetClassIDWithName(1)
string strSql = string.Format("select dbo.GetClassIDWithName('{0}')",dtTime);
DataTable dt = DB_Contrast.DB.OleDbHelper.GetDataTable(strSql);
1.2 表值函数
sql语句调用 select * from GetAnalysis('2015-1-15',1)
string strSql = string.Format("select * from dbo.GetAnalysis('{0}',{1}) where 部门='{2}' ",dtTime, classid,"开发");
DataSet ds = DB_Contrast.DB.OleDbHelper.GetDataSet(strSql);
2、表值函数,
内层select获取不重复的记录
外层按照部门进行分组
USE [BW_Contrast]
GO
/****** Object: UserDefinedFunction [dbo].[GetAnalysis] Script Date: 01/15/2015 13:09:17 ******/
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetAnalysis]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GetAnalysis]
GO /****** Object: UserDefinedFunction [dbo].[GetAnalysis] Script Date: 01/15/2015 13:09:17 ******/
SET ANSI_NULLS ON
GO SET QUOTED_IDENTIFIER ON
GO
/*
-----------------------------------------------------------------------------
《根据入井时间获取相应的班次ID》
-----------------------------------------------------------------------------
参数:
1、@classdate 班次日期 2、@classid 班次ID 返回值:table
-----------------------------------------------------------------------------
Written by
-----------------------------------------------------------------------------
*/ CREATE function [dbo].[GetAnalysis](@classdate datetime,@classid int)
returns table as
return (
--declare @classdate datetime,@classid int
--set @classid=2
--set @classdate='2015-1-14'
select
case when(grouping(a.部门)=1) then '合计' else a.部门 end as 部门
,(select top 1 ID from dbo.v_Dept where 部门名称=a.部门) as deptid
,Sum(case when dt_RealTime is not null and myclassid=@classid then 1 else 0 end ) as 派班人数
,Sum(case when dt_GetTime is not null and myclassid=@classid then 1 else 0 end ) as 领灯人数
,Sum(case when dtInWellTime is not null and myclassid=@classid then 1 else 0 end ) as 下井人数
,Sum(case when dt_OutWellTime is not null and myclassid=@classid then 1 else 0 end ) as 上井人数
,Sum(case when dt_ReturnTime is not null and myclassid=@classid then 1 else 0 end ) as 还灯人数
from
(
select
部门,deptid,myclassdate,myclassid,mypersonid
,min(dt_RealTime) as dt_RealTime
,min(dt_GetTime) as dt_GetTime
,min(dtInWellTime) as dtInWellTime
,min(dt_OutWellTime) as dt_OutWellTime
,min(dt_ReturnTime) as dt_ReturnTime
from v_ALL_NEW
where myclassdate=@classdate and myclassid=@classid
group by 部门,myclassdate,myclassid,mypersonid,deptid
)a
where myclassdate=@classdate and myclassid=@classid and 部门 is not null
group by 部门
with rollup
) GO
3、标量值函数
USE [BW_Contrast]
GO /****** Object: UserDefinedFunction [dbo].[GetClassIDWithName] Script Date: 01/15/2015 14:31:39 ******/
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetClassIDWithName]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GetClassIDWithName]
GO USE [BW_Contrast]
GO /****** Object: UserDefinedFunction [dbo].[GetClassIDWithName] Script Date: 01/15/2015 14:31:39 ******/
SET ANSI_NULLS ON
GO SET QUOTED_IDENTIFIER ON
GO /*
-----------------------------------------------------------------------------
《根据入井时间获取相应的班次ID》
-----------------------------------------------------------------------------
参数:
1.@InWellTime 入井时间 返回值:int型 班次ID
-----------------------------------------------------------------------------
Written by
-----------------------------------------------------------------------------
*/
create function [dbo].[GetClassIDWithName](@InWellTime varchar(50))
returns int as
begin
declare @returnValue int
set @returnValue=0
select @returnValue=classID from v_Class where 时间段名称 =@InWellTime
return @returnValue
end GO
sql自定义函数及C#中调用的更多相关文章
- SQL自定义函数split分隔字符串
SQL自定义函数split分隔字符串 一.F_Split:分割字符串拆分为数据表 Create FUNCTION [dbo].[F_Split] ( @SplitString nvarchar(max ...
- MS SQL自定义函数IsPositiveInteger MS SQL自定义函数IsNumeric 水晶报表使用IEnumerable<T>数据源
MS SQL自定义函数IsPositiveInteger 判断字符串是否为正整数,0开始的的数字不算. SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON ...
- Spark SQL 自定义函数类型
Spark SQL 自定义函数类型 一.spark读取数据 二.自定义函数结构 三.附上长长的各种pom 一.spark读取数据 前段时间一直在研究GeoMesa下的Spark JTS,Spark J ...
- PL/SQL自定义函数
从SQL表达式中调用函数的限制 为了从SQL表达式中调用函数,一个用户定义函数必须: 是存储函数 只接受IN函数 只接收有受的SQL数据类型,而不接受PL/SQL数据类型 返回数据类型为有效的SQL数 ...
- sql 自定义函数--固定格式字符转时间类型
遇到一个德国的客户,他们的时间格式是JJJJ-TT-DD HH:MM:SS,程序按照这个格式将时间插入数据库,但是在sql自带的转换函数convert.cast过程中报错,网上搜了下都说用conver ...
- sqlserver自定义函数的创建与调用
sqlserver中有系统提供的函数,像avg.sum.getdate()等,用户还可以自定义函数. 用户自定义的函数包括:标量函数和表值函数,其中标量函数和系统函数的用法一样,表值函数根据主体的定义 ...
- c++纯虚函数在父类中调用的规避
构造和析构函数不允许调用纯虚函数,可以先调用虚函数,里面再调用纯虚函数实现. class Base{public: virtual void foo()=0; Base() { call_ ...
- SQL自定义函数
1,自定义函数--返回单一值 CREATE FUNCTION [dbo].[Round2] ( -- Add the parameters for the function here @p1 sql_ ...
- 在自定义的dwt文件中调用page_header.lbi和page_footer.lbi
昨天下午接到需求说要增加一个新的页面,作为优惠活动规则的介绍之用,之前对ecshop各种修改,但是这次自己做页面还是第一次,文件太多,函数也太多,一个一个的读过来时间很头疼的事情,于是就参照goods ...
随机推荐
- UVA 11925:Generating Permutations(冒泡排序 Grade D)
VJ题目链接 题意:n个数(n<300),是一个1~n的某个排列.有两种操作:操作1把前两个数换位置,操作2把第一个数移动到最后.问给出一个排列,问通过怎样的操作,能把1,2,3,...,n变到 ...
- UVALive 3507:Keep the Customer Satisfied(贪心 Grade C)
VJ题目链接 题意: 知道n(n <= 8e6)个工作的完成所需时间q和截止时间d,你一次只能做一个工作.问最多能做多少工作? 思路: 首先很像贪心.观察发现如下两个贪心性质: 1)一定存在一个 ...
- nagios部署+短信和邮件报警
操作系统 CentOS6.6 服务端:10.0.0.20 客户端:10.0.0.50 一.nagios的服务端安装部署 1.nagios安装 [root@manager src]# rzrz wai ...
- 解决ASP.NET裁剪图片失真
//有的时候剪切图片时,出现图片失真的问题,是因为图片质量下降造成的 //按照指定的数据画出画板(位图) System.Drawing.Image imgPhoto = System.Drawing. ...
- Visual Studio跨平台开发(1):Hello Xamarin!
前言 应用程序发展的脚步, 从来没有停过. 从早期的Windows 应用程序, 到网络时代的web 应用程序, 再到近几年相当盛行的行动装置应用程序(Mobile Application), 身为C# ...
- 对事务的特性ACID的理解
对事务的特性ACID的理解 数据库的事务必须具备ACID特性,ACID是指 Atomicity(原子性).Consistensy(一致性).Isolation(隔离型)和Durability(持久性) ...
- springboot微服务的简单小结
springboot微服务的简单小结 近来公司用springboot微服务,所以小结一下. 基础: 什么是SpingBoot微服务? 如何创建SpringBoot微服务? 如何管理和完善SpringB ...
- CQRS读写职责分离模式(Command and Query Responsibility Segregation (CQRS) Pattern)
此文翻译自msdn,侵删. 原文地址:https://msdn.microsoft.com/en-us/library/dn568103.aspx 通过使用不同的接口来分离读和写操作,这种模式最大化了 ...
- SQL SERVER Update from 使用陷阱
原文:SQL SERVER Update from 使用陷阱 update A set from A left join B on 此方法常用来使用根据一个表更新另一个表的数据,来进行数据同步更新.若 ...
- VBA 时间相关的函数
DateSerial DateADD Datediff http://www.yiibai.com/vba/vba_datediff_function.html https://www.techont ...