I using .net core mvc create a web then I need to update mutiple row at one time by store procedure . how should i do, plese
Loading
I using .net core mvc create a web then I need to update mutiple row at one time by store procedure . how should i do, plese
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Anupam MaitiPosted Sep 26, 2024, 6:28 PM
Using a Table-Valued Parameter to perform bulk updates in SQL Server can be highly efficient. I implemented a similar approach in one of my projects.
Let's see in details:
1- Create Table Type in Db.
2- Create SP in Db.
3- Create entity and DbContect
4 - Add method to call the SP
I will create an article and add detailed steps. Once done I will share here.
Manikandan MurugesanPosted Sep 25, 2024, 10:01 AM
Kindly refer to the following URL:
https://dotnettutorials.net/lesson/bulk-insert-and-update-using-stored-procedure-in-ado-net-core/
https://stackoverflow.com/questions/20635796/bulk-update-in-c-sharp
Gurpreet AroraPosted Sep 25, 2024, 4:22 AM
Example Stored Procedure for Updating Multiple Rows:
Suppose you want to update the locations with specific
LIDvalues.1. Using Parameters for Each Row
Executing the Procedure:
You would call this stored procedure by passing values for each parameter:
EXEC UpdateLocations 1, 'Chittagong', 2, 'Barisal';
EXEC UpdateLocations 1, 'Chittagong', 2, 'Barisal';
This will update two rows at once, changing the location for
LID1 and 2.2. Using a Temporary Table or Table-Valued Parameter
If you have many rows to update, passing a table as a parameter is more efficient. Here's how you can use a table-valued parameter to update multiple rows at once:
Step 1: Create a User-Defined Table Type
CREATE TYPE LocationUpdateTable AS TABLE
(
LID INT,
NewLocation VARCHAR(100)
);
Step 2: Create the Stored Procedure
Step 3: Execute the Stored Procedure
You can pass a table variable to the stored procedure:
DECLARE @Updates LocationUpdateTable;
-- Insert values for updates
INSERT INTO @Updates (LID, NewLocation) VALUES (1, 'Chittagong');
INSERT INTO @Updates (LID, NewLocation) VALUES (2, 'Barisal');
-- Execute the stored procedure
EXEC UpdateMultipleLocations @Updates;
This approach is better for batch updates when you have multiple rows to update, and it allows you to scale more easily without hardcoding parameters.