求解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