?
Solved

Fetch [ SQL]

Posted on 2005-03-17
9
Medium Priority
?
1,050 Views
Last Modified: 2009-12-16
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
Comment
Question by:Chuckbuchan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 10

Assisted Solution

by:makerp
makerp earned 500 total points
ID: 13568274
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
 

Author Comment

by:Chuckbuchan
ID: 13568350
I have seen those links a moment ago, the explanation is not simple.
0
 
LVL 10

Expert Comment

by:makerp
ID: 13568383
are you just curious or do you think you need to use FETCH to solve a specific problem.
0
Independent Software Vendors: 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!

 

Assisted Solution

by:stjowa
stjowa earned 500 total points
ID: 13568487
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
 

Author Comment

by:Chuckbuchan
ID: 13568506
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
 

Author Comment

by:Chuckbuchan
ID: 13569601
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
 
LVL 2

Accepted Solution

by:
Subhasree earned 1000 total points
ID: 13573105
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
 

Author Comment

by:Chuckbuchan
ID: 13586061
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
 
LVL 2

Expert Comment

by:Subhasree
ID: 13599555
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Introduction to Processes

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question