Reputation: 2475
I have a table, and it has the Columns Quantity, Price and LineTotal. When I add something to the Cart, I add the Quantity and the Price of the product, and when the user visits the Cart page to view their cart, I want to re-calculate the LineTotal when they update the quantities for the items they've selected.
Now, my question is, should I re-calculate the LineTotal of each item in the cart using SQL (and if so, how?) or, should I do it in C# (And if so, what would be the best way to go about this?) To my surprise, I can't really seem to find anything on calculations in SQL - other than forums where people talk about it, but I have yet to see any code or documentation.
Upvotes: 1
Views: 704
Reputation: 52655
Well if you wanted to you could change the LineTotal column to a computed column but there's nothing stopping you from doing it in C# before the Insert/update
Since it seems to be a fairly simple calculation its tough to really strongly go with one or the other, and its mostly up to preference
Computed Column sample
ALTER TABLE dbo.YourTable
ADD LineTotalComputed AS (Quantity * Price) PERSISTED
Upvotes: 1
Reputation: 39480
You should be able to use the arithmetic operators within SQL to generate your line totals. Reference here.
Upvotes: 1