SQL Server2005游标和递归的使用实例

 declare @CompanyID varchar(50),@Year varchar(50),@Department varchar(50),@ShowParentAccount varchar(20)
set @companyId ='328BB999-23E7-4EC6-8F4A-859112439AA6';
set @year ='2012';
set @Department='SG_ZH';
set @ShowParentAccount ='true'

 

declare @CompanyID varchar(50),@Year varchar(50)
set @companyId ='ECB9F979-0CB5-4549-9AFA-483EDEB869F6';
set @year ='2012';

declare @TableSet table
(
	CompName varchar(100),
	Code varchar(50),
	ParentCode varchar(50),
	AccountCode varchar(50),
	AccountName varchar(100),
	ReportNumber decimal(18,2),
	ApprovalNumber decimal(18,2),
	UpdateUser	 varchar(50),
	Remark varchar(500)
)
declare @TableSet1 table
(
	CompName varchar(100),
	Code varchar(50),
	ParentCode varchar(50),
	AccountCode varchar(50),
	AccountName varchar(100),
	ReportNumber decimal(18,2),
	ApprovalNumber decimal(18,2),
	UpdateUser varchar(50),
	Remark varchar(500)
)

declare @TableSetAll table 
(
	CompName varchar(100),
	Code varchar(50),
	ParentCode varchar(50),
	AccountCode varchar(50),
	AccountName varchar(100),
	ReportNumber decimal(18,2),
	ApprovalNumber decimal(18,2),
	UpdateUser varchar(50),
	Remark varchar(500)
)
declare @TableSetAll1 table 
(
	CompName varchar(100),
	Code varchar(50),
	ParentCode varchar(50),
	AccountCode varchar(50),
	AccountName varchar(100),
	ReportNumber decimal(18,2),
	ApprovalNumber decimal(18,2),
	UpdateUser varchar(50),
	Remark varchar(500)
)

declare @CompanyType int
select @CompanyType = case when B.DictSort_Id='BMDWXZ' and B.DictNo ='1' then 1
			when B.DictSort_Id='BMDWXZ' and B.DictNo ='2' then 2
			when B.DictSort_Id='BMDWXZ' and B.DictNo ='3' then 3
			when B.DictSort_Id='BMDWXZ' and B.DictNo ='4' then 4
			else 1 
		end 
 from BM_SYSCompanyEXT A inner join EHS_SYS_DictInfo B on A.CompanyNature = B.DictInfo_Id 
 where A.ID = @CompanyID
if(@CompanyType=3 or @CompanyType=4)
begin 
	insert into @TableSet1
	select 
		D.CompName,
		replace(B.AccountCode,H.CompanyCode,'') as Code,
		replace(G.AccountCode,H.CompanyCode,'') as ParentCode,
		E.CompanyCode + replace(B.AccountCode,H.CompanyCode,''),
		B.AccountName,
		convert(decimal(18,2),isnull(A.ResolutionNum,0)),
		convert(decimal(18,2),isnull(A.ResolutionApprovalNum,0)),
		null as UpdateUser,
		null as Remark
	from 
		BM_AccountResolution A inner join BM_BudgetCycle C on C.ID = A.BudgetCycleID
		inner join Org_Company D on A.CompanyID = D.ID
		inner join BM_CompanyBudgetAccounts B on A.StandardAccountId = B.ID
		inner join BM_SYSCompanyEXT E on D.ID = E.ID
		inner join EHS_SYS_DictInfo F on F.DictInfo_Id = B.BudgetType
		inner join BM_SYSCompanyEXT H on B.CompanyID = H.ID
		left join BM_CompanyBudgetAccounts G on B.ParentAccountID = G.ID
	where
		B.Status=1
		and isnull(B.AccountType,0) <> 1
		and F.DictNo = 'YY' and F.DictSort_Id = 'BMYSLX'
		and E.ID = @CompanyID
		and C.[Year] = @Year
end

insert into @TableSet
select 
	e.CompName,
	replace(a.AccountCode,f.CompanyCode,'') as Code,
	replace(g.AccountCode,f.CompanyCode,'') as ParentCode,
	a.AccountCode,
	a.AccountName,
	convert(decimal(18,2),b.ReportNumber),
	convert(decimal(18,2),b.ApprovalNumber),
        b.UpdateUser,
(select top 1 opinion from AM_ApprovalOpinion WHERE BudgetingAccountID=b.ID ORDER BY ApprovalDate desc) AS Remark
	--o.Opinion AS Remark
from 
	BM_CompanyBudgetAccounts a inner join BM_BudgetingAccount b on a.ID = b.BudgetAccountID 
	inner join BM_BudgetingInvoice c on b.BudgetingInvoiceID = c.ID 
	inner join BM_BudgetCycle d on c.BudgetCycleID = d.ID 
	inner join org_company e on a.CompanyID = e.ID
	inner join BM_SYSCompanyEXT f on e.ID = f.ID
	inner join EHS_SYS_DictInfo h on h.DictInfo_Id = f.CompanyNature
	left join BM_CompanyBudgetAccounts g on a.ParentAccountID = g.ID
        --left join AM_ApprovalOpinion o on o.BudgetingAccountID=b.ID 
where 
	a.Status=1
	and isnull(a.AccountType,0) <> 1
	and f.ID = 'ECB9F979-0CB5-4549-9AFA-483EDEB869F6'
	and d.[Year] = '2012'

insert into @TableSetAll
select 
	A.CompName, --公司名称
	A.Code,     
	'KY' + A.ParentCode, 
	A.AccountCode,--科目编码
	A.AccountName,--科目名称
	isnull(A.ReportNumber,0)+isnull(B.ReportNumber,0) as ReportNumber,--上报数
	isnull(A.ApprovalNumber,0)+isnull(B.ApprovalNumber,0) as ApprovalNumber,--编制审核数
	A.UpdateUser,
	A.Remark --备注
from 
	@TableSet A left join @TableSet1 B on A.Code = B.Code and isnull(A.ParentCode,'') = isnull(B.ParentCode,'')
	and A.AccountCode=B.AccountCode and A.AccountName = B.AccountName 
union
select 
	A.CompName,
	A.Code,
	'KY'+ A.ParentCode,
	A.AccountCode,
	A.AccountName,
	isnull(A.ReportNumber,0) as ReportNumber,
	isnull(A.ApprovalNumber,0) as ApprovalNumber,
	A.UpdateUser,
	A.Remark 
from @TableSet1 A
where not exists(select B.Code from @TableSet B where A.Code = B.Code and isnull(A.ParentCode,'') = isnull(B.ParentCode,'')
and A.AccountCode=B.AccountCode and A.AccountName = B.AccountName)

--select * from @TableSetAll

declare cur_var cursor global for
 select * from @TableSetAll where updateUser in 
(select emplName from org_employee where departmentid ='sg_zh')

declare @compName varchar(100),
	@code varchar(50),
	@parentCode varchar(50),
	@accountCode varchar(50),
	@accountName varchar(100),
	@reportNumber decimal(18,2),
	@approvalNumber decimal(18,2),
	@updateUser varchar(50),
	@remark varchar(500)


--打开游标
open cur_var 
fetch next from cur_var 
into  @compName,@code,@parentCode,@accountCode,@accountName,@reportNumber,@approvalNumber,@updateUser,@remark
--逐行显示数据,并取下一行数据
while @@fetch_status =0
begin 
insert into @TableSetAll1(compName,code,parentCode,accountCode,accountName,reportNumber,approvalNumber,updateUser,remark)
VALUES(@compName,@code,@parentCode,@accountCode,@accountName,@reportNumber,@approvalNumber,@updateUser,@remark);

   WITH PAccount(compName,code,parentCode,accountCode,accountName,reportNumber,approvalNumber,updateUser,remark)
    AS
    (
			select *
				from @TableSetAll where accountCode = @parentCode
        UNION ALL
        SELECT  t.* FROM @TableSetAll t
            INNER JOIN PAccount p ON t.accountCode=p.parentCode 
    )
		insert into @TableSetAll1 select * from PAccount
 
fetch next from cur_var 
into @compName,@code,@parentCode,@accountCode,@accountName,@reportNumber,@approvalNumber,@updateUser,@remark
end
close cur_var
deallocate cur_var

select distinct * from @TableSetAll1 order by AccountCode


 

你可能感兴趣的:(SQL,Server)