标题:求解SQL语句封装
取消只看楼主
林学军1993
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2015-9-25
结帖率:0
 问题点数:0 回复次数:0 
求解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



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-457999-1-1.html




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

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