USE [UFDATA_999_2014]
GO
/****** Object: StoredProcedure [dbo].[p_XMonPerNums] Script Date: 06/12/2017 16:32:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>dEnterUnitDate
-- Description: <Description,,> 当月在职员工统计
-- =============================================
ALTER PROCEDURE [dbo].[p_XMonPerNums] @XDate datetime AS
BEGIN declare @NowMonFir datetime
set @NowMonFir=convert(datetime,convert(nchar(),YEAR(@XDate))+'-'+CONVERT(nchar(),MONTH(@XDate))+'-01') select T1.cDept_num,T1.cDepName,isnull(T2.aaa,) as PriveMonth,isnull(T3.aaa,) as NowMonthIn,isnull(T4.aaa,) as NowMonthGo,(isnull(T2.aaa,) + isnull(T3.aaa,) - isnull(T4.aaa,) ) as NowMonthNum from(
(select h.cDept_num,d.cDepName from Department d join hr_hi_person h on d.cDepCode=h.cDept_num GROUP BY h.cDept_num,d.cDepName ) T1
left join
(select isnull(COUNT(h.cDept_num),) as aaa,h.cDept_num from hr_hi_person h WHERE dEnterUnitDate <@NowMonFir AND (dLeaveDate is NULL OR dLeaveDate>=@NowMonFir) GROUP BY cDept_num ) T2
on T1.cDept_num=T2.cDept_num
left JOIN
(select isnull(COUNT(h.cDept_num),) as aaa,h.cDept_num from hr_hi_person h WHERE dEnterUnitDate>=@NowMonFir AND dEnterUnitDate<DATEADD(MONTH,,@NowMonFir) GROUP BY cDept_num ) T3
on T1.cDept_num=T3.cDept_num
left JOIN
(select isnull(COUNT(h.cDept_num),) as aaa,h.cDept_num from hr_hi_person h WHERE dLeaveDate>=@NowMonFir AND dLeaveDate<DATEADD(MONTH,,@NowMonFir) GROUP BY cDept_num ) T4
on T1.cDept_num=T4.cDept_num ) END

当月在职员工统计

CREATE PROCEDURE p_lll
@LDate datetime
AS
BEGIN select * into
#templxc
from
(
SELECT sum(rds.iPrice ) AS iPrice ,ic2.cInvCName,ic2.cInvCCode,det2.cDepName,rd.dDate
FROM rdrecord11 rd
JOIN rdrecords11 rds
ON rd.id=rds.id
JOIN Inventory inv
ON rds.cInvCode=inv.cInvCode
JOIN Department det2
ON LEFT(rd.cDepCode,)=det2.cDepCode
JOIN InventoryClass ic2
ON LEFT(rds.cInvCode,)=ic2.cInvCCode
WHERE Ic2.cInvCCode='' OR Ic2.cInvCCode=''
GROUP BY det2.cDepName,ic2.cInvCName,rd.dDate,ic2.cInvCCode
UNION
SELECT sum(rds.iPrice ) AS iPrice ,ic2.cInvCName,ic2.cInvCCode,det2.cDepName,rd.dDate
FROM rdrecord11 rd
JOIN rdrecords11 rds
ON rd.id=rds.id
JOIN Inventory inv
ON rds.cInvCode=inv.cInvCode
JOIN Department det2
ON LEFT(rd.cDepCode,)=det2.cDepCode
JOIN InventoryClass ic2
ON LEFT(rds.cInvCode,)=ic2.cInvCCode
WHERE Ic2.cInvCCode=''
GROUP BY det2.cDepName,ic2.cInvCName,rd.dDate,ic2.cInvCCode)a declare @YFDate datetime
DECLARE @MFDate datetime
SET @YFDate=CONVERT(datetime,convert(nvarchar(),YEAR(@LDate))+'-01-01')
SET @MFDate=CONVERT(datetime,convert(nvarchar(),YEAR(@LDate))+'-'+convert(nvarchar(),month(@LDate))+'-01') SELECT T0.cDepname,T0.cInvCName,T1.a as TDate,T2.a as TMonth,T3.a as TYear from(
(SELECT cDepname,cInvCCode,cInvCName from #templxc group BY cDepname,cInvCCode,cInvCName) T0
left join
(SELECT cInvCCode,SUM(iPrice) As a FROM #templxc where dDate=@LDate GROUP BY cInvCCode) T1
on T0.cInvCCode=T1.cInvCCode
left join
(select cInvCCode,SUM(iPrice) As a FROM #templxc where dDate>=@MFDate and dDate<=@LDate GROUP BY cInvCCode) T2
on T1.cInvCCode=T2.cInvCCode
left join
(select cInvCCode,SUM(iPrice) As a FROM #templxc where dDate>=@YFDate and dDate<=@LDate GROUP BY cInvCCode) T3
on T1.cInvCCode=T3.cInvCCode) ORDER BY cDepname END
GO

不同部门费用

SELECT sum(rds.iPrice ) AS iPrice ,ic2.cInvCName,ic2.cInvCCode,det2.cDepName,det2.cDepCode,rd.dDate
FROM rdrecord11 rd --材料出库单主表
JOIN rdrecords11 rds --材料出库单子表
ON rd.id=rds.id
JOIN Inventory inv --存货档案
ON rds.cInvCode=inv.cInvCode
JOIN Department det2 --部门基本信息
ON rd.cDepCode=det2.cDepCode
JOIN InventoryClass ic2 --存货分类档案
ON LEFT(rds.cInvCode,)=ic2.cInvCCode
WHERE Ic2.cInvCCode='' OR Ic2.cInvCCode=''
GROUP BY det2.cDepName,ic2.cInvCName,rd.dDate,ic2.cInvCCode,det2.cDepCode
UNION
SELECT sum(rds.iPrice ) AS iPrice ,ic2.cInvCName,ic2.cInvCCode,det2.cDepName,det2.cDepCode,rd.dDate
FROM rdrecord11 rd
JOIN rdrecords11 rds
ON rd.id=rds.id
JOIN Inventory inv
ON rds.cInvCode=inv.cInvCode
JOIN Department det2
ON rd.cDepCode=det2.cDepCode
JOIN InventoryClass ic2
ON LEFT(rds.cInvCode,)=ic2.cInvCCode
WHERE Ic2.cInvCCode=''
GROUP BY det2.cDepName,ic2.cInvCName,rd.dDate,ic2.cInvCCode,det2.cDepCode

V

EXEC sys.sp_rename p_lll,p_lll  ——修改名称 旧名字,新名字
DROP PROCEDURE dbo.p_lxc  ——删除

SQL Server 数据库存储过程实例的更多相关文章

  1. 在易语言中调用MS SQL SERVER数据库存储过程方法总结

    Microsoft SQL SERVER 数据库存储过程,根据其输入输出数据,笼统的可以分为以下几种情况或其组合:无输入,有一个或多个输入参数,无输出,直接返回(return)一个值,通过output ...

  2. SQL Server数据库存储过程的异常处理

    SQL Server数据库存储过程的异常处理是非常重要的,明确的异常提示能够帮助我们快速地找到问题的根源,节省很多时间.本文我们就以一个插入数据为例来说明SQL Server中的存储过程怎么捕获异常的 ...

  3. SQL Server数据库存储过程中拼接字符串注意的问题

    在SQL Server数据库中书写复杂的存储过程时,一般的做法是拼接字符串,最后使用EXEC sp_executesql '拼接的字符串' 查询出结果. 先看一段代码: -- ============ ...

  4. C#操作access和SQL server数据库代码实例

    在C#的学习中,操作数据库是比较常用的技术,而access和sql server 数据库的操作却有着不同.那么,有哪些不同呢? 首先,需要引用不同的类.因为有着不同的数据引擎. access:usin ...

  5. sql server系统存储过程大全

    关键词:sql server系统存储过程,mssql系统存储过程 xp_cmdshell --*执行DOS各种命令,结果以文本行返回. xp_fixeddrives --*查询各磁盘/分区可用空间 x ...

  6. sql server数据库中raiserror函数的用法

    server数据库中raiserror的作用就和asp.NET中的throw new Exception一样,用于抛出一个异常或错误.这个错误可以被程序捕捉到. raiserror的常用格式如下:ra ...

  7. 解决修改重命名sql Server数据库名称

    错误: 消息 5030,级别 16,状态 2,第 1 行无法用排他锁锁定该数据库,以执行该操作. 解决方法:(原理一样) 1.将数据库设置为单用户模式 (单用户模式指定一次只有一个用户可访问数据库,该 ...

  8. Sql Server数据库之存储过程

    阅读目录 一:存储过程概述 二:存储过程分类 三:创建存储过程 1.创建无参存储过程 2.修改存储过程 3.删除存储过程 4.重命名存储过程 5.创建带参数的存储过程   简单来说,存储过程就是一条或 ...

  9. SQL Server 数据库的维护(一)__存储过程(procedure)

    --维护数据库-- --存储过程(procedure)-- --概述: SQl Serve的存储过程是由一个或多个T-SQL语句组成的一个集合.常用的程序代码段通常被创建成存储过程,一次创建多次调用, ...

随机推荐

  1. Realtek 8192cu 支持 Android Hotspot 软ap

    http://www.cnblogs.com/bpasser/archive/2011/10/15/2213483.html Android 2.2 开始增加了WiFi Hotspot,可将Andro ...

  2. 2018.10.23 NOIP模拟 行星通道计划(bit)

    传送门 卡常题. 成功卡掉了作死写树套树的zxy. 然而对我的二维bit无能为力. 直接维护两棵bit. bit1[i][j]bit1[i][j]bit1[i][j]表示左端点小于等于iii,右端点小 ...

  3. Django入门与实践-第20章:QuerySets(查询结果集)(完结)

    http://127.0.0.1:8000/boards/1/ #boards/models.py from django.utils.text import Truncator class Topi ...

  4. python面向对象-4类的继承与方法的重载

    1.类的继承与方法的重载 上面就是先定义了一个类A,然后由定义了一个类B,B继承了类A,这样B就有了A的非私有属性和方法. class Washer: company='ZBL' def __init ...

  5. select自定义下拉选择图标

    闲言少叙: 上CSS: appearance: none; -moz-appearance: none; -webkit-appearance: none; cursor: pointer; back ...

  6. SharpMap源代码解析

    1. 简介 SharpMap是基于.net2.0的GIS系统.支持多种.NET开发语言(C# C++ 等).使用属性数据作为注记.符合OpenGIS的简单要素规范(OpenGIS Simple Fea ...

  7. (并查集)Connections in Galaxy War -- zoj --3261 还没写

    链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3261 http://acm.hust.edu.cn/vjudge/ ...

  8. Java中JNI的使用详解第一篇:HelloWorld

    转自: http://blog.csdn.net/jiangwei0910410003/article/details/17465085 今天开始研究JNI技术,首先还是老套路,输出一个HelloWo ...

  9. hdu1171 Big Event in HDU(01背包) 2016-05-28 16:32 75人阅读 评论(0) 收藏

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  10. iterm2 学习笔记

    itrem 笔记 选中即复制,有两种方式. 在新Tab中自动使用前一Tab路径,该怎么用? 系统热键:option+space 自动完成:输入打头几个字母,然后输入command+“;” iterm2 ...