Nested Fetch statement
I'm trying to use a nested fetch statement to get the most current records for a record in a previous fetch and to get the last record in one Fetch within the nested loop.
Anyone got some expertise on this?
here's how it goes:
DECLARE ALLTanks_Cur CURSOR FOR
SELECT DISTINCT tk.vslname, tk.siteid, vf.id, vf.tankname,
ORder by tk.vslname, tk.siteid, vf.id, vf.tankname,
vf.tanknumber,vf.tanklocation
OPEN ALLTanks_Cur -- all tanks for vessel
FETCH NEXT FROM ALLTanks_Cur
INTO âvariablesâ
--check the @@Fetch_status to see if there are any more tanks in the ----vessel to
---fetch
while @@fetch_status = 0
begin --fetch tank loop begin
--Declare the variables to store the values in the Last_Tank_Insp ------Corsor FETCH used to find the next cellface for the full tank
DECLARE Last_FULLTank_Insp_Cur SCROLL CURSOR FOR
SELECT Distinct
OPEN Last_FULLTank_Insp_Cur -- all tanks for vessel
FETCH LAST FROM Last_FULLTank_Insp_Cur
INTO âvariablesâ
WHILE @@FETCH_STATUS=0
BEGIN
--CLOSE Last_FULLTank_Insp_Cur
--DEALLOCATE Last_FULLTank_Insp_Cur
----------------------------------------------------------------------
--open full_cursor and insert records to temp variables
declare full_cur CURSOR FOR -- (37 fields)
open full_cur
fetch next from full_cur into --(37 fields)
@VslName,
Insert Into #vessel
VALUES (@VslName,â¦â¦â¦) DECLARE Part_Tank_Insp_cur CURSOR FOR --(37 fields)
OPEN Part_Tank_Insp_cur
FETCH NEXT FROM Part_Tank_Insp_Cur
INTO @LPTTankName,
UPDATE #Vessel
SET CoatingsGen = @CoatingsGen,
WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM Part_Tank_Insp_Cur
INTO "Variables",
End
CLOSE Part_Tank_Insp_Cur
DEALLOCATE Part_Tank_Insp_Cur
begin
--fetch next fulltank & cellfaces cursor
fetch next from full_cur into
@VslName,â¦)
END -- loop end
CLOSE full_cur
DEALLOCATE full_cur
FETCH LAST FROM Last_FULLTank_Insp_Cur
INTO âVariablesâ
END -- last full tank loop
CLOSE Last_FULLTank_Insp_Cur
DEALLOCATE Last_FULLTank_Insp_Cur
FETCH NEXT FROM ALLTanks_Cur
INTO @LFTTankName, @LFTSiteID, @LFTTankID, @LFTTankName,
@LFTTanknumber, @LFTTankLocation
END -- all tanks loop
CLOSE ALLTanks_Cur
DEALLOCATE ALLTanks_Cur
|