This snippet is a query which does select Insert operation into a table. We here would see that creating a Stored procedure which actually selects the data from a table and may be insert into the same table or other tables with identical columns. here I would show you how to insert into the same table.
This is really handy when there is a huge amount of records to be selected and inserted and using foreach in Entity Framework would hamper the performance heavily.
- ALTER PROCEDURE [dbo].[spAddRecordsForNewX]
-
- @fkID INT,
- @conditionID INT,
- AS
- BEGIN
-
- BEGIN TRANSACTION
- INSERT INTO
- dbo.[TABLE#]
- SELECT
- @fkID,
- pRel.Column1,
- pRel.Column2,
- pRel.Column3,
- pRel.Column4,
- pRel.Column5,
- FROM
- dbo.[TABLE#] as pRel
- WHERE
- PRIMARYKEY = @conditionID
- IF @Rollback = 1
- ROLLBACK TRANSACTION
- ELSE
- COMMIT
- //The transanction is trackerd and rolled back if any issue
I hope this helps.
Please use it and improve performance.
Thanks