SQL 存储过程

1、

USE [p2p]
GO
/****** Object: StoredProcedure [dbo].[Proc_BranchCompanyStatistic] Script Date: 2015/8/7 14:55:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROC [dbo].[Proc_BranchCompanyStatistic]    --CREATE 创建  ALTER 修改
--传的参数
@DeductDate_B nvarchar(100),--生效起始日
@DeductDate_E nvarchar(100),--生效结束日
@VerifyStatus int,--订单状态
@FirstFinaDeptId int,--总理财部门编号
@FirstFinaPosId int,--总理财职位编号
@IsSingle int--订单状态

AS
BEGIN

declare @forindex int
set @forindex=1;
declare @count int--得到分公司总数
declare @id INT--某个部门下的ID

--创建临时表
declare @end_table table
(
Results money, --预定业绩总和
SumAmount money, --折后业绩总和
GuimoSumAmount money, --规模业绩总和
DName nvarchar(500), --部门名称
PName nvarchar(500) --上级部门名称
)

--接收所有分公司部门编号 临时表
declare @depid_table table
(
DepId int
)

--向营业部临时表中插入数据
insert into @depid_table(DepId) select DepId from (select DepId from t_departmentinfo where parentdepid=@FirstFinaDeptId)a

select @count=count(depid) from @depid_table--分公司条数

while @forindex<=@count
begin

set @id=(select top 1 DepId from @depid_table where DepId not in (select top (@forindex-1) DepId from @depid_table));--取出数据集中每一条数据的编号

--查询某个部门下所有部门ID
WITH dept
AS (
--SELECT DepId from T_DepartmentInfo where DepId=@id UNION ALL
SELECT DepId
FROM dbo.[T_DepartmentInfo]
WHERE [DepId] = @id
UNION ALL
SELECT d.DepId
FROM dbo.[T_DepartmentInfo] d
INNER JOIN dept ON d.[ParentDepId] = dept.[DepId]
)

insert into @end_table(Results,SumAmount,GuimoSumAmount,DName,PName) select * from (
select Results,SumAmount,GuimoSumAmount,b.DepName as DeptName,c.DepName as ParentDeptName from (
select @id as DeptId,SUM(p.Results) Results,SUM(f.SumAmount) SumAmount,SUM(f.GuimoSumAmount) GuimoSumAmount from T_OperaterInfo as o
inner join (select * from T_DepartmentInfo ) as d on o.DepId=d.DepId
inner join (select * from T_Position where IsSingle=0 and ParentPosId=@FirstFinaPosId) as p on o.PositionId=p.PosId
LEFT join (
select x.FirstCreateId, sum((x.Amount*y.AfterPercent))as SumAmount, sum(x.Amount) as GuimoSumAmount from (select FirstCreateId,Amount,ProductTypeId from T_FinancialProductOrder as y where
(CONVERT(varchar,y.DeductDate,23)>=@DeductDate_B or @DeductDate_B='' or @DeductDate_B is null )--条件查询判断为空忽略判断
and
(CONVERT(varchar,y.DeductDate,23)<=@DeductDate_E or @DeductDate_E='' or @DeductDate_E is null )--条件查询判断为空忽略判断
and y.VerifyStatus=@VerifyStatus
)x
left join T_Product as y on x.ProductTypeId=y.ProductId group by x.FirstCreateId
) as f
on f.FirstCreateId=o.OptId
where o.DepId in (select DepId from dept)
)a
left join T_DepartmentInfo b on a.DeptId=b.DepId --关联得到部门名称
left join T_DepartmentInfo c on b.ParentDepId=c.DepId --关联得到该部门上级部门名称

) tb_name


set @forindex+=1;
end

insert into @end_table(DName,GuimoSumAmount,SumAmount,Results,PName)
select '所有地区',GuimoSumAmount,SumAmount,Results,'合计' from(
select SUM(GuimoSumAmount) GuimoSumAmount,SUM(SumAmount) SumAmount,SUM(Results) Results from @end_table)a

select ROW_NUMBER() OVER ( PARTITION BY 1 ORDER BY SumAmount) AS Row_n,DName,GuimoSumAmount,SumAmount,Results,PName from @end_table order by SumAmount


END

2、

  声明多个变量:
  DECLARE @s varchar(10),@a INT

  

多条件选择语句:
Example:

Sql代码  收藏代码
    1. declare @today int  
    2. declare @week nvarchar(3)  
    3. set @today=3  
    4. set @week= case  
    5.      when @today=1 then '星期一'  
    6.      when @today=2 then '星期二'  
    7.      when @today=3 then '星期三'  
    8.      when @today=4 then '星期四'  
    9.      when @today=5 then '星期五'  
    10.      when @today=6 then '星期六'  
    11.      when @today=7 then '星期日'  
    12.      else '值错误'  
    13. end  
    14. print @week  
原文地址:https://www.cnblogs.com/weijiafd/p/4710790.html