I currently have two SQL commands.
One retrieves a list of unique IDs from a table.
The other iterates over these IDs calling the same stored procedure, which at the moment is merely an insert statement.
So, to give an example (with fake names), at the moment I am calling :
SELECT DISTINCT ID FROM TableOfIDs;
and storing the returned values in a List, and then calling the following stored procedure.
INSERT INTO Table ([Date], [Number], [ID]) VALUES( @CurrDate,
(SELECT SUM(Number2)
FROM Table2
WHERE RowID = @ID
AND CurrDate = (SELECT MAX(CurrDate)
FROM Table2
WHERE RowID = @ID))
,@ID)
So a simplified table looks like so :
Table
Date | Number | ID (FK)
Table2
CurrDate | RowID(FK) | Number2
Both IDs are foreign keys into the TableOfIDs table.
How can I make it so that all of this is run in either one query, or one stored procedure rather than retrieving all of the IDs in one query, and iterating over them calling ~400 stored procedures a night? 🙂
Thanks very much; if any more detail is required please ask and I will fill in the gaps.
They are both foreign keys to the same table.
1 Answer