博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sql自定义函数及C#中调用
阅读量:4682 次
发布时间:2019-06-09

本文共 3863 字,大约阅读时间需要 12 分钟。

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 ONGOSET QUOTED_IDENTIFIER ONGO   /*   -----------------------------------------------------------------------------  《根据入井时间获取相应的班次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]GOUSE [BW_Contrast]GO/****** Object:  UserDefinedFunction [dbo].[GetClassIDWithName]    Script Date: 01/15/2015 14:31:39 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO/*-----------------------------------------------------------------------------                              《根据入井时间获取相应的班次ID》-----------------------------------------------------------------------------参数:   1.@InWellTime   入井时间返回值:int型 班次ID-----------------------------------------------------------------------------                                 Written by -----------------------------------------------------------------------------*/create    function [dbo].[GetClassIDWithName](@InWellTime varchar(50))returns int asbegin  declare @returnValue int  set @returnValue=0    select @returnValue=classID from v_Class where 时间段名称 =@InWellTime  return @returnValueend GO

 

转载于:https://www.cnblogs.com/xiaochun126/p/4226296.html

你可能感兴趣的文章
HDU 4006: The kth great number
查看>>
将PDF格式转换成其它文档
查看>>
19-7/2作业:Java 打印九九乘法表
查看>>
Android 桌面生成快捷方式
查看>>
【转】How to hire——创业公司应该如何招人
查看>>
iOS Webapp的桌面图标及更新
查看>>
cvpr2017 object detection
查看>>
黑马程序员——OC语言Foundation框架 (2) NSArray NSSet NSDictionary\NSMutableDictionary
查看>>
转载一篇关于追求的文章
查看>>
有关新的博客的
查看>>
[solr] - 索引数据删除
查看>>
django 和 mongdb 写一个简陋的网址,以及用django内置的分页功能
查看>>
[vue]生命周期
查看>>
LB服务,软LB的服务能力(下)
查看>>
XSS知识的总结
查看>>
容器化部署Cassandra高可用集群
查看>>
Oracle锁表信息处理步骤
查看>>
k-近邻算法-Machine Learning In Action学习笔记
查看>>
nvmw安装,用于控制node版本;
查看>>
HADOOP__HBASE集群安装(自带ZOOKEEPER)
查看>>