I have two cursors, I want to loop through first one, and use its values as parameters for the second cursor and then do some processing. I know how to do this in PL-SQL, but T-SQL puzzles me.
I have gotten so far. For some reason the nested cursor does not print anything.
DECLARE @period DATE
DECLARE @table_type VARCHAR(2)
DECLARE @clmn_clr VARCHAR(100)
DECLARE @clmn_per VARCHAR(100)
DECLARE @period_num INT
DECLARE @period_date DATE
DECLARE @table_type2 VARCHAR(2)
DECLARE c_table CURSOR FOR SELECT period,
table_type
FROM #period_table
DECLARE c_period CURSOR LOCAL FAST_FORWARD FOR SELECT clmn_clr,
clmn_per,
period_num,
period_date,
table_type
FROM #column_period
WHERE table_type = @table_type
OPEN c_table
FETCH NEXT FROM c_table
INTO @period, @table_type
WHILE @@FETCH_STATUS = 0
BEGIN
print @period
print @table_type
print '------------'
FETCH NEXT FROM c_table
INTO @period, @table_type
--Nested cursor
OPEN c_period
FETCH NEXT FROM c_period
INTO @clmn_clr, @clmn_per, @period_num, @period_date, @table_type2
BEGIN
print @clmn_clr
print @clmn_per
print @period_num
print @period_date
print @table_type2
FETCH NEXT FROM c_period
INTO @clmn_clr, @clmn_per, @period_num, @period_date, @table_type2
END
CLOSE c_period
DEALLOCATE c_period
END
CLOSE c_table
DEALLOCATE c_table
It does print the first line from cursor c_table but that is all.
Also, any comments for how to do this in T-SQL better are very welcomed.