question

Muccinit avatar image
Muccinit asked

I do not know how to do a "loop" in SQL

SELECT a.parameter_value into top_limit FROM next_gen_tactical.a10_parameter a WHERE a.parameter_description='top'; open period_tab; eachperiod: loop fetch period_tab into cur_period; if done=1 then leave eachperiod; end if; open fc_tab; eachfc: loop fetch fc_tab into cur_fc; if done=1 then leave eachfc; end if; open std_tab; eachstd: loop fetch std_tab into cur_std; if done=1 then leave eachstd; end if;
sql-server-2008helpprocedureloop
7 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kev Riley avatar image Kev Riley ♦♦ commented ·
What SQL product is this? I don't recognise the syntax, the question is tagged SQL Server 2008, but it is clearly not T-SQL
1 Like 1 ·
Muccinit avatar image Muccinit commented ·
And I forgot this that follows the above: SET @sql := CONCAT('INSERT INTO inventory_analysis.t01_fc_inventory_reserve SELECT t.fc_number, t.sku_number, t.storage_class_detail_id,t.units_type,t.forecasted_slots,t.lprd FROM inventory_analysis.temp13_forecast_slots_sku t where t.fc_number = ', cur_fc ,' and t.lprd = "',cur_period, '" and t.storage_class_detail_id = ' ,cur_std ,' group by t.fc_number, t.sku_number, t.storage_class_detail_id, t.lprd order by forecasted_slots desc limit ', top_limit) ; PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; end loop eachstd; close std_tab; set done=0; end loop eachfc; close fc_tab; set done=0; end loop eachperiod; close period_tab; end
0 Likes 0 ·
Muccinit avatar image Muccinit commented ·
Can anyone help me? Thanks!
0 Likes 0 ·
VishalhSingh avatar image VishalhSingh commented ·
Looks like a hybrid code Kev. i.e. someone from .NET (or VB) tried writing the loop in TSQL. Oh my phew :D
0 Likes 0 ·
Muccinit avatar image Muccinit commented ·
Yes, this was written in an old TSQL database and we are trying to migrate over to our new SQL Server. I am not an expert at writing code. I can understand it, run it, but I would have to take an advanced SQL class to learn this. Any advice, help, would be appreciated! Thanks!
0 Likes 0 ·
Show more comments

1 Answer

·
VishalhSingh avatar image
VishalhSingh answered
Better to read it from [here][1] [1]: http://msdn.microsoft.com/en-IN/library/ms178642.aspx
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.