2012-04-25 8 views
5

döngüsüne WITH table AS nasıl sonuçlanır? Ben tableTablo OLDUĞUMU İLE İLGİLİ İHTİME BAŞLATILMIŞ OLUŞTURULMAK IÇIN BIRAKILMIŞ BIRAKINIZ

declare @id int, @parent int 
declare cur cursor local fast_forward 
for 
    select id, parent from C 
open cur 
fetch next from cur into @id, @parent 
while @@fetch_status = 0 
    begin 
    exec storedProcedure @[email protected], @[email protected] 
fetch next from cur into @id, @parent 
end 
close cur 
deallocate cur 

tüm sonuçlar için spesifik saklı yordamı çalıştırmak istediğiniz CURSOR döngü oluşturduk

How to read all records recursively and show by level depth TSQL

;with C as 
(
    definition ... 
) 

benim tablosundan özyinelemeli sonuçlar elde etmek için nasıl daha önce hakkında sorular var Sorun şu ki CURSOR, WITH AS sonucundan table'u bilmiyor.

Invalid object name 'C'. 

cevap

3

Sen CTE sorgu tarafından döndürülen satırları tutmak için geçici bir tablo veya tablo değişkeni oluşturabilir ve sonra da imleci için kaynak olarak bu tabloyu kullanın.

declare @T table 
(
    id int, 
    parent int 
) 

;with C as 
(
    select 1 as id, 2 as parent 
) 
insert into @T 
select id, parent 
from C 

declare cur cursor for select id, parent from @T