How to read position information that Cursor is in SQL Server

Asked

Viewed 193 times

1

In the Cursor examples I found, to read the information I have to use the command: FETCH NEXT FROM

In my code it’s like this: FETCH NEXT FROM CURSOR1 INTO @NAME - at the line Cursor is on I assign the data found to the @NAME variable.

With that, I have two problems and maybe one of them is not necessarily a "problem".

PROBLEM ONE

I necessarily end up having to advance to the next line every time I want to capture from the die where Cursor is. There is how to read the Cursor regardless of where it is?

2nd PROBLEM (Maybe not a problem)

This variable-value assignment every time you move to the next line should consume more resource than necessary. So I want to assign the value to the variable only after going through N positions and not at all times.

1 answer

1

To be able to read the Cursor without necessarily having to pass line by line, just use the command FETCH ABSOLUTE instead of FETCH NEXT,and then the number of the desired line, for example, to access the line 10: FETCH ABSOLUTE 10 FROM CURSOR1

It is important to note that to use any argument other than NEXT the specification of SCROLL in the Cursor declaration. Example: DECLARE CURSOR1 SCROLL CURSOR FOR SELECT NAMES FROM [BASE_NOMES]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.