Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

@@FETCH_STATUS Problem

 
   Database Help (Home) -> Programming RSS
Next:  .net native SqlClient regional settings problem  
Author Message
Mike

External


Since: Jun 03, 2008
Posts: 2



(Msg. 1) Posted: Tue Jun 03, 2008 12:39 am
Post subject: @@FETCH_STATUS Problem
Archived from groups: microsoft>public>sqlserver>programming (more info?)

I have a table with 15000 records in it where I need to update a
couple of columns from a calculation. I would normally do this through
ADO and a bit of VB but I thought I would have a go at doing this
using a cursor in a stored procedure having never done it before. I
have followed the advice on the web but whenever I run the procedure
it gets between 100 and 1500 records into the table and then just
stops. The records have been imported from an Excel spread sheet.
Below is the SP:

ALTER PROCEDURE BriminghamSP
AS
declare @ID int
declare @postcode char(Cool
declare @RecordCount int
declare @RiskFactor decimal(18,4)
declare @Risk varchar(10)
declare @Homes int
declare @Leads int
declare @Easting float
declare @Northing float
declare myCursor CURSOR FOR select id, postcode1 from SPBirmingham
open myCursor
FETCH NEXT FROM myCursor into @id, @postcode
while (@@FETCH_STATUS <> -1)
begin
if (@@FETCH_STATUS <> -2)
begin
set @Risk = ''
set @RiskFactor = 0
set @Homes = 0
set @Leads = 0
SELECT @RecordCount = count(postCode) FROM
dbo.EnglandMappointLeads2001to2005 WHERE postCode = @postCode
if @RecordCount > 0
begin
SELECT @Easting = E, @Northing = N FROM
dbo.EnglandMappointLeads2001to2005 WHERE postCode = @postCode
SELECT @Leads = sum(Leads), @Homes = sum(Homes) FROM
dbo.EnglandMappointLeads2001to2005 WHERE sqrt(square(@Easting-E)
+square(@Northing-N))<=500
if (@Leads > 0 and @Homes > 0)
begin
set @RiskFactor = convert(decimal(18,4),@Leads)/
convert(decimal(18,4),@Homes)
end
if @RiskFactor < 0.0022
begin
set @Risk = 'Low'
end
if (@RiskFactor > 0.0022 and @RiskFactor <= 0.0044)
begin
set @Risk = 'Medium'
end
if (@RiskFactor > 0.0044 and @RiskFactor <= 0.0088)
begin
set @Risk = 'High'
end
if @RiskFactor > 0.0088
begin
set @Risk = 'Very High'
end
end
update spbirmingham set leads = @Leads, homes = @homes, risk_factor =
@RiskFactor, risk = @Risk where id = @id
FETCH NEXT FROM myCursor INTO @id, @postcode
end
end
close myCursor
deallocate myCursor

I tried the @@FETCH_STATUS <> -1 etc but this made no difference

 >> Stay informed about: @@FETCH_STATUS Problem 
Back to top
Login to vote
Hugo Kornelis

External


Since: Jan 11, 2008
Posts: 440



(Msg. 2) Posted: Tue Jun 03, 2008 11:12 pm
Post subject: Re: @@FETCH_STATUS Problem [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Tue, 3 Jun 2008 00:39:14 -0700 (PDT), Mike wrote:

>I have a table with 15000 records in it where I need to update a
>couple of columns from a calculation. I would normally do this through
>ADO and a bit of VB but I thought I would have a go at doing this
>using a cursor in a stored procedure having never done it before. I
>have followed the advice on the web but whenever I run the procedure
>it gets between 100 and 1500 records into the table and then just
>stops. The records have been imported from an Excel spread sheet.
>Below is the SP:
(snip)

Hi Mike,

You didn't post enough information to reproduce the problem (see
www.aspfaq.com/5006 for what we need). However, I *THINK* that the
problem is related to using default cursor options, probably in
combination with there not being a proper key on the table. You might
find that the problem goes away if you explicitly ask for a STATIC
cursor.

That being said, you'll probably find that the performance of this thing
will be much better if you manage to rip out the cursor completely and
use a single, set-based update statement to work on the whole table at
once. With more information (see the link above), people in this group
are probably able to help you get there.

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis

 >> Stay informed about: @@FETCH_STATUS Problem 
Back to top
Login to vote
Mike

External


Since: Jun 03, 2008
Posts: 2



(Msg. 3) Posted: Sun Jun 08, 2008 7:39 am
Post subject: Re: @@FETCH_STATUS Problem [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Many thanks for getting back to me, it turned out that the sp ran ok
it was just that it was timing out, extending the timeout period
solved the problem. But you are right, it ran really slowly and I will
investigate the alternative method you proposed.

On 3 Jun, 22:12, Hugo Kornelis
wrote:
> On Tue, 3 Jun 2008 00:39:14 -0700 (PDT), Mike wrote:
> >I have a table with 15000 records in it where I need to update a
> >couple of columns from a calculation. I would normally do this through
> >ADO and a bit of VB but I thought I would have a go at doing this
> >using a cursor in a stored procedure having never done it before. I
> >have followed the advice on the web but whenever I run the procedure
> >it gets between 100 and 1500 records into the table and then just
> >stops. The records have been imported from an Excel spread sheet.
> >Below is the SP:
>
> (snip)
>
> Hi Mike,
>
> You didn't post enough information to reproduce the problem (seewww.aspfaq.com/5006for what we need). However, I *THINK* that the
> problem is related to using default cursor options, probably in
> combination with there not being a proper key on the table. You might
> find that the problem goes away if you explicitly ask for a STATIC
> cursor.
>
> That being said, you'll probably find that the performance of this thing
> will be much better if you manage to rip out the cursor completely and
> use a single, set-based update statement to work on the whole table at
> once. With more information (see the link above), people in this group
> are probably able to help you get there.
>
> --
> Hugo Kornelis, SQL Server MVP
> My SQL Server blog:http://sqlblog.com/blogs/hugo_kornelis
 >> Stay informed about: @@FETCH_STATUS Problem 
Back to top
Login to vote
Adam Backlund

External


Since: Jan 05, 2012
Posts: 1



(Msg. 4) Posted: Thu Jan 05, 2012 5:28 am
Post subject: Re: @@FETCH_STATUS Problem [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Hi,
Shouldn't the last 'FETCH' statement be outside the 'BEGIN END' of the 'if (@@FETCH_STATUS <> -2)'?
It now looks like if the @@FETCH_STATUS would be 2, then there will be an eternal loop since the FETCH will never be executed anymore.
Am I right or have I missed something here?

Regards

> On Tuesday, June 03, 2008 5:12 PM Hugo Kornelis wrote:

> On Tue, 3 Jun 2008 00:39:14 -0700 (PDT), Mike wrote:
>
> (snip)
>
> Hi Mike,
>
> You didn't post enough information to reproduce the problem (see
> www.aspfaq.com/5006 for what we need). However, I *THINK* that the
> problem is related to using default cursor options, probably in
> combination with there not being a proper key on the table. You might
> find that the problem goes away if you explicitly ask for a STATIC
> cursor.
>
> That being said, you'll probably find that the performance of this thing
> will be much better if you manage to rip out the cursor completely and
> use a single, set-based update statement to work on the whole table at
> once. With more information (see the link above), people in this group
> are probably able to help you get there.
>
> --
> Hugo Kornelis, SQL Server MVP
> My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis


>> On Wednesday, June 04, 2008 9:20 PM Mike wrote:

>> I have a table with 15000 records in it where I need to update a
>> couple of columns from a calculation. I would normally do this through
>> ADO and a bit of VB but I thought I would have a go at doing this
>> using a cursor in a stored procedure having never done it before. I
>> have followed the advice on the web but whenever I run the procedure
>> it gets between 100 and 1500 records into the table and then just
>> stops. The records have been imported from an Excel spread sheet.
>> Below is the SP:
>>
>> ALTER PROCEDURE BriminghamSP
>> AS
>> declare @ID int
>> declare @postcode char(Cool
>> declare @RecordCount int
>> declare @RiskFactor decimal(18,4)
>> declare @Risk varchar(10)
>> declare @Homes int
>> declare @Leads int
>> declare @Easting float
>> declare @Northing float
>> declare myCursor CURSOR FOR select id, postcode1 from SPBirmingham
>> open myCursor
>> FETCH NEXT FROM myCursor into @id, @postcode
>> while (@@FETCH_STATUS <> -1)
>> begin
>> if (@@FETCH_STATUS <> -2)
>> begin
>> set @Risk = ''
>> set @RiskFactor = 0
>> set @Homes = 0
>> set @Leads = 0
>> SELECT @RecordCount = count(postCode) FROM
>> dbo.EnglandMappointLeads2001to2005 WHERE postCode = @postCode
>> if @RecordCount > 0
>> begin
>> SELECT @Easting = E, @Northing = N FROM
>> dbo.EnglandMappointLeads2001to2005 WHERE postCode = @postCode
>> SELECT @Leads = sum(Leads), @Homes = sum(Homes) FROM
>> dbo.EnglandMappointLeads2001to2005 WHERE sqrt(square(@Easting-E)
>> +square(@Northing-N))<=500
>> if (@Leads > 0 and @Homes > 0)
>> begin
>> set @RiskFactor = convert(decimal(18,4),@Leads)/
>> convert(decimal(18,4),@Homes)
>> end
>> if @RiskFactor < 0.0022
>> begin
>> set @Risk = 'Low'
>> end
>> if (@RiskFactor > 0.0022 and @RiskFactor <= 0.0044)
>> begin
>> set @Risk = 'Medium'
>> end
>> if (@RiskFactor > 0.0044 and @RiskFactor <= 0.0088)
>> begin
>> set @Risk = 'High'
>> end
>> if @RiskFactor > 0.0088
>> begin
>> set @Risk = 'Very High'
>> end
>> end
>> update spbirmingham set leads = @Leads, homes = @homes, risk_factor =
>> @RiskFactor, risk = @Risk where id = @id
>> FETCH NEXT FROM myCursor INTO @id, @postcode
>> end
>> end
>> close myCursor
>> deallocate myCursor
>>
>> I tried the @@FETCH_STATUS <> -1 etc but this made no difference


>>> On Monday, June 09, 2008 1:11 AM Mike wrote:

>>> Many thanks for getting back to me, it turned out that the sp ran ok
>>> it was just that it was timing out, extending the timeout period
>>> solved the problem. But you are right, it ran really slowly and I will
>>> investigate the alternative method you proposed.
>>>
>>> On 3 Jun, 22:12, Hugo Kornelis
>>> wrote:
 >> Stay informed about: @@FETCH_STATUS Problem 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
How to reset @@fetch_status - Hi SQL server experts: I have a while loop that fetch each row from the table. some rows have bad data which I want to ignore and continue fetching the next row. But the fetch failed after encountering the first bad row. the rest of the table rows neve...

Problem - I've posted this here and in Security w/o a response, so maybe my question is confusing. But here's hoping: I’ve got two servers, say ServerA and ServerB, that are Windows 2000 Adv'd Server SP4 & Sql Server 2000 SP4 . We are going to have to g...

Problem! help! - Hi I have a table called "Member" as given below.. familyID memberID firstName -------- -------- -------------------- 0 7 Stuart 0 5 Kasey 0 1 Sally 0 2 Cooper 1 9 Rosemary ...

Help with this problem - Hello All DDL ================================================================================== set nocount on declare @LeaveTable table (LeaveID int IDENTITY (1, 1) NOT NULL ,EmployeeID int ,LeaveTypeID int ,LeaveSubTypeID int ,LeaveStartDate..

Collation problem - How can I set the database can read english, chinese Traditional and chinese simplified? Thanks
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]