Hi George Thanks.
Background info:
There are two table:
_SMDBA_.LEASEAGRMNT AND
_SMDBA_.LEASEYEARDET
I get the start and end dates from the _SMDBA_.LEASEAGRMNT table for the @LeaseSequenceNo passed in as a parameter.
What I need to do is for every matching seq_lease_agreement number(@LeaseSequenceNo)in _SMDBA_.LEASEYEARDET create a record between the start and end dates retrieved from the _SMDBA_.LEASEAGRMNT table.
I've included a SQL statement at the bottom of the Procedure to show how the 2 tables are joined.
CREATE PROCEDURE LBHF_AddAnnualLeaseValue
@LeaseSequenceNo int=0
AS
declare @rows int
declare @error int
declare @SEQ int
begin
declare @StartDate DATETIME
declare @EndDate DATETIME
set @StartDate = "select start_date from _SMDBA_.LEASEAGRMNT where sequence = @LeaseSequenceNo"
set @EndDate = "select end_date from _SMDBA_.LEASEAGRMNT where sequence = @LeaseSequenceNo"
declare @YearNo integer
select @YearNo = Year(@StartDate)
--declare @data leaseyeardet (Year_number integer identity(1,1), FinancialPeriod varchar(100))
WHILE @YearNo < Year(@EndDate)
begin
EXEC SMSYSGETNEXTRECNUMDB 'LEASEYEARDET', @SEQ OUTPUT
insert into _smdba_.leaseyeardet
(sequence,
lastmodified,
lastuser,
_group_,
[_inactive_:],
year_number,
financial_year,
note,
seq_lease_agreement,
annual_value,
lease_open,
costperthousandperyear,
tot_item_value,
ann_payment)
values
(@SEQ,
getdate(),
'MAGIC',
1,
0,
0,
0,
'',
@LeaseSequenceNo,
0,
1,
0,
0,
0)
--where _smdba_.leaseyeardet.seq_lease_agreement = @LeaseSequenceNo)
set @YearNo =@YearNo+1
end
END
GO
I know I have the WHERE claues in the wrong place. I'm not sure where it goes.
Thanks for your help.
Here's the SQL statement:
SELECT _SMDBA_.LEASEYEARDET.SEQUENCE, _SMDBA_.LEASEYEARDET.LASTMODIFIED, _SMDBA_.LEASEYEARDET.LASTUSER,
_SMDBA_.LEASEYEARDET._GROUP_, _SMDBA_.LEASEYEARDET.[_INACTIVE_:], _SMDBA_.LEASEYEARDET.YEAR_NUMBER,
_SMDBA_.LEASEYEARDET.FINANCIAL_YEAR, _SMDBA_.LEASEYEARDET.NOTE, _SMDBA_.LEASEYEARDET.SEQ_LEASE_AGREEMENT,
_SMDBA_.LEASEYEARDET.ANNUAL_VALUE, _SMDBA_.LEASEYEARDET.LEASE_OPEN, _SMDBA_.LEASEYEARDET.COSTPERTHOUSANDPERYEAR,
_SMDBA_.LEASEYEARDET.TOT_ITEM_VALUE, _SMDBA_.LEASEYEARDET.ANN_PAYMENT
FROM _SMDBA_.LEASEAGRMNT INNER JOIN
_SMDBA_.LEASEYEARDET ON _SMDBA_.LEASEAGRMNT.SEQUENCE = _SMDBA_.LEASEYEARDET.SEQ_LEASE_AGREEMENT
WHERE (_SMDBA_.LEASEYEARDET.[_INACTIVE_:] = 0)AND (_SMDBA_.LEASEAGRMNT.sequence = @LeaseSequenceNo )