![]() |
| ||
|
|
Новости |
Поиск |
FAQ |
Статьи |
Книги |
Новости VCL
| | Форумы ЧАТ |
Орешник |
|
| |||
| |||
« предыдущая ветвь | форум | следующая ветвь »
упроавление вложенными курсорами MSSQL2000
stud © (2005-08-16 14:09:00)каким образом организовать "вложеный" цикл в sql 2000?
т.е. есть запрос типа:
declare contr_cur cursor for
select distinct
.........
where contracts.contract_type=1 and
(contracts.contract_close_date is null or
contracts.contract_close_date>getdate())
declare account_cur cursor for
select account.account,
account.rest_bal
from account where account.contract_id=@contract_id */
open contr_cur
fetch next from contr_cur into @contract_num, @client_id, @contract_date_from, @contract_date_to,
@contract_id, @cl_name
while @@fetch_status=0 begin
select @Temp_fetch=@@fetch_status
fetch next from contr_cur into @contract_num, @client_id, @contract_date_from, @contract_date_to,
@contract_id, @cl_name
open account_cur
fetch next from account_cur into @cl_account, @ostatok
while @@fetch_status=0 begin
fetch next from account_cur into @cl_account, @ostatok
insert into #kredit_port (contract_num, client_id, contract_date_from, contract_date_to, contract_id, cl_name, cl_account, ostatok)
values (@contract_num, @client_id, @contract_date_from, @contract_date_to, @contract_id, @cl_name, @cl_account, @ostatok)
end
deallocate account_cur
end
но при выборе данных вложенного набора значение @@Fetch_status=0 и происходит выход из внешнего цикла
как быть в такой ситуации?
Fay (2005-08-16 15:18:00) [1]2 stud © (16.08.05 15:16) [8]
Часто помогают уверенность и натиск 8)
stud (2005-08-16 15:16:00) [2]
> Fay © (16.08.05 14:57) [4][Ответить]
спасибо. переместил внуть цикла первого курсора определение второго курсора и заработало, хотя и не сразу))
Fay (2005-08-16 15:08:00) [3]2 DiamondShark © (16.08.05 15:05) [5]
Мне так нравится. Удобно при внесении изменений и (IMHO) симпатичнее.
stud (2005-08-16 15:06:00) [4]
> DiamondShark © (16.08.05 14:50) [3]
> stud © (16.08.05 14:21) [2][Ответить]
DiamondShark (2005-08-16 15:05:00) [5]goto внутрь цикла -- это сильно.
Fay (2005-08-16 14:57:00) [6]2 stud © (16.08.05 14:21) [2]
Вот пример.
use pubs
go
if object_id('T2') is not null drop table T2
if object_id('T1') is not null drop table T1
create table T1(
ID int not null identity(1, 1),
NAME varchar(30) not null,
constraint PK_T1 primary key (ID)
)
create table T2(
ID int not null identity(1, 1),
T1_ID int not null,
NAME varchar(30) not null,
constraint PK_T2 primary key (ID),
constraint FK_T2_T1 foreign key (T1_ID) references T1(ID)
)
insert into T1 values ('Один')
insert into T1 values ('Два')
insert into T2 values (1, '1-1')
insert into T2 values (1, '1-2')
insert into T2 values (1, '1-3')
insert into T2 values (2, '2-1')
insert into T2 values (2, '2-2')
insert into T2 values (2, '2-3')
declare
@T1_ID int,
@T2_ID int,
@T1_NAME varchar(30),
@T2_NAME varchar(30)
declare c1 cursor for select ID, NAME from T1
open c1
goto L1
while @@fetch_status = 0 begin
declare c2 cursor for select ID, NAME from T2 where T1_ID = @T1_ID
open c2
goto L2
while @@fetch_status = 0 begin
print 'T1.ID = ' + cast(@T1_ID as varchar(8)) + ', T1.NAME = ' + @T1_NAME + ', T2.ID = ' + cast(@T2_ID as varchar(8)) + ', T2.NAME = ' + @T2_NAME
L2:
fetch next from c2 into @T2_ID, @T2_NAME
end
close c2
deallocate c2
L1:
fetch next from c1 into @T1_ID, @T1_NAME
end
close c1
deallocate c1
DiamondShark (2005-08-16 14:50:00) [7]У тебя бред написан.
open contr_cur
fetch next from contr_cur into @contract_num, @client_id, @contract_date_from, @contract_date_to,
@contract_id, @cl_name
while @@fetch_status=0 begin
open account_cur
fetch next from account_cur into @cl_account, @ostatok
while @@fetch_status=0 begin
insert into #kredit_port (contract_num, client_id, contract_date_from, contract_date_to, contract_id, cl_name, cl_account, ostatok)
values (@contract_num, @client_id, @contract_date_from, @contract_date_to, @contract_id, @cl_name, @cl_account, @ostatok)
fetch next from account_cur into @cl_account, @ostatok
end
close account_cur
fetch next from contr_cur into @contract_num, @client_id, @contract_date_from, @contract_date_to,
@contract_id, @cl_name
end
Так попробуй.
stud (2005-08-16 14:21:00) [8]не помогает, в результате пустой набор, если выводить результаты отдельно для каждого курсора, имею все нужные данные
в документации сказано что fetch_status - глобальная переменная для текущего подключения, очевидно проблема в этом
Fay (2005-08-16 14:13:00) [9]второй объявляй внутри