Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Insert and update

Author  Topic 

OldMySQLUser
Constraint Violating Yak Guru

301 Posts

Posted - 2009-09-10 : 04:48:25
I wish to extend an existing INSERT script so that another table can also be updated from the same passed parameters.

Here is my script:


CREATE PROCEDURE InsertPaymentArrangements

@DebtorID int,
@FirstPaymentDate datetime,
@LastPaymentDate datetime,
@NextPaymentDate datetime,
@NextReviewDate datetime,
@DayOfMonth int,
@ArrangementDate datetime,
@CompletionDate datetime,
@AmountInWords varchar(60),
@AmountInFigures money,
@PaymentTypeID int,
@NumberOfPayments int,
@ExpiryDate datetime,
@ArrangementComplete bit,
@PaymentCategory int,
@APR float,
@PaymentFrequency int,
@CreatedBy int,
@CreatedOn datetime,
@TimeStamp timestamp OUTPUT

AS

INSERT INTO [PaymentArrangements] (
DebtorID, FirstPaymentDate, LastPaymentDate, NextPaymentDate,
NextReviewDate, DayOfMonth, ArrangementDate, CompletionDate, AmountInWords,
AmountInFigures, PaymentTypeID, NumberOfPayments, CreatedBy,
CreatedOn, ExpiryDate, ArrangementComplete, PaymentCategory, APR
)

VALUES(

@DebtorID, @FirstPaymentDate, @LastPaymentDate, @NextPaymentDate,
@NextReviewDate, @DayOfMonth, @ArrangementDate, @CompletionDate, @AmountInWords,
@AmountInFigures, @PaymentTypeID, @NumberOfPayments, @CreatedBy,
@CreatedOn, @ExpiryDate, @ArrangementComplete, @PaymentCategory, @APR
)

IF @@ROWCOUNT > 0
BEGIN
SELECT @TimeStamp = [TimeStamp]
FROM PaymentArrangements
WHERE PaymentArrangementID = SCOPE_IDENTITY()
END

GO

-- update Debtor table for report/letter merge references ------

UPDATE Debtor

SET

NoOfPaymentsDue = @NumberOfPayments,
PaymentReviewDate = @PaymentReviewDate,
FirstPaymentDate = @FirstPaymentDate,
DayOfMonth = @DayOfMonth,
PaymentTypeID = @PaymentTypeID,
AmountInWords = @AmountInWords,
AmountInFigures = @AmountInFigures

WHERE
DebtorID = @DebtorID AND
[TimeStamp] = @TimeStamp


IF @@ROWCOUNT > 0
BEGIN
SELECT @TimeStamp = [TimeStamp]
FROM Debtor
WHERE DebtorID = @DebtorID
END


But I am getting the error

*** ERROR: Must declare the scalar variable "@TimeStamp". Must declare the scalar variable "@NumberOfPayments". ***

How should I have correctly structured my script please?



webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-09-10 : 04:52:31
you cannot use variable when a GO ends the batch.
delete that GO and try.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

OldMySQLUser
Constraint Violating Yak Guru

301 Posts

Posted - 2009-09-10 : 04:56:24
quote:
Originally posted by webfred

you cannot use variable when a GO ends the batch.
delete that GO and try.


No, you're never too old to Yak'n'Roll if you're too young to die.



D'oh!!!

Many thanks for the reply webfred. Much appreciated.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-09-10 : 05:07:44
welcome


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-09-10 : 06:32:30
Here is fun with GO
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/09/05/fun-with-go.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -