Declare T_SC cursor for Select id from A
Declare @Id int
Declare @Id_i int
Declare @T_name varchar(10)
Declare @T varchar(8000)
set @T=''
open T_SC
fetch next from T_SC into @Id
while @@FETCH_STATUS=0
begin
Declare T_SC1 cursor for Select T_name from A_i where id=@Id ---内部游标
open T_SC1
fetch next from T_SC1 into @T_name
while @@FETCH_STATUS=0
begin
set @T=@T+@T_name
fetch next from T_SC1 into @T_name
end
close T_SC1
deallocate T_SC1
fetch next from T_SC into @Id
end
close T_SC
deallocate T_SC
DECLARE authors_cursor CURSOR FOR
SELECT au_id, au_fname, au_lname
FROM authors
WHERE state = "UT"
ORDER BY au_id
OPEN authors_cursor
FETCH NEXT FROM authors_cursor
INTO @au_id, @au_fname, @au_lname
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT " "
SELECT @message = "----- Books by Author: " +
@au_fname + " " + @au_lname
PRINT @message
-- Declare an inner cursor based
-- on au_id from the outer cursor.
DECLARE titles_cursor CURSOR FOR
SELECT t.title
FROM titleauthor ta, titles t
WHERE ta.title_id = t.title_id AND
ta.au_id = @au_id -- Variable value from the outer cursor
OPEN titles_cursor
FETCH NEXT FROM titles_cursor INTO @title
IF @@FETCH_STATUS <> 0
PRINT " <<No Books>>"
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @message = " " + @title
PRINT @message
FETCH NEXT FROM titles_cursor INTO @title
END
CLOSE titles_cursor
DEALLOCATE titles_cursor
-- Get the next author.
FETCH NEXT FROM authors_cursor
INTO @au_id, @au_fname, @au_lname
END
CLOSE authors_cursor
DEALLOCATE authors_cursor
GO
-------- Utah Authors report --------
----- Books by Author: Anne Ringer
The Gourmet Microwave
Is Anger the Enemy?
----- Books by Author: Albert Ringer
Is Anger the Enemy?
Life Without Fear
begin
declare cur1 cursor
for SELECT gatheringid,notecode,rederamount,updatedate from gathering_info_t where isvalid='1' and rederamount>0 order by updatedate
open cur1
fetch next from cur1 INTO @gatheringid,@notecode,@rederamount,@updatedate_g
while (@@fetch_status=0 )
begin
--取开票信息
declare cur2 cursor
for SELECT sendformid,sendformcode,sendformnumber,updatedate from sendform_info_t where isvalid='1' order by updatedate
open cur2
fetch next from cur2 INTO @sendformid,@sendformcode,@sendformnumber,@sendfromupdatedate
while (@@fetch_status=0 and @rederamount>0)
begin
--取本张发票已结算数量
select @inum_pay=isnull(sum(balancenumber),0)
from place_orderprice_t
where sendformid=@sendformid
set @balance_num=@balance_num-@inum_pay
select @balance_price=orderprice from old_orderprice_t where startdate<=@updatedate_g and @updatedate_g<enddate
if @rederamount/@balance_price>=@sendformnumber
begin
set @balance_num=@sendformnumber
end
else
begin
set @balance_num=@rederamount/@balance_price
end
if @balance_num>0
begin
insert into place_orderprice_t(sendformid,balancenumber,gatherid,orderprice) values(@sendformid,@balance_num,@gatheringid,@balance_price)
---update gathering_info_t set rederamount=rederamount-(@balance_price*@balance_num)
set @rederamount=@rederamount-(@balance_price*@balance_num)
end
fetch next from cur2 into @sendformid,@sendformcode,@sendformnumber,@sendfromupdatedate
end
close cur2
deallocate cur2
fetch next from cur1 INTO @gatheringid,@notecode,@rederamount,@updatedate_g
end