I’m working on a .NET component that gets a set of data from the database, performs some business logic on that set of data, and then updates single records in the database via a stored procedure that looks something like spUpdateOrderDetailDiscountedItem.
For small sets of data, this isn’t a problem, but when I had a very large set of data that required an iteration of 368 stored proc calls to update the records in the database, I realized I had a problem. A senior dev looked at my stored proc code and said it looked fine, but now I’d like to explore a better method for sending “batch” data to the database.
What options do I have for updating the database in batch? Is this possible with stored procs? What other options do I have?
I won’t have the option of installing a full-fledged ORM, but any advice is appreciated.
Additional Background Info:
Our current data access model was built 5 years ago and all calls to the db currently get executed via modular/static functions with names like ExecQuery and GetDataTable. I’m not certain that I’m required to stay within that model, but I’d have to provide a very good justification for going outside of our current DAL to get to the DB.
Also worth noting, I’m fairly new when it comes to CRUD operations and the database. I much prefer to play/work in the .NET side of code, but the data has to be stored somewhere, right?
Stored Proc contents:
ALTER PROCEDURE [dbo].[spUpdateOrderDetailDiscountedItem]
-- Add the parameters for the stored procedure here
@OrderDetailID decimal = 0,
@Discount money = 0,
@ExtPrice money = 0,
@LineDiscountTypeID int = 0,
@OrdersID decimal = 0,
@QuantityDiscounted money = 0,
@UpdateOrderHeader int = 0,
@PromoCode varchar(6) = '',
@TotalDiscount money = 0
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
Update OrderDetail
Set Discount = @Discount, ExtPrice = @ExtPrice, LineDiscountTypeID = @LineDiscountTypeID, LineDiscountPercent = @QuantityDiscounted
From OrderDetail with (nolock)
Where OrderDetailID = @OrderDetailID
if @UpdateOrderHeader = -1
Begin
--This code should get code the last time this query is executed, but only then.
exec spUpdateOrdersHeaderForSkuGroupSourceCode @OrdersID, 7, 0, @PromoCode, @TotalDiscount
End
An easy and alternative way I’ve seen in use is to build a SQL statement consisting of sql_execs calling the sproc with the parameters in the string. Not sure if this is advised or not, but from the .NET perspective, you are only populating one SqlCommand and calling ExecuteNonQuery once…
Note if you choose this then please, please use the
StringBuilder! 🙂Update: I much prefer Chris Lively’s answer, didn’t know about table-valued parameters until now… unfortunately the OP is using 2005.