Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1057
  • Last Modified:

Fetch [ SQL]

I would like to get a good grasp of using Fetch keyword in SQL.
I have read about it on the internet,but still not clear.
Any simple and clear explanation will be appreciated.

thanks
0
Chuckbuchan
Asked:
Chuckbuchan
  • 4
  • 2
  • 2
  • +1
3 Solutions
 
makerpCommented:
for oracle see this http://www.nis.com/oracle/sql_fetch.html

http://mail.ulacit.ac.pa/SQLHelp/00000413.htm

from reading it looks like this is a useful way to reuse a cursor without re-executing the actual SQL statement.
0
 
ChuckbuchanAuthor Commented:
I have seen those links a moment ago, the explanation is not simple.
0
 
makerpCommented:
are you just curious or do you think you need to use FETCH to solve a specific problem.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
stjowaCommented:
The fetch statement retrieves one row from a group of rows that were identified by the SQL SELECT statement.  After a SELECT statement executes, you can use the FETCH statement to sequentially retrieve one row at a time from the selected rows.
0
 
ChuckbuchanAuthor Commented:
I would like to know it in advance, I need it later when I write T-SQL for some specific tasks. If I manage to get a grasp of it, I will not have to waste time to ask for each specif task next time.

Well ,do your best to explain it, I am raising the points now.
thanks
0
 
ChuckbuchanAuthor Commented:
Like in what circumstances can I use fetch with cursor, can I use fetch only on the server or on the client application also?
in what circumstances can I use Fetch with: Next,Prior,first,last,absolute,relative,global,cursor name,cursor variable name,into.

some simple and clear explanation with an example for each parameter would be helpful.
0
 
SubhasreeCommented:
Below explanation is wrt. MS SQL Server.

FETCH keyword is used to retreive a particular row from a cursor.
(Cursor is a set of records fetched from one or more tableand by use of which each row can be processed.)

You can use the FETCH keyword in TSQL in Server side.

Once u have defined a cursor as  :

DECLARE Test_cursor CURSOR FOR
SELECT field1, field2  FROM test_tab

Open the cursor using the statement :

OPEN Test_cursor

Now to fetch the first or next row , you can use the FETCH keyword as

FETCH NEXT FROM Test_cursor
INTO @TEMP_field1,  @TEMP_field2

Other options that are used with FETCH :

-- Fetch the last row in the cursor.
FETCH LAST FROM Test_cursor
INTO @TEMP_field1,  @TEMP_field2

-- Fetch the first row in the cursor.
FETCH LAST FROM Test_cursor
INTO @TEMP_field1,  @TEMP_field2  

-- Fetch the row immediately prior to the current row in the cursor.
FETCH PRIOR FROM Test_cursor
INTO @TEMP_field1,  @TEMP_field2

-- Fetch the second row in the cursor.
FETCH ABSOLUTE 2 FROM Test_cursor
INTO @TEMP_field1,  @TEMP_field2

-- Fetch the row that is three rows after the current row.
FETCH RELATIVE 3 FROM Test_cursor
INTO @TEMP_field1,  @TEMP_field2

-- Fetch the row that is two rows prior to the current row.
FETCH RELATIVE -2 FROM Test_cursor
INTO @TEMP_field1,  @TEMP_field2


Hope this helps.



0
 
ChuckbuchanAuthor Commented:
To Subhasree :
When it's recommended or preferable to use the Fetch?
for example when writing a program with VB or VB.net ?
OR

when writing Stored Procedure, triggers?


thanks



0
 
SubhasreeCommented:
Hi,
Sorry for the delayed reply.

You can use this FETCH statement in a Stored preocedure. It is always recommended to do maximum part of data processing in the Backend(SQL Server) and send just the result recordset /values to the frontends like VB/VB.NET
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now