I'm trying to accomplish the following thing: I receive a DocumentID. Find all the records in a table that match the specific DocumentID, for example I have 10 records matching and every record is with different DocumentAttachmentID. I update all the records with the new data. The problem comes that I need to insert some of the information from these ten records + other information received to a new table, which is History table, i.e. I need to insert ten new records there.
I've succeeded to this with Cursor, but it looks like that the Cursor isn't really good, because of the performance.
Is there a way to loop throught the 10 records that I selected from this table and for every record to take some information, add some additional info and then insert this in the other table ?
EDIT: I tried to do this withoud looping(thanks you all for the answers) I will try it tomorrow, do you think this is gonna work ?:
With the first Update, I update all documentAttachments, The second block is INSERT TO, which should insert all document attachments in the other table with some extra columns.
UPDATE [sDocumentManagement].[tDocumentAttachments]
SET DeletedBy = @ChangedBy,
DeletedOn = @CurrentDateTime,
IsDeleted = 1,
WHERE DocumentID = @DocumentID;
INSERT INTO [sDocumentManagement].[tDocumentHistory] ( DocumentAttachmentID, DocumentID, ActivityCodeID, ChangedOn, ChangedBy, AdditionalInformation )
SELECT DocumentAttachmentID,
@DocumentID, [sCore].[GetActivityCodeIDByName] ( 'DeletedDocument' ),
@CurrentDateTime,
@ChangedBy,
@AdditionalInformation
FROM [sDocumentManagement].[tDocumentAttachments]
WHERE DocumentID = @DocumentID;