I have created a stored proc that does the following:
1. Creates a one column recordset of client numbers from a union query (never more than 150 records)
2. Open a cursor and loop through the recordset inserting a new record into an expense statement table based on the current record.
3. Prior to going to the next record, I need to get the primary key from the newly created record to perform an update on the expenses table.
How do I get the StatementNumber(primary key of expense statements table) for the newly added record? Is there a Bookmark-type feature so I can reference it?
Here's the code from the stored proc:
1. Creates a one column recordset of client numbers from a union query (never more than 150 records)
2. Open a cursor and loop through the recordset inserting a new record into an expense statement table based on the current record.
3. Prior to going to the next record, I need to get the primary key from the newly created record to perform an update on the expenses table.
How do I get the StatementNumber(primary key of expense statements table) for the newly added record? Is there a Bookmark-type feature so I can reference it?
Here's the code from the stored proc:
Code:
--insert Union query into the cursor
OPEN c1
FETCH NEXT FROM c1
INTO @ClientKey
-- loop until we run out of rows
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @LastStatementNumber = Max(StatementNumber)
FROM tExpenses WHERE ClientKey = @ClientKey
INSERT INTO tExpenseStatements (ClientKey, StatementDate, LastStatementNumber)
VALUES (@ClientKey, @RunDate, @LastStatementNumber)
FETCH NEXT FROM c1
INTO @ClientKey
END
CLOSE c1
DEALLOCATE c1