Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 26th, 2004, 11:21 AM
Authorized User
 
Join Date: Mar 2004
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default Stored procedure question

Hi all

I'm starting to get my head round stored procedures (normally I write ASP pages for everything!)

This stored procedure examines the end date in a particular table and where it has lapsed it updates both itself and a child table to roll a contract on.

If Exec this in query analyser it gives the correct results as long as I comment out the two update statements if not all it does it run one record then stop.

Appreciate if some can give me a clue as to what I've got wrong

Thanks
Graham

CREATE procedure dbo.cascade_hire_end as

set nocount off
declare @rn int
declare @drn int
declare @hire_end datetime
declare @datetime datetime
declare @newdate datetime
select @datetime=getdate()
declare @vehicle_id nvarchar(12)
declare @driver_name nvarchar(255)
declare @alloc int

-- Declare the cursor
declare vehicles_cursor cursor local for
     select record_number,vehicle_id,driver_name,driver_record _number,hire_end from vehicles where archive_status<>'A' and depot_id<>'DEHIRED' and left(vehicle_status,1)<>'D' and (len(ltrim(quote_ref))=0 or quote_ref is null) and datediff(d,vehicles.hire_end,getdate())>0 and hire_end<>'1800/01/01' and hire_end is not null order by vehicle_id

open vehicles_cursor
fetch next from vehicles_cursor into @rn,@vehicle_id,@driver_name,@drn,@hire_end

while @@fetch_status=0
begin
-- loop
    fetch next from vehicles_cursor into @rn,@vehicle_id,@driver_name,@drn,@hire_end
    select @newdate=@hire_end

    while datediff(d,@newdate,getdate())>0
    begin
        select @newdate=dateadd(m,1,@newdate)

    end

    update platinum_sql.dbo.vehicles set changed_by='Cascade',changed_date=getdate(),hire_e nd=@newdate where record_number=@rn
    update platinum_sql.dbo.allocations set changed_by='Cascade',changed_date=getdate(),end_da te=@newdate where vehicle_record_number=@rn and driver_record_number=@drn and end_date=@hire_end
    print 'Vehicle= '+@vehicle_id+' driven by '+@driver_name+' due to expire on '+cast(@hire_end as nvarchar)+' now extended to ' +cast(@newdate as nvarchar)

end
-- tidy
close vehicles_cursor
deallocate vehicles_cursor
GO


 
Old November 26th, 2004, 11:47 AM
Authorized User
 
Join Date: Oct 2004
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hard to say without knowing the data that is involved. But I would try defining the cursor as dynamic e.g. declare vehicles_cursor cursor local dynamic for
 
Old November 26th, 2004, 12:04 PM
Authorized User
 
Join Date: Mar 2004
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hiya

No that didn't do it!

Just seems weird that it works fine if remove the update lines, query analyser shows me the records that have expired and what the contract needs to be extended to so its back in date,

I've also put @@error checks since I posted and QA does not report problems

Oh well suppose I'll have to put the code back into the ASP page for now.

If you come across anything else would appreciate a post.

Regards



 
Old November 26th, 2004, 03:58 PM
Authorized User
 
Join Date: Oct 2004
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I know this is not the problem but you do have a bug in the code in that you open the cursor and do a fetch next. You then go into the loop and do a further fetch next which means the first record in the cursor is not processed.

It is also worth mentioning that cursors are to be avoided if you can. They can makes things go very slow.





Similar Threads
Thread Thread Starter Forum Replies Last Post
FormView and Stored Procedure question mtschindler ASP.NET 2.0 Basics 0 November 16th, 2007 05:11 PM
This Stored Procedure rao965 SQL Server 2000 2 July 2nd, 2007 07:21 PM
stored procedure kdm260 SQL Server 2000 2 June 19th, 2006 04:45 PM
Help On Stored Procedure desireemm SQL Language 2 October 31st, 2005 07:11 PM
C# and stored procedure Msmsn C# 1 August 26th, 2003 11:03 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.