Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Stored Proc Help Needed
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Stored Proc Help Needed

 Post Reply Post Reply Page  12>
Author
Gary View Drop Down
Senior Member
Senior Member
Avatar

Joined: 20 March 2002
Location: United Kingdom
Status: Offline
Points: 326
Post Options Post Options   Thanks (0) Thanks(0)   Quote Gary Quote  Post ReplyReply Direct Link To This Post Topic: Stored Proc Help Needed
    Posted: 29 September 2003 at 5:58am

Hi,

I have a table (tblContracts) which has 2 columns (Months, Value) where Months is the length of the contract and Value is the total value of the contract - typical values may be 12 months & £24000.
I want to insert the monthly payment schedule to another table (tblSchedule) where each row would represent 1 month's payment, so in my example there would be 12 rows written, each with a value of £2000. Also, there is a third column which needs to identify whether the payment is the first in a series ('N'ew) or part of an existing collection ('R'epeat).

So, what I need is a way of doing this (sp???) whereby data is read from tblContracts and written to tblSchedule. I guess that a Stored Procedure containing a loop and conditional statements is needed.

Can anyone offer any advice ????

Thanks
Gary

Back to Top
michael View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
Post Options Post Options   Thanks (0) Thanks(0)   Quote michael Quote  Post ReplyReply Direct Link To This Post Posted: 29 September 2003 at 9:17am

Well it is a more or less manual process for every contract, otherwise place it in a trigger to fire when a new contract has been added:

Create PROCEDURE updatecontracts
 @contractid int         &nb sp;   --the contractid from tbl_contracts

AS
Declare @valcounter int      --Just a temp counter for the loop
DECLARE @series char(1)      --For new or recurring payment
DECLARE @noofmonth int       --how many month are in the schedule
DECLARE @value money         --how much is the contract
Set @valcounter = 1
Set @series = 'N'
Select @noofmonth = (Select [Month] from tblContracts where [id] = @contractid)
Select @value = (Select [Value] from tblContracts where [id] = @contractid)

WHILE @valcounter < @noofmonth + 1
BEGIN  
 INSERT INTO tblSchedule (fk,paymentmonth, value, paymentseries)
 VALUES
  (@contractid, @valcounter, @value / @noofmonth, @series)
 Set @valcounter = @valcounter + 1
 Set @series = 'R'
END
GO

Back to Top
michael View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
Post Options Post Options   Thanks (0) Thanks(0)   Quote michael Quote  Post ReplyReply Direct Link To This Post Posted: 29 September 2003 at 9:21am
forgot to say I assumed a foreign key (fk) in the tableschudule relating to the contractid
Back to Top
Gary View Drop Down
Senior Member
Senior Member
Avatar

Joined: 20 March 2002
Location: United Kingdom
Status: Offline
Points: 326
Post Options Post Options   Thanks (0) Thanks(0)   Quote Gary Quote  Post ReplyReply Direct Link To This Post Posted: 29 September 2003 at 9:53am

Brilliant (almost )

Your solution works great Michael, excpet that the sp will only work for 1 contract at a time. What I need is for the sp to go through the entire contract table and insert the schedule details for each and every contract.

Please excuse my ignorance, but I have zero experience with sp's and dont have a clue here. In theory, it should be quite easy, but what about the payment type ('N' / 'R') - it would only be 'N' for the first contract, not the first payment of the other contracts.

 

Back to Top
michael View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
Post Options Post Options   Thanks (0) Thanks(0)   Quote michael Quote  Post ReplyReply Direct Link To This Post Posted: 29 September 2003 at 1:15pm

Oh, ok here goes:
I was trying to get around re-inserting values into the schedule table that were already there using a coursor but first off I do not like cursors and for some reason it always resulted into an endless loop, anyways, here is a solution that should work even though it might not be the most elegant.

ALTER  PROCEDURE updatecontracts
AS
DECLARE @contractid int
Declare @valcounter int
DECLARE @series char(1)
DECLARE @noofmonth int
DECLARE @value money
Set @contractid = (Select TOP 1 c.[id] from tblcontracts c LEFT JOIN tblschedule s ON s.fk=c.[id]) 
Set @noofmonth = (Select [Month] from tblContracts where [id] = @contractid)
Set @valcounter = 1
Set @series = 'N' 
WHILE (select count(*) from tblschedule s) < (Select SUM([MONTH]) from tblcontracts)
 BEGIN
 --This is the loop for inserting schedule
 WHILE @valcounter < @noofmonth + 1
  BEGIN
   Set @value = (Select [Value] from tblContracts where [id] = @contractid) 
   INSERT INTO tblSchedule (fk,paymentmonth, value, paymentseries)
      VALUES
       (@contractid, @valcounter, @value / @noofmonth, @series)
    --Re-initializing variables after loop run
   Set @valcounter = @valcounter + 1
    Set @series = 'R'
   END
 Set @contractid = (Select TOP 1 c.[id] from tblcontracts c LEFT OUTER JOIN tblschedule s ON s.fk=c.[id] WHERE s.fk IS NULL) 
  Set @noofmonth = (Select [Month] from tblContracts where [id] = @contractid)
  Set @valcounter = 1
 Set @series = 'N'

 --End inserting schedule loop
 END

Added: Change the script to Create Procedure instead of  Alter Procedure if you deleted it already.



Edited by michael
Back to Top
michael View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
Post Options Post Options   Thanks (0) Thanks(0)   Quote michael Quote  Post ReplyReply Direct Link To This Post Posted: 29 September 2003 at 1:18pm
Oh, one more thing, you mentioned about the 'R' and the 'N', it took care of that in the first script already unless I misunderstood you.
Back to Top
Gary View Drop Down
Senior Member
Senior Member
Avatar

Joined: 20 March 2002
Location: United Kingdom
Status: Offline
Points: 326
Post Options Post Options   Thanks (0) Thanks(0)   Quote Gary Quote  Post ReplyReply Direct Link To This Post Posted: 30 September 2003 at 2:21am

Michael,

You are a  - that worked a treat and I am eternally grateful to you for your assistance .

Just a little background - My colleague is working on this project and coded the SQL to be executed directly from an asp page. Boy was it ugly !!!

Back to Top
michael View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
Post Options Post Options   Thanks (0) Thanks(0)   Quote michael Quote  Post ReplyReply Direct Link To This Post Posted: 30 September 2003 at 7:00am
Glad it worked out for you.
Back to Top
 Post Reply Post Reply Page  12>

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.