Solved

CURSOR and FETCH STATUS

Posted on 2003-12-09
2
3,323 Views
Last Modified: 2009-02-06
I have the following SQL Statement against MS SQL Server:
DECLARE @acct char(10)
SELECT @acct='418895'
DECLARE cur_comor CURSOR LOCAL SCROLL FOR SELECT TOP 10 [24] FROM tblComor WHERE PatAcctPA=@acct ORDER BY [24TeirStatus]
DECLARE @sql varchar(8000)
DECLARE @sql_val varchar(8000)
DECLARE @c char(10)
DECLARE @i as int
OPEN cur_comor
--print @@FETCH_STATUS
SELECT @i=1
SELECT @sql='insert into #piv_comor(PatacctPA'
SELECT @sql_val = 'VALUES(' + @acct
WHILE @@FETCH_STATUS=0
      BEGIN
      FETCH NEXT FROM cur_comor into @c
      --print 'HELLO: ' + @c
      SELECT @sql=@sql+',comor'+CONVERT(varchar(2),@i)
      SELECT @i=@i+1
      SELECT @sql_val=@sql_val+','''+@c +''''
      end
close cur_comor
--PRINT @sql + ') ' + @sql_val + ')'
DEALLOCATE cur_comor

If I run it once it is fine.  If I try to run it a second time I get a FetchStatus of -1.
Does anyone know what is going on and what I am doing wrong?
0
Comment
Question by:clangl
  • 2
2 Comments
 
LVL 18

Accepted Solution

by:
ShogunWade earned 250 total points
ID: 9904614
Yes..

YOU have no FETCH NEXT FROM .....   before the WHILE @@FETCH_STATUS   loop
0
 
LVL 18

Expert Comment

by:ShogunWade
ID: 9904625
The idomatic was of cursoring is :


FETCH NEXT FROM ........
WHERE @@FETCH_STATUS=0
BEGIN
....
....
....
FETCH NEXT FROM..........
END
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

912 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now