Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
simonchia
Starting Member
11 Posts |
Posted - 2004-08-09 : 21:11:49
|
| Greetings, below are my codes for a cursor, the reason i'm using a cursor is that i can loop out the record, can someone show me alternate methods for looping without using a cursor?DECLARE DistiUploadIns_Cursor CURSOR LOCAL FAST_FORWARD FOR select a.ins_id, a.frcst_altr_id, wh.wh_id, b.ins_1st_cnt_dt, b.ins_2nd_cnt_dt, b.ins_st_cnt_dt, b.ins_pos_cnt_dt from ins_product as ainner join ins_country as b on a.ins_id = b.ins_id, warehouse as whwhere a.ins_id = @insId and b.disti_ctry_id = @ctryId and wh.disti_id = @disti_id and wh_inactive_flag ='T' and (ins_1st_cnt_dt <> '' or ins_2nd_cnt_dt <> '' or ins_2nd_cnt_dt <> '' or ins_pos_cnt_dt <> '')OPEN DistiUploadIns_CursorFETCH NEXT FROM DistiUploadIns_Cursor INTO @ins_id, @frcst_altr_id, @WH_ID, @1SC_Type, @2SC_Type, @SC_Type, @POS_Type |
|
|
eyechart
Master Smack Fu Yak Hacker
3575 Posts |
Posted - 2004-08-09 : 23:27:00
|
first, that code is really hard to read. Try formatting it a little bit so we can read it better.something like this:DECLARE DistiUploadIns_Cursor CURSOR LOCAL FAST_FORWARD FORSELECT a.ins_id, a.frcst_altr_id, wh.wh_id, b.ins_1st_cnt_dt, b.ins_2nd_cnt_dt, b.ins_st_cnt_dt, b.ins_pos_cnt_dt FROM ins_product AS a INNER JOIN ins_country as b ON a.ins_id = b.ins_id, warehouse as whWHERE a.ins_id = @insId and b.disti_ctry_id = @ctryId and wh.disti_id = @disti_id and wh_inactive_flag ='T' and (ins_1st_cnt_dt <> '' or ins_2nd_cnt_dt <> '' or ins_2nd_cnt_dt <> '' or ins_pos_cnt_dt <> '' )OPEN DistiUploadIns_CursorFETCH NEXT FROM DistiUploadIns_Cursor INTO @ins_id, @frcst_altr_id, @WH_ID, @1SC_Type, @2SC_Type, @SC_ype, @POS_Type now, for answering your question check out this article: http://www.sql-server-performance.com/dp_no_cursors.asp-ec |
 |
|
|
kselvia
Aged Yak Warrior
526 Posts |
Posted - 2004-08-10 : 02:02:52
|
| Also, if you post the rest of the procedure we might be able to show you how to do it without a cursor.--Ken"Knowledge is a process of piling up facts; wisdom lies in their simplification." |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2004-08-10 : 04:34:55
|
In short:DECLARE @tmpTable table (RowID int IDENTITY(1, 1), ins_id int, frcst_altr_id int ...)DECLARE @Counter intSET @Counter = 0INSERT INTO @tmpTableSELECT blahblah (same select as when declaring the cursor)WHILE (@Counter <= (SELECT MAX(RowID) FROM @tmpTable)) BEGIN SELECT @ins_id = ins_id, @frcst_altr_id = frcst_altr_id, ... FROM @tmptable WHERE RowID = @Counter --> Do your row-by-row stuff SET @Counter = @Counter + 1 END --Lumbago"Real programmers don't document, if it was hard to write it should be hard to understand" |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-08-10 : 08:42:14
|
| the real question as always: Does it have to be done row by row??? not as often as people think sometimes...Corey |
 |
|
|
|
|
|
|
|