Главная страница
    Top.Mail.Ru    Яндекс.Метрика
Форум: "Базы";
Текущий архив: 2005.09.25;
Скачать: [xml.tar.bz2];

Вниз

упроавление вложенными курсорами MSSQL2000   Найти похожие ветки 

 
stud ©   (2005-08-16 14:09) [0]

каким образом организовать "вложеный" цикл в 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 14:13) [1]

второй объявляй внутри


 
stud ©   (2005-08-16 14:21) [2]

не помогает, в результате пустой набор, если выводить результаты отдельно для каждого курсора, имею все нужные данные
в документации сказано что fetch_status - глобальная переменная для текущего подключения, очевидно проблема в этом


 
DiamondShark ©   (2005-08-16 14:50) [3]

У тебя бред написан.

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

Так попробуй.


 
Fay ©   (2005-08-16 14:57) [4]

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 15:05) [5]

goto внутрь цикла -- это сильно.


 
stud ©   (2005-08-16 15:06) [6]


> DiamondShark ©   (16.08.05 14:50) [3]


> stud ©   (16.08.05 14:21) [2][Ответить]


 
Fay ©   (2005-08-16 15:08) [7]

2 DiamondShark ©   (16.08.05 15:05) [5]
Мне так нравится. Удобно при внесении изменений и (IMHO) симпатичнее.


 
stud ©   (2005-08-16 15:16) [8]


> Fay ©   (16.08.05 14:57) [4][Ответить]

спасибо. переместил внуть цикла первого курсора определение второго курсора и заработало, хотя и не сразу))


 
Fay ©   (2005-08-16 15:18) [9]

2 stud ©   (16.08.05 15:16) [8]
Часто помогают уверенность и натиск 8)



Страницы: 1 вся ветка

Форум: "Базы";
Текущий архив: 2005.09.25;
Скачать: [xml.tar.bz2];

Наверх





Память: 0.47 MB
Время: 0.039 c
2-1123945723
Саша У
2005-08-13 19:08
2005.09.25
Как правильно сделать одинаковые обработчики для нескольких компо


1-1125571062
dreamse
2005-09-01 14:37
2005.09.25
Вопрос про TreeView


3-1123832682
ShotGun
2005-08-12 11:44
2005.09.25
Как с помощью SQL сохранить картинку в BLOB поле?


14-1125978782
Ozone
2005-09-06 07:53
2005.09.25
VideoAssm Home Edition :) (зацените)


14-1125609338
KilkennyCat
2005-09-02 01:15
2005.09.25
2 сентября. С Днем рождения!





Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Estonian Filipino Finnish French
Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latvian Lithuanian Macedonian Malay Maltese Norwegian
Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh Yiddish Bengali Bosnian
Cebuano Esperanto Gujarati Hausa Hmong Igbo Javanese Kannada Khmer Lao Latin Maori Marathi Mongolian Nepali Punjabi Somali Tamil Telugu Yoruba
Zulu
Английский Французский Немецкий Итальянский Португальский Русский Испанский