经验证过
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER procedure [dbo].[Pr_GetCGB] '2009-12-1','2009-12-9'
(
@BDate Datetime,
@EDate Datetime
)
as
if exists(Select Name From sysobjects where Name='#T3')
drop table #T3
Create table #T3(AgentNo varchar(20),BillNo varchar(500),calckg float)
Declare @V Varchar(30),@Str Varchar(500),@calckg float
Declare @M Varchar(30)
Set @V=''
set @Str=''
DECLARE My_cursor CURSOR FOR
select AgentNo, sum(calckg) as calckg from Bill_tab group by AgentNo
Open My_cursor
FETCH NEXT FROM My_cursor Into @V,@calckg
WHILE @@FETCH_STATUS = 0
BEGIN
Set @Str=''
Declare My2 cursor for select BillNo From Bill_tab where AgentNo=@V
Open My2
Fetch Next From My2 Into @M
While @@Fetch_Status =0
begin
Set @Str=@Str+@M+','
Fetch Next From My2 Into @M
end
CLOSE My2
DEALLOCATE My2
insert into #T3 values(@V,@Str,@calckg)
FETCH NEXT FROM My_cursor Into @V,@calckg
END
CLOSE My_cursor
DEALLOCATE My_cursor
select * from #T3
drop table #T3
------------------------------------------------------------------------------
/*
create Table T3
( B varchar(10),A varchar(30))
*/
Declare @V Varchar(30),@Str Varchar(30)
Declare @M Varchar(30)
Set @V=''
set @Str=''
DECLARE My_cursor CURSOR FOR
select distinct B from T1
Open My_cursor
FETCH NEXT FROM My_cursor Into @V
WHILE @@FETCH_STATUS = 0
BEGIN
Set @Str=''
Declare My2 cursor for select A From T1 where B=@V
Open My2
Fetch Next From My2 Into @M
While @@Fetch_Status =0
begin
Set @Str=@Str+@M+','
Fetch Next From My2 Into @M
end
CLOSE My2
DEALLOCATE My2
insert into T3 values(@V,@Str)
FETCH NEXT FROM My_cursor Into @V
END
CLOSE My_cursor
DEALLOCATE My_cursor
select * from T3
drop table T3