Author |
Topic |
birdie3321
Starting Member
8 Posts |
Posted - 2009-02-11 : 07:26:56
|
Hi all. I have the following code:<begin code>DECLARE prod_cursor CURSORFOR SELECT productid <--Problem child FROM @products OPEN prod_cursor DECLARE @prodid_ FETCH NEXT FROM prod_cursor INTO @prodid_ WHILE (@@FETCH_STATUS <> -1) BEGIN IF(@@FETCH_STATUS <> -2) BEGIN (line by line processing) END FETCH NEXT FROM prod_cursor INTO @prodid_ ENDCLOSE prod_cursorDEALLOCATE prod_cursorSELECT * from @prodid_ <end code>Problem is that I am getting the cursor select's result first then the last select's result set. I only want the last result set. Is there a way i can supress the cursor result set? many thanksbirdie3321 |
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-02-11 : 07:40:03
|
quote: Originally posted by birdie3321 Hi all. I have the following code:<begin code>DECLARE prod_cursor CURSORFOR SELECT productid <--Problem child FROM @products OPEN prod_cursor DECLARE @prodid_ FETCH NEXT FROM prod_cursor INTO @prodid_ WHILE (@@FETCH_STATUS <> -1) BEGIN IF(@@FETCH_STATUS <> -2) BEGIN (line by line processing) END FETCH NEXT FROM prod_cursor INTO @prodid_ ENDCLOSE prod_cursorDEALLOCATE prod_cursorSELECT * from @prodid_ <end code>Problem is that I am getting the cursor select's result first then the last select's result set. I only want the last result set. Is there a way i can supress the cursor result set? many thanksbirdie3321
You can use the LAST argument..FETCH LAST FROM CURSOR_NAME |
|
|
birdie3321
Starting Member
8 Posts |
Posted - 2009-02-11 : 07:48:33
|
Hi, thanks for the reply. I get an error message:fetch: The fetch type last cannot be used with forward only cursors.birdie3321 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-02-11 : 07:55:51
|
Mayve you can use a SET-BASED method instead?What is "line by line processing"? E 12°55'05.63"N 56°04'39.26" |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-02-11 : 07:59:53
|
or just define your cursor as SCROLL CURSOR |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-02-11 : 08:00:33
|
eg :DECLARE prod_cursor SCROLL CURSORFOR.... |
|
|
birdie3321
Starting Member
8 Posts |
Posted - 2009-02-11 : 08:07:03
|
sackets, i get a ton of single results sets when i do that and i have to cancel the query.birdie3321 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-02-11 : 08:08:25
|
quote: Originally posted by birdie3321 sackets, i get a ton of single results sets when i do that and i have to cancel the query.birdie3321
Can you show the sql you are running? |
|
|
birdie3321
Starting Member
8 Posts |
Posted - 2009-02-11 : 08:13:06
|
Sure, what's your email or can i attach code to this forum? Can't email. thanks for the helpbirdie3321 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-02-11 : 08:14:07
|
you can paste it here |
|
|
sridhar.dbe
Starting Member
34 Posts |
Posted - 2009-02-11 : 08:18:28
|
try like thisDECLARE @prodid_ intDECLARE prod_cursor SCROLL CURSORFORSELECT ID from aOPEN prod_cursorFETCH last FROM prod_cursor INTO @prodid_select @prodid_CLOSE prod_cursorDEALLOCATE prod_cursor |
|
|
birdie3321
Starting Member
8 Posts |
Posted - 2009-02-11 : 08:21:14
|
sackets, really it is too long to paste...i'll put it up on zoho and post a link.birdie3321 |
|
|
birdie3321
Starting Member
8 Posts |
|
birdie3321
Starting Member
8 Posts |
Posted - 2009-02-11 : 08:28:32
|
Hi sridhar.dbe, yes, someone already suggested that. I get lots of single return results and have to cancel the query.birdie3321 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-02-11 : 08:37:24
|
You can replace your cursor code with this-- Update matching recordsUPDATE dstSET provid = src.provid, variantid = src.productidFROM @ProductFilter AS dstINNER JOIN @ProvsFilter AS src ON src.productid = dst.productid-- Insert new recordsINSERT @ProductFilter ( productid, provid, displayorder, VariantID, VariantDisplayOrder, ProductName, VariantName ) SELECT src.productid, src.provid, 0, 0, 0, '', ''FROM @ProvsFilter AS srcLEFT JOIN @ProductFilter AS dst ON dst.productid = src.productidWHERE dst.productid IS NULL E 12°55'05.63"N 56°04'39.26" |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-02-11 : 08:37:32
|
Your cursor is running into an infinite loop.Change this part here in your code and give it a try,DECLARE prov_cursor SCROLL CURSORFOR SELECT productid,provid,0,0,0,'','' FROM @provsfilter OPEN prov_cursorDECLARE @prodid_ int, @provid_ int, @temp int, @variantid int, @temp3 int, @temp4 varchar(200),@temp5 varchar(200), @recCount intFETCH LAST FROM prov_cursor INTO @prodid_, @provid_,@temp,@variantid,@temp3,@temp4,@temp5WHILE (@@FETCH_STATUS <> -1) BEGIN IF(@@FETCH_STATUS <> -2) BEGIN SET @variantid = @prodid_ SELECT * FROM @productfilter WHERE productid = @prodid_ SET @recCount = @@rowcount IF(@recCount = 0) BEGIN INSERT @productfilter (productid, provid, displayorder, VariantID, VariantDisplayOrder, ProductName, VariantName) SELECT @prodid_,@provid_,@temp,@variantid,@temp3,@temp4,@temp5 END ELSE BEGIN UPDATE @productfilter SET provid = @provid_, variantid = @variantid WHERE productid = @prodid_ --print 'update made to productid: ' + cast(@prodid_ as varchar(10)) + ' with provid: ' + cast(@provid_ as varchar(10)) END FETCH NEXT FROM prov_cursor INTO @prodid_, @provid_,@temp,@variantid,@temp3,@temp4,@temp5 END END--FETCH LAST FROM prov_cursor INTO @prodid_, @provid_,@temp,@variantid,@temp3,@temp4,@temp5 CLOSE prov_cursorDEALLOCATE prov_cursorEND |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-02-11 : 08:42:38
|
@temp, @variantid, @temp3, @temp4, @temp5 are not necessary since the cursor have hardwired the values as 0 or empty space.See my set-based suggested instead.All the cursor-based code does, is the check if there is a line equal productid = @prodid_ . If not (@@rowcount = 0) the you insert a record with @prodid and @provid_ values (rest are hardwired).If there is a match, you update the record with provid = @provid_, variantid = @variantid Think set-based! E 12°55'05.63"N 56°04'39.26" |
|
|
|