| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1305 人关注过本帖
标题:求解SQL语句封装
只看楼主 加入收藏
林学军1993
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2015-9-25
结帖率:0
收藏
 问题点数:0 回复次数:1 
求解SQL语句封装
怎样将下列代码封装

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].Report_RY011') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].Report_RY011



CREATE TABLE [dbo].Report_RY011

(dept_code varchar(20),content char(200),NAN VARCHAR(6),NV VARCHAR(6),QITA VARCHAR(6))


begin tran
  declare @dept_code varchar(20),@content char(200),@NAN VARCHAR(6),@NV VARCHAR(6),@QITA VARCHAR(6)
  
declare AutoXL CURSOR for
 
select dept_code, content from B01 where grade=2
  
open AutoXL
  fetch next from AutoXL into @dept_code,@content
  
while(@@FETCH_STATUS =0)
 
begin

  
select @NAN=(select COUNT(A0188) from A01 where ISNULL(A0107,'')='1' and dept_code like @dept_code +'%')
  
select @NV= (select COUNT(A0188) from A01 where ISNULL(A0107,'')='2'  and dept_code like @dept_code +'%')
  
select @QITA=(select COUNT(A0188) from A01 where ISNULL(A0107,'')=''  and dept_code like @dept_code +'%')
 

  
insert into [dbo].Report_RY011(DEPT_CODE,content,NAN,NV,QITA)
  
select @dept_code,@content,@NAN,@NV,@QITA

     
IF(@@ERROR != 0)
         
BEGIN
           
ROLLBACK TRAN
           
RETURN
         
END
 
FETCH NEXT FROM AutoXL INTO @dept_code,@content
         
END
     
CLOSE AutoXL
     
DEALLOCATE AutoXL
COMMIT TRAN


select content '部门',NAN '男',NV '女',QITA '其他' from [dbo].Report_RY011
2015-10-08 15:14
mxbing1984
Rank: 7Rank: 7Rank: 7
来 自:贵州道真
等 级:贵宾
威 望:37
帖 子:73
专家分:342
注 册:2014-8-8
收藏
得分:0 
统计grade=2的,每个部门的男,女,其他的人数,显示部门名称,男,女,其他
Select DEPT_CODE,Max(content) As content,
SUM(case when ISNULL(A0107,'')='1' then 1 else 0 end) As NAN,
SUM(case when ISNULL(A0107,'')='2' then 1 else 0 end) As NV,
SUM(case when ISNULL(A0107,'')=''  then 1 else 0 end) As QITA
from  B01 A left join A01 B
on A.dept_code=B.dept_code
where A.grade=2
Group by DEPT_CODE

天再高,踮起脚尖就能更接近阳光
2015-10-13 10:53
快速回复:求解SQL语句封装
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016960 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved